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



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/haveapi/model_adapters/active_record.rb', line 254

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|
    values = if raw.is_a?(String)
               raw.strip.split(',')
             elsif raw.is_a?(Array)
               raw
             else
               raise HaveAPI::ValidationError, 'includes must be a string or array'
             end

    values.map do |value|
      unless value.is_a?(String)
        raise HaveAPI::ValidationError, 'includes must contain only strings'
      end

      value.strip
    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



306
307
308
309
310
311
312
313
314
315
# File 'lib/haveapi/model_adapters/active_record.rb', line 306

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:



301
302
303
304
# File 'lib/haveapi/model_adapters/active_record.rb', line 301

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

#metaObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/haveapi/model_adapters/active_record.rb', line 317

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