module HaveAPI::ModelAdapters::ActiveRecord::Action::InstanceMethods

Public Instance Methods

ar_default_includes() click to toggle source

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.

# 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
ar_inner_includes(includes) click to toggle source

Called by ar_parse_includes for recursion purposes.

# 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
ar_parse_includes(raw) click to toggle source

Parse includes sent by the user and return them in an array of symbols and hashes.

# 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
ar_with_pagination(q, parameter: :from_id, check: false) { |q, paginable| ... } click to toggle source

@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

# 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
with_asc_pagination(q = nil) click to toggle source

Apply pagination on query in ascending order @param q [ActiveRecord::Relation] query to apply pagination on

# 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
Also aliased as: with_pagination
with_desc_pagination(q = nil) click to toggle source

Apply pagination on query in descending order @param q [ActiveRecord::Relation] query to apply pagination on

# 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
with_includes(q = nil) click to toggle source

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.

# 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
with_pagination(q = nil)
Alias for: with_asc_pagination