Class: Gecko::Helpers::CollectionProxy
- Inherits:
-
Object
- Object
- Gecko::Helpers::CollectionProxy
- Includes:
- Enumerable
- Defined in:
- lib/gecko/helpers/association_helper.rb
Overview
Provides a convenient wrapper for a collection of child records. Exposes both an Enumerable interface as well as the ability to create new child records
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#build(attributes) ⇒ Gecko::Record::Base
Build a new child object inside the collection.
-
#embed_records? ⇒ Boolean
private
Should this collection of records be serialized inside it’s parent object.
-
#initialize(parent:, association_name:, class_name:, target:, embedded:) ⇒ Hash
constructor
private
Setup the child collection proxy.
Constructor Details
#initialize(parent:, association_name:, class_name:, target:, embedded:) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup the child collection proxy
127 128 129 130 131 132 133 |
# File 'lib/gecko/helpers/association_helper.rb', line 127 def initialize(parent:, association_name:, class_name:, target:, embedded:) @parent = parent @target = target = @class_name = class_name @association_name = association_name end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
120 121 122 |
# File 'lib/gecko/helpers/association_helper.rb', line 120 def association_name @association_name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
120 121 122 |
# File 'lib/gecko/helpers/association_helper.rb', line 120 def parent @parent end |
Instance Method Details
#build(attributes) ⇒ Gecko::Record::Base
Build a new child object inside the collection
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/gecko/helpers/association_helper.rb', line 146 def build(attributes) if @parent.persisted? parent_foreign_key = @parent.class.demodulized_name.foreign_key.to_sym attributes[parent_foreign_key] = @parent.id end record = client.adapter_for(@class_name).build(attributes) @target << record record end |
#embed_records? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Should this collection of records be serialized inside it’s parent object
162 163 164 |
# File 'lib/gecko/helpers/association_helper.rb', line 162 def !! end |