Class: JsonapiCompliable::Adapters::ActiveRecord
- Defined in:
- lib/jsonapi_compliable/adapters/active_record.rb
Overview
Instance Method Summary collapse
-
#associate(parent, child, association_name, association_type) ⇒ Object
When a has_many relationship, we need to avoid Activerecord implicitly firing a query.
-
#average(scope, attr) ⇒ Float
The average of the scope.
-
#count(scope, attr) ⇒ Numeric
The count of the scope.
-
#create(model_class, create_params) ⇒ Object
The model instance just created.
-
#destroy(model_class, id) ⇒ Object
The model instance just destroyed.
-
#filter(scope, attribute, value) ⇒ Object
The scope.
-
#maximum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#minimum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#order(scope, attribute, direction) ⇒ Object
The scope.
-
#paginate(scope, current_page, per_page) ⇒ Object
The scope.
-
#resolve(scope) ⇒ Object
Resolve the scope.
-
#sideloading_module ⇒ Object
This module gets mixed in to Sideload classes This is where you define methods like has_many, belongs_to etc that wrap the lower-level Sideload#allow_sideload.
-
#sum(scope, attr) ⇒ Numeric
The sum of the scope.
-
#transaction(model_class) ⇒ Object
Run this write request within an ActiveRecord transaction.
-
#update(model_class, update_params) ⇒ Object
The model instance just created.
Instance Method Details
#associate(parent, child, association_name, association_type) ⇒ Object
When a has_many relationship, we need to avoid Activerecord implicitly firing a query. Otherwise, simple assignment will do
74 75 76 77 78 79 80 81 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 74 def associate(parent, child, association_name, association_type) if association_type == :has_many parent.association(association_name).loaded! parent.association(association_name).add_to_target(child, :skip_callbacks) else child.send("#{association_name}=", parent) end end |
#average(scope, attr) ⇒ Float
Returns the average of the scope
32 33 34 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 32 def average(scope, attr) scope.average(attr).to_f end |
#count(scope, attr) ⇒ Numeric
Returns the count of the scope
23 24 25 26 27 28 29 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 23 def count(scope, attr) if attr.to_sym == :total scope.distinct.count else scope.distinct.count(attr) end end |
#create(model_class, create_params) ⇒ Object
Returns the model instance just created
84 85 86 87 88 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 84 def create(model_class, create_params) instance = model_class.new(create_params) instance.save instance end |
#destroy(model_class, id) ⇒ Object
Returns the model instance just destroyed
98 99 100 101 102 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 98 def destroy(model_class, id) instance = model_class.find(id) instance.destroy instance end |
#filter(scope, attribute, value) ⇒ Object
Returns the scope
8 9 10 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 8 def filter(scope, attribute, value) scope.where(attribute => value) end |
#maximum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope
42 43 44 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 42 def maximum(scope, attr) scope.maximum(attr) end |
#minimum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope
47 48 49 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 47 def minimum(scope, attr) scope.minimum(attr) end |
#order(scope, attribute, direction) ⇒ Object
Returns the scope
13 14 15 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 13 def order(scope, attribute, direction) scope.order(attribute => direction) end |
#paginate(scope, current_page, per_page) ⇒ Object
Returns the scope
18 19 20 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 18 def paginate(scope, current_page, per_page) scope.page(current_page).per(per_page) end |
#resolve(scope) ⇒ Object
Resolve the scope. This is where you'd actually fire SQL, actually make an HTTP call, etc.
52 53 54 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 52 def resolve(scope) scope.to_a end |
#sideloading_module ⇒ Object
This module gets mixed in to Sideload classes This is where you define methods like has_many, belongs_to etc that wrap the lower-level Sideload#allow_sideload
67 68 69 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 67 def sideloading_module JsonapiCompliable::Adapters::ActiveRecordSideloading end |
#sum(scope, attr) ⇒ Numeric
Returns the sum of the scope
37 38 39 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 37 def sum(scope, attr) scope.sum(attr) end |
#transaction(model_class) ⇒ Object
Run this write request within an ActiveRecord transaction
60 61 62 63 64 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 60 def transaction(model_class) model_class.transaction do yield end end |
#update(model_class, update_params) ⇒ Object
Returns the model instance just created
91 92 93 94 95 |
# File 'lib/jsonapi_compliable/adapters/active_record.rb', line 91 def update(model_class, update_params) instance = model_class.find(update_params.delete(:id)) instance.update_attributes(update_params) instance end |