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

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

Constant Summary collapse

MAX_INCLUDE_DEPTH =
16

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.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/haveapi/model_adapters/active_record.rb', line 153

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_include_tree(parts) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/haveapi/model_adapters/active_record.rb', line 142

def ar_include_tree(parts)
  ret = parts.last
  (parts.size - 2).downto(0) do |i|
    ret = { parts[i] => [ret] }
  end
  ret
end

#ar_inner_includes(includes) ⇒ Object

Kept for callers that used the old parser directly.



106
107
108
109
110
111
112
113
# File 'lib/haveapi/model_adapters/active_record.rb', line 106

def ar_inner_includes(includes)
  includes.filter_map do |assoc|
    parts = assoc.to_s.split('__').reject(&:empty?).map(&:to_sym)
    next if parts.empty? || parts.size > MAX_INCLUDE_DEPTH

    ar_include_tree(parts)
  end
end

#ar_parse_include_path(path, model) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/haveapi/model_adapters/active_record.rb', line 115

def ar_parse_include_path(path, model)
  parts = path.to_s.split('__')
  return if parts.empty? || parts.size > MAX_INCLUDE_DEPTH

  current_model = model
  symbols = []
  i = 0

  while i < parts.size
    part = parts[i]
    return if part.empty?

    begin
      reflection = current_model.reflections[part.to_s]
      return unless reflection

      symbols << part.to_sym
      current_model = reflection.klass
    rescue NameError
      return
    end
    i += 1
  end

  ar_include_tree(symbols)
end

#ar_parse_includes(raw) ⇒ Object

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



97
98
99
100
101
102
103
# File 'lib/haveapi/model_adapters/active_record.rb', line 97

def ar_parse_includes(raw)
  return @ar_parsed_includes if @ar_parsed_includes

  @ar_parsed_includes = raw.filter_map do |assoc|
    ar_parse_include_path(assoc, self.class.model)
  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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/haveapi/model_adapters/active_record.rb', line 69

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]

  if limit && limit > HaveAPI::Actions::Paginable::MAX_LIMIT
    error!(
      "limit has to be maximally #{HaveAPI::Actions::Paginable::MAX_LIMIT}",
      {},
      http_status: 400
    )
  end

  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



48
49
50
51
52
# File 'lib/haveapi/model_adapters/active_record.rb', line 48

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



56
57
58
59
60
# File 'lib/haveapi/model_adapters/active_record.rb', line 56

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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/haveapi/model_adapters/active_record.rb', line 28

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