Class: HaveAPI::ModelAdapters::ActiveRecord::Output

Inherits:
HaveAPI::ModelAdapter::Output show all
Defined in:
lib/haveapi/model_adapters/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HaveAPI::ModelAdapter::Output

#initialize

Constructor Details

This class inherits a constructor from HaveAPI::ModelAdapter::Output

Class Method Details

.used_by(action) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/haveapi/model_adapters/active_record.rb', line 156

def self.used_by(action)
  action.meta(:object) do
    output do
      custom :path_params, label: 'URL parameters',
                           desc: 'An array of parameters needed to resolve URL to this object'
      bool :resolved, label: 'Resolved', desc: 'True if the association is resolved'
    end
  end

  return unless %i[object object_list].include?(action.input.layout)

  clean = proc do |raw|
    if raw.is_a?(String)
      raw.strip.split(',')
    elsif raw.is_a?(Array)
      raw
    end
  end

  desc = <<~END
    A list of names of associated resources separated by a comma.
    Nested associations are declared with '__' between resource names.
    For example, 'user,node' will resolve the two associations.
    To resolve further associations of node, use e.g. 'user,node__location',
    to go even deeper, use e.g. 'user,node__location__environment'.
  END

  action.meta(:global) do
    input do
      custom :includes, label: 'Included associations',
                        desc:, &clean
    end
  end

  action.send(:include, Action::InstanceMethods)
end

Instance Method Details

#[](name) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/haveapi/model_adapters/active_record.rb', line 198

def [](name)
  param = @context.action.output[name]
  v = @object.send(param.db_name)

  if v.is_a?(::ActiveRecord::Base)
    resourcify(param, v)
  else
    v
  end
end

#has_param?(name) ⇒ Boolean

Returns:



193
194
195
196
# File 'lib/haveapi/model_adapters/active_record.rb', line 193

def has_param?(name)
  param = @context.action.output[name]
  param && @object.respond_to?(param.db_name)
end

#metaObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/haveapi/model_adapters/active_record.rb', line 209

def meta
  res = @context.action.resource

  params = if @context.action.name.demodulize == 'Index' \
     && !@context.action.resolve \
     && res.const_defined?(:Show)
             res::Show.resolve_path_params(@object)

           else
             @context.action.resolve_path_params(@object)
           end

  {
    path_params: params.is_a?(Array) ? params : [params],
    resolved: true
  }
end