Class: JsonapiActions::EagerLoader
- Inherits:
-
Object
- Object
- JsonapiActions::EagerLoader
- Defined in:
- lib/jsonapi_actions/eager_loader.rb
Instance Attribute Summary collapse
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
- #eager_load ⇒ ActiveRecord::Relation
-
#initialize(records, serializer, includes) ⇒ EagerLoader
constructor
A new instance of EagerLoader.
Constructor Details
#initialize(records, serializer, includes) ⇒ EagerLoader
8 9 10 11 12 |
# File 'lib/jsonapi_actions/eager_loader.rb', line 8 def initialize(records, serializer, includes) @records = records @serializer = serializer @includes = includes end |
Instance Attribute Details
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
3 4 5 |
# File 'lib/jsonapi_actions/eager_loader.rb', line 3 def includes @includes end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
3 4 5 |
# File 'lib/jsonapi_actions/eager_loader.rb', line 3 def records @records end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
3 4 5 |
# File 'lib/jsonapi_actions/eager_loader.rb', line 3 def serializer @serializer end |
Instance Method Details
#eager_load ⇒ ActiveRecord::Relation
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jsonapi_actions/eager_loader.rb', line 15 def eager_load serializer.relationships_to_serialize&.each do |rel| next if @records.eager_load_values.include?(rel[0]) @records = @records.eager_load(rel[0]) end includes.each do |include| rel = path_to_relationship(include.to_s.split('.')) next if @records.eager_load_values.include?(rel) @records = records.eager_load(rel) end @records end |