Class: DuckRecord::Reflection::EmbedsAssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/duck_record/reflection.rb

Overview

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

Direct Known Subclasses

EmbedsManyReflection, EmbedsOneReflection

Instance Attribute Summary collapse

Attributes inherited from MacroReflection

#duck_record, #name, #options, #plural_name, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #check_validity!, #class_name

Constructor Details

#initialize(name, scope, options, duck_record) ⇒ EmbedsAssociationReflection

Returns a new instance of EmbedsAssociationReflection.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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

Instance Attribute Details

#parent_reflectionObject

Reflection



229
230
231
# File 'lib/duck_record/reflection.rb', line 229

def parent_reflection
  @parent_reflection
end

Instance Method Details

#add_as_source(seed) ⇒ Object



289
290
291
# File 'lib/duck_record/reflection.rb', line 289

def add_as_source(seed)
  seed
end

#association_classObject

Raises:

  • (NotImplementedError)


287
# File 'lib/duck_record/reflection.rb', line 287

def association_class; raise NotImplementedError; end

#collection?Boolean

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.

Returns:

  • (Boolean)


267
268
269
# File 'lib/duck_record/reflection.rb', line 267

def collection?
  false
end

#compute_class(name) ⇒ Object



225
226
227
# File 'lib/duck_record/reflection.rb', line 225

def compute_class(name)
  duck_record.send(:compute_type, name)
end

#constructable?Boolean

:nodoc:

Returns:

  • (Boolean)


247
248
249
# File 'lib/duck_record/reflection.rb', line 247

def constructable? # :nodoc:
  @constructable
end

#has_one?Boolean

Returns true if self is a has_one reflection.

Returns:

  • (Boolean)


285
# File 'lib/duck_record/reflection.rb', line 285

def has_one?; false; end

#klassObject

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.



221
222
223
# File 'lib/duck_record/reflection.rb', line 221

def klass
  @klass ||= compute_class(class_name)
end

#macroObject

Returns the macro type.

has_many :clients returns :has_many

Raises:

  • (NotImplementedError)


262
# File 'lib/duck_record/reflection.rb', line 262

def macro; raise NotImplementedError; end

#nested?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/duck_record/reflection.rb', line 255

def nested?
  false
end

#source_reflectionObject



251
252
253
# File 'lib/duck_record/reflection.rb', line 251

def source_reflection
  self
end

#validate?Boolean

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

Returns:

  • (Boolean)


280
281
282
# File 'lib/duck_record/reflection.rb', line 280

def validate?
  !options[:validate].nil? ? options[:validate] : collection?
end