class CrudMuffins::Rails::Adapter::Collection

Chainable, immutable PORO that encapsulates an ActiveRecord::Collection, but can delegate `as_json` to map the records through the provided adapter.

Attributes

adapter[R]
collection[R]

Public Class Methods

new(collection, adapter) click to toggle source
# File lib/crud-muffins-rails.rb, line 25
def initialize(collection, adapter)
  @collection = collection
  @adapter = adapter
end

Public Instance Methods

+(other) click to toggle source
# File lib/crud-muffins-rails.rb, line 50
def +(other)
  @collection = collection + other.collection
  self
end
as_json(*) click to toggle source
# File lib/crud-muffins-rails.rb, line 55
def as_json(*)
  json = {
    data: collection.map { |obj| adapter.new(obj) },
  }

  if collection.respond_to?(:previous_page)
    json[:pagination] = {
      previous: collection&.previous_page,
      next: collection&.next_page
    }
  end

  json
end
limit(*args) click to toggle source
# File lib/crud-muffins-rails.rb, line 45
def limit(*args)
  @collection = collection.limit(*args)
  self
end
order(*args) click to toggle source
# File lib/crud-muffins-rails.rb, line 35
def order(*args)
  @collection = collection.order(*args)
  self
end
paginate(*args) click to toggle source
# File lib/crud-muffins-rails.rb, line 40
def paginate(*args)
  @collection = collection.paginate(*args)
  self
end
where(*args) click to toggle source
# File lib/crud-muffins-rails.rb, line 30
def where(*args)
  @collection = collection.where(*args)
  self
end