class NestedRecord::CollectionProxy

Public Class Methods

__nested_record_setup__() click to toggle source
# File lib/nested_record/collection_proxy.rb, line 16
def __nested_record_setup__
  @setup
end
new(owner) click to toggle source
# File lib/nested_record/collection_proxy.rb, line 21
def initialize(owner)
  @owner = owner
end
subclass_for(setup) click to toggle source
# File lib/nested_record/collection_proxy.rb, line 6
def subclass_for(setup)
  Class.new(self) do
    methods = setup.collection_class.public_instance_methods
    methods -= NestedRecord::Collection.public_instance_methods
    methods -= NestedRecord::CollectionProxy.public_instance_methods(false)
    def_delegators :__collection__, *methods unless methods.empty?
    @setup = setup
  end
end

Public Instance Methods

__build__(attributes) click to toggle source
# File lib/nested_record/collection_proxy.rb, line 45
def __build__(attributes)
  __collection__.build(attributes)
end
__collection__() click to toggle source
# File lib/nested_record/collection_proxy.rb, line 58
def __collection__
  @owner.read_attribute(self.class.__nested_record_setup__.name)
end
build(attributes = {}) { |record| ... } click to toggle source
# File lib/nested_record/collection_proxy.rb, line 38
def build(attributes = {})
  __collection__.build(attributes) do |record|
    ensure_primary! record
    yield record if block_given?
  end
end
find_or_initialize_by(attributes) { |record| ... } click to toggle source
# File lib/nested_record/collection_proxy.rb, line 49
def find_or_initialize_by(attributes)
  __collection__.find_or_initialize_by(attributes) do |record|
    ensure_primary! record
    yield record if block_given?
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/nested_record/collection_proxy.rb, line 25
def method_missing(method_name, *args, &block)
  collection = __collection__
  if collection.respond_to? method_name
    collection.public_send(method_name, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, _) click to toggle source
Calls superclass method
# File lib/nested_record/collection_proxy.rb, line 34
def respond_to_missing?(method_name, _)
  super || __collection__.respond_to?(method_name)
end

Private Instance Methods

ensure_primary!(record) click to toggle source
# File lib/nested_record/collection_proxy.rb, line 64
def ensure_primary!(record)
  check = self.class.__nested_record_setup__.primary_check(record.read_attribute('type'))
  check&.perform!(__collection__, record)
end