Class: DuckRecord::Reflection::EmbedsAssociationReflection
- Inherits:
-
MacroReflection
- Object
- AbstractReflection
- MacroReflection
- DuckRecord::Reflection::EmbedsAssociationReflection
- 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
Instance Attribute Summary collapse
-
#parent_reflection ⇒ Object
Reflection.
Attributes inherited from MacroReflection
#duck_record, #name, #options, #plural_name, #scope
Instance Method Summary collapse
- #add_as_source(seed) ⇒ Object
- #association_class ⇒ Object
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
- #compute_class(name) ⇒ Object
-
#constructable? ⇒ Boolean
:nodoc:.
-
#has_one? ⇒ Boolean
Returns
true
ifself
is ahas_one
reflection. -
#initialize(name, scope, options, duck_record) ⇒ EmbedsAssociationReflection
constructor
A new instance of EmbedsAssociationReflection.
-
#klass ⇒ Object
Returns the target association’s class.
-
#macro ⇒ Object
Returns the macro type.
- #nested? ⇒ Boolean
- #source_reflection ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
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, , duck_record) super @constructable = calculate_constructable(macro, ) if [:class_name] && [: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: '#{[:class_name]}'` MSG end end |
Instance Attribute Details
#parent_reflection ⇒ Object
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_class ⇒ Object
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.
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:
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.
285 |
# File 'lib/duck_record/reflection.rb', line 285 def has_one?; false; end |
#klass ⇒ Object
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 |
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
262 |
# File 'lib/duck_record/reflection.rb', line 262 def macro; raise NotImplementedError; end |
#nested? ⇒ Boolean
255 256 257 |
# File 'lib/duck_record/reflection.rb', line 255 def nested? false end |
#source_reflection ⇒ Object
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
280 281 282 |
# File 'lib/duck_record/reflection.rb', line 280 def validate? ![:validate].nil? ? [:validate] : collection? end |