class Gecko::Helpers::CollectionProxy

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

Attributes

association_name[R]
parent[R]

Public Class Methods

new(parent:, association_name:, class_name:, target:, embedded:) click to toggle source

Setup the child collection proxy

@return [Hash]

@api private

# File lib/gecko/helpers/association_helper.rb, line 129
def initialize(parent:, association_name:, class_name:, target:, embedded:)
  @parent           = parent
  @target           = target
  @embedded         = embedded
  @class_name       = class_name
  @association_name = association_name
end

Public Instance Methods

build(attributes) click to toggle source

Build a new child object inside the collection

@example

item = order.order_line_items.build(variant_id: 1234, quantiy: 12.5, price: 13.45)
order.order_line_items.include?(item) #=> true

@param [#to_hash] attributes for the child record

@return [Gecko::Record::Base]

@api public

# File lib/gecko/helpers/association_helper.rb, line 148
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?() click to toggle source

Should this collection of records be serialized inside it's parent object

@return [Boolean]

@api private

# File lib/gecko/helpers/association_helper.rb, line 164
def embed_records?
  !!@embedded
end

Private Instance Methods

client() click to toggle source

Access the Gecko Client object

@return [Gecko::Client]

@api private

# File lib/gecko/helpers/association_helper.rb, line 175
def client
  @parent.instance_variable_get(:@client)
end