Module: APIResourceMultigettable::HelperMethods

Defined in:
app/api/concerns/api_resource_multigettable.rb

Instance Method Summary collapse

Instance Method Details

#multiget(resource, find_by: :id, max: 10) ⇒ Object

Get multiple resources from a resource URL by specifing ids split by ','

Params:

resource

ActiveRecord::Base or ActiveRecord::Relation resource collection to find data from

find_by

Symbol the attribute that is used to find data

max

Integer maxium count of returning results


67
68
69
70
71
72
73
74
75
76
# File 'app/api/concerns/api_resource_multigettable.rb', line 67

def multiget(resource, find_by: :id, max: 10)
  ids = params[find_by].split(',')
  ids = ids[0..(max - 1)]

  if ids.count > 1
    resource.where(find_by => ids)
  else
    resource.find_by(find_by => ids[0])
  end
end

#multiget?(find_by: :id) ⇒ Boolean

Is the a multiget request?

Returns:


79
80
81
# File 'app/api/concerns/api_resource_multigettable.rb', line 79

def multiget?(find_by: :id)
  params[find_by].include?(',')
end