module HaveAPI::ModelAdapters::ActiveRecord::Action::InstanceMethods
Public Instance Methods
Source
# File lib/haveapi/model_adapters/active_record.rb, line 129 def ar_default_includes ret = [] self.class.output.params.each do |p| if p.is_a?(HaveAPI::Parameters::Resource) && self.class.model.reflections[p.name.to_sym] ret << p.name.to_sym end end ret end
Default includes contain all associated resources specified inaction output parameters. They are fetched from the database anyway, to return the label for even unresolved association.
Source
# File lib/haveapi/model_adapters/active_record.rb, line 108 def ar_inner_includes(includes) args = [] includes.each do |assoc| if assoc.index('__') tmp = {} parts = assoc.split('__') tmp[parts.first.to_sym] = ar_inner_includes([parts[1..].join('__')]) args << tmp else args << assoc.to_sym end end args end
Called by ar_parse_includes
for recursion purposes.
Source
# File lib/haveapi/model_adapters/active_record.rb, line 87 def ar_parse_includes(raw) return @ar_parsed_includes if @ar_parsed_includes @ar_parsed_includes = ar_inner_includes(raw).select do |inc| # Drop associations that are not registered in the AR: # The API resource may have associations that are not based on # associations in AR. if inc.is_a?(::Hash) inc.each_key do |k| next(false) unless self.class.model.reflections.has_key?(k.to_s) end else next(false) unless self.class.model.reflections.has_key?(inc.to_s) end true end end
Parse includes sent by the user and return them in an array of symbols and hashes.
Source
# File lib/haveapi/model_adapters/active_record.rb, line 67 def ar_with_pagination(q, parameter: :from_id, check: false) pk = self.class.model.primary_key if check && !pk.is_a?(String) raise 'only simple primary key is supported, ' \ "#{self.class.model} has a composite primary key (#{pk.join(', ')})" end q ||= self.class.model.all paginable = input[parameter] limit = input[:limit] q = yield(q, paginable) if paginable q = q.limit(limit) if limit q end
@param q [ActiveRecord::Relation] query to apply pagination on @param parameter [Symbol] input parameter used for pagination @param check [Boolean] raise if the model does not have a simple primary key @yieldparam q [ActiveRecord::Relation] query to apply pagination on @yieldparam parameter [any] value of the input parameter
Source
# File lib/haveapi/model_adapters/active_record.rb, line 46 def with_asc_pagination(q = nil) ar_with_pagination(q, check: true) do |query, from_id| query.where("`#{self.class.model.table_name}`.`#{self.class.model.primary_key}` > ?", from_id) end end
Apply pagination on query in ascending order @param q [ActiveRecord::Relation] query to apply pagination on
Source
# File lib/haveapi/model_adapters/active_record.rb, line 54 def with_desc_pagination(q = nil) ar_with_pagination(q, check: true) do |query, from_id| query.where("`#{self.class.model.table_name}`.`#{self.class.model.primary_key}` < ?", from_id) end end
Apply pagination on query in descending order @param q [ActiveRecord::Relation] query to apply pagination on
Source
# File lib/haveapi/model_adapters/active_record.rb, line 26 def with_includes(q = nil) q ||= self.class.model includes = meta && meta[:includes] args = includes.nil? ? [] : ar_parse_includes(includes) # Resulting includes may still contain duplicities in form of nested # includes. ar_default_includes returns a flat array where as # ar_parse_includes may contain hashes. But since ActiveRecord is taking # it well, it is not necessary to fix. args.concat(ar_default_includes).uniq if args.empty? q else q.includes(*args) end end
Helper method that sets correct ActiveRecord
includes according to the meta includes sent by the user. ‘q` is the model or partial AR query. If not set, action’s model class is used instead.