class DuckRecord::Reflection::EmbedsAssociationReflection

Holds all the metadata about an association as it was specified in the Active Record class.

Attributes

parent_reflection[RW]

Public Class Methods

new(name, scope, options, duck_record) click to toggle source
# File lib/duck_record/reflection.rb, line 231
      def initialize(name, scope, options, duck_record)
        super
        @constructable = calculate_constructable(macro, options)

        if options[:class_name] && options[:class_name].class == Class
          ActiveSupport::Deprecation.warn(<<-MSG.squish)
            Passing a class to the `class_name` is deprecated and will raise
            an ArgumentError in Rails 5.2. It eagerloads more classes than
            necessary and potentially creates circular dependencies.

            Please pass the class name as a string:
            `#{macro} :#{name}, class_name: '#{options[:class_name]}'`
          MSG
        end
      end

Public Instance Methods

add_as_source(seed) click to toggle source
# File lib/duck_record/reflection.rb, line 289
def add_as_source(seed)
  seed
end
association_class() click to toggle source
# File lib/duck_record/reflection.rb, line 287
def association_class; raise NotImplementedError; end
collection?() click to toggle source

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

# File lib/duck_record/reflection.rb, line 267
def collection?
  false
end
compute_class(name) click to toggle source
# File lib/duck_record/reflection.rb, line 225
def compute_class(name)
  duck_record.send(:compute_type, name)
end
has_one?() click to toggle source

Returns true if self is a has_one reflection.

# File lib/duck_record/reflection.rb, line 285
def has_one?; false; end
klass() click to toggle source

Returns the target association's class.

class Author < ActiveRecord::Base
  has_many :books
end

Author.reflect_on_association(:books).klass
# => Book

Note: Do not call klass.new or klass.create to instantiate a new association object. Use build_association or create_association instead. This allows plugins to hook into association object creation.

# File lib/duck_record/reflection.rb, line 221
def klass
  @klass ||= compute_class(class_name)
end
macro() click to toggle source

Returns the macro type.

has_many :clients returns :has_many

# File lib/duck_record/reflection.rb, line 262
def macro; raise NotImplementedError; end
nested?() click to toggle source
# File lib/duck_record/reflection.rb, line 255
def nested?
  false
end
source_reflection() click to toggle source
# File lib/duck_record/reflection.rb, line 251
def source_reflection
  self
end
validate?() click to toggle source

Returns whether or not the association should be validated as part of the parent's validation.

Unless you explicitly disable validation with validate: false, validation will take place when:

  • you explicitly enable validation; validate: true

  • you use autosave; autosave: true

  • the association is a has_many association

# File lib/duck_record/reflection.rb, line 280
def validate?
  !options[:validate].nil? ? options[:validate] : collection?
end

Protected Instance Methods

actual_source_reflection() click to toggle source
# File lib/duck_record/reflection.rb, line 295
def actual_source_reflection # FIXME: this is a horrible name
  self
end

Private Instance Methods

calculate_constructable(macro, options) click to toggle source
# File lib/duck_record/reflection.rb, line 301
def calculate_constructable(macro, options)
  true
end
derive_class_name() click to toggle source
# File lib/duck_record/reflection.rb, line 305
def derive_class_name
  class_name = name.to_s
  class_name = class_name.singularize if collection?
  class_name.camelize
end