Class: JsonApi::Associations::ToMany
- Inherits:
-
JsonApi::Association
- Object
- JsonApi::Association
- JsonApi::Associations::ToMany
- Defined in:
- lib/json_api_ruby/associations/to_many.rb
Instance Attribute Summary
Attributes inherited from JsonApi::Association
#explicit_resource_class, #included, #name, #parent, #parent_model, #resources
Instance Method Summary collapse
-
#build_resources(options) ⇒ Object
Build the related resources.
- #cardinality ⇒ Object
- #data(options = {}) ⇒ Object
- #resource_objects ⇒ Object
Methods inherited from JsonApi::Association
#included?, #initialize, #relationship_links, #to_hash
Constructor Details
This class inherits a constructor from JsonApi::Association
Instance Method Details
#build_resources(options) ⇒ Object
Build the related resources
12 13 14 15 16 17 18 19 |
# File 'lib/json_api_ruby/associations/to_many.rb', line 12 def build_resources() return unless included? parent_model.send(name).each do |resource_model| resource_class = JsonApi::Resources::Discovery.resource_for_name(resource_model, .merge(parent_resource: parent, resource_class: explicit_resource_class)) @resources << resource_class.new(resource_model, .merge({include: included.next})) end end |
#cardinality ⇒ Object
25 26 27 |
# File 'lib/json_api_ruby/associations/to_many.rb', line 25 def cardinality :many end |
#data(options = {}) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/json_api_ruby/associations/to_many.rb', line 4 def data(={}) data = resource_objects.map do |object| object.identifier_hash end {'data' => data} end |
#resource_objects ⇒ Object
21 22 23 |
# File 'lib/json_api_ruby/associations/to_many.rb', line 21 def resource_objects @resources end |