Class: FlexCommerceApi::JsonApiClientExtension::HasManyAssociationProxy
- Inherits:
-
Object
- Object
- FlexCommerceApi::JsonApiClientExtension::HasManyAssociationProxy
- Defined in:
- lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb
Instance Method Summary collapse
- #create(attrs) ⇒ Object
-
#find(pk) ⇒ Object
Finds an instance by primary key.
-
#initialize(real_instance, container_instance, association_name, options) ⇒ HasManyAssociationProxy
constructor
A new instance of HasManyAssociationProxy.
-
#new(attrs) ⇒ Object
Creates a new associated item - if the option “inverse_of” is specified, then automatically create a relationship back to the container with the name specified in “inverse_of”.
- #to_a ⇒ Object
Constructor Details
#initialize(real_instance, container_instance, association_name, options) ⇒ HasManyAssociationProxy
Returns a new instance of HasManyAssociationProxy.
6 7 8 9 10 11 |
# File 'lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb', line 6 def initialize(real_instance, container_instance, association_name, ) self.real_instance = real_instance self.container_instance = container_instance self.association_name = association_name self. = end |
Instance Method Details
#create(attrs) ⇒ Object
37 38 39 40 41 |
# File 'lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb', line 37 def create(attrs) new(attrs).tap do |instance| instance.save end end |
#find(pk) ⇒ Object
Finds an instance by primary key
19 20 21 22 |
# File 'lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb', line 19 def find(pk) method = primary_key real_instance.detect {|i| i.send(method) == pk} end |
#new(attrs) ⇒ Object
Creates a new associated item - if the option “inverse_of” is specified, then automatically create a relationship back to the container with the name specified in “inverse_of”
27 28 29 30 31 32 33 34 35 |
# File 'lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb', line 27 def new(attrs) my_attrs = attrs.dup if .key?(:inverse_of) my_attrs[:relationships] ||= {} # Dont modify the original relationship if it was present below my_attrs[:relationships] = my_attrs[:relationships].merge([:inverse_of] => container_instance) end association.association_class.new(my_attrs) end |
#to_a ⇒ Object
43 44 45 |
# File 'lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb', line 43 def to_a real_instance end |