Module: HaveAPI::ModelAdapters::ActiveRecord::Action::InstanceMethods

Defined in:
lib/haveapi/model_adapters/active_record.rb

Instance Method Summary collapse

Instance Method Details

#ar_default_includesObject

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.



129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object

Called by ar_parse_includes for recursion purposes.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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, parameter| ... } ⇒ Object

Parameters:

  • q (ActiveRecord::Relation)

    query to apply pagination on

  • parameter (Symbol) (defaults to: :from_id)

    input parameter used for pagination

  • check (Boolean) (defaults to: false)

    raise if the model does not have a simple primary key

Yield Parameters:

  • q (ActiveRecord::Relation)

    query to apply pagination on

  • parameter (any)

    value of the input parameter



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object Also known as: with_pagination

Apply pagination on query in ascending order

Parameters:

  • q (ActiveRecord::Relation) (defaults to: nil)

    query to apply pagination on



46
47
48
49
50
# 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

#with_desc_pagination(q = nil) ⇒ Object

Apply pagination on query in descending order

Parameters:

  • q (ActiveRecord::Relation) (defaults to: nil)

    query to apply pagination on



54
55
56
57
58
# 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) ⇒ Object

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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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