Module: JSONAPI::Realizer::Adapter::ActiveRecord

Defined in:
lib/jsonapi/realizer/adapter/active_record.rb

Instance Method Summary collapse

Instance Method Details

#filtering(scope, filters) ⇒ Object



15
16
17
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 15

def filtering(scope, filters)
  scope.where(filters.slice(*scope.column_names))
end

#find_many(scope) ⇒ Object



7
8
9
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 7

def find_many(scope)
  scope.all
end

#find_one(scope, id) ⇒ Object



11
12
13
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 11

def find_one(scope, id)
  scope.find(id)
end

#include_relationships(scope, includes) ⇒ Object



46
47
48
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 46

def include_relationships(scope, includes)
  scope.eager_load(*includes.map(&method(:arel_chain)))
end

#paginate(scope, per, offset) ⇒ Object



34
35
36
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 34

def paginate(scope, per, offset)
  scope.page(offset).per(per)
end

#sorting(scope, sorts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 19

def sorting(scope, sorts)
  scope.order(
    *sorts
      .map do |(keychain, direction)|
        [keychain, direction == "-" ? :DESC : :ASC]
      end
      .map do |(keychain, direction)|
        [keychain.map(&:inspect).join("."), direction]
      end
      .map do |pair|
        Arel.sql(pair.join(" "))
      end
  )
end

#write_attributes(model, attributes) ⇒ Object



38
39
40
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 38

def write_attributes(model, attributes)
  model.assign_attributes(attributes)
end

#write_relationships(model, relationships) ⇒ Object



42
43
44
# File 'lib/jsonapi/realizer/adapter/active_record.rb', line 42

def write_relationships(model, relationships)
  model.assign_attributes(relationships)
end