Class: Formed::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- AbstractReflection
- MacroReflection
- Formed::Reflection::AssociationReflection
- Defined in:
- lib/formed/reflection.rb
Overview
Holds all the metadata about an association as it was specified in the Active Record class.
Direct Known Subclasses
BelongsToReflection, HasAndBelongsToManyReflection, HasManyReflection, HasOneReflection
Instance Attribute Summary collapse
-
#foreign_type ⇒ Object
readonly
Returns the value of attribute foreign_type.
-
#parent_reflection ⇒ Object
Reflection.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from MacroReflection
#active_form, #name, #options, #plural_name, #scope
Instance Method Summary collapse
- #add_as_polymorphic_through(reflection, seed) ⇒ Object
- #add_as_source(seed) ⇒ Object
- #add_as_through(seed) ⇒ Object
- #association_class ⇒ Object
- #association_foreign_key ⇒ Object
- #association_primary_key(klass = nil) ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. - #check_eager_loadable! ⇒ Object
- #check_validity! ⇒ Object
-
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner.
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
-
#compute_class(name) ⇒ Object
:nodoc:.
- #extensions ⇒ Object
- #foreign_key ⇒ Object
- #has_inverse? ⇒ Boolean
-
#has_one? ⇒ Boolean
Returns
true
ifself
is ahas_one
reflection. - #has_scope? ⇒ Boolean
-
#initialize(name, scope, options, active_form) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #join_table ⇒ Object
-
#macro ⇒ Object
Returns the macro type.
- #nested? ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #polymorphic_inverse_of(associated_class) ⇒ Object
- #source_reflection ⇒ Object
- #through_reflection ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
Methods inherited from MacroReflection
#==, #autosave=, #klass, #scope_for
Methods inherited from AbstractReflection
#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #inverse_of, #scopes, #strict_loading?, #strict_loading_violation_message, #table_name, #through_reflection?
Constructor Details
#initialize(name, scope, options, active_form) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
349 350 351 352 353 354 355 |
# File 'lib/formed/reflection.rb', line 349 def initialize(name, scope, , active_form) super @type = -([:foreign_type].to_s || "#{[:as]}_type") if [:as] @foreign_type = -([:foreign_type].to_s || "#{name}_type") if [:polymorphic] ensure_option_not_given_as_class!(:class_name) end |
Instance Attribute Details
#foreign_type ⇒ Object (readonly)
Returns the value of attribute foreign_type.
346 347 348 |
# File 'lib/formed/reflection.rb', line 346 def foreign_type @foreign_type end |
#parent_reflection ⇒ Object
Reflection
347 348 349 |
# File 'lib/formed/reflection.rb', line 347 def parent_reflection @parent_reflection end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
346 347 348 |
# File 'lib/formed/reflection.rb', line 346 def type @type end |
Instance Method Details
#add_as_polymorphic_through(reflection, seed) ⇒ Object
473 474 475 |
# File 'lib/formed/reflection.rb', line 473 def add_as_polymorphic_through(reflection, seed) seed + [PolymorphicReflection.new(self, reflection)] end |
#add_as_source(seed) ⇒ Object
469 470 471 |
# File 'lib/formed/reflection.rb', line 469 def add_as_source(seed) seed end |
#add_as_through(seed) ⇒ Object
477 478 479 |
# File 'lib/formed/reflection.rb', line 477 def add_as_through(seed) seed + [self] end |
#association_class ⇒ Object
461 462 463 |
# File 'lib/formed/reflection.rb', line 461 def association_class raise NotImplementedError end |
#association_foreign_key ⇒ Object
365 366 367 |
# File 'lib/formed/reflection.rb', line 365 def association_foreign_key @association_foreign_key ||= -([:association_foreign_key].to_s || class_name.foreign_key) end |
#association_primary_key(klass = nil) ⇒ Object
369 370 371 |
# File 'lib/formed/reflection.rb', line 369 def association_primary_key(klass = nil) primary_key(klass || self.klass) end |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
452 453 454 |
# File 'lib/formed/reflection.rb', line 452 def belongs_to? false end |
#check_eager_loadable! ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/formed/reflection.rb', line 377 def check_eager_loadable! return unless scope return if scope.arity.zero? raise ArgumentError, <<-MSG.squish The association scope '#{name}' is instance dependent (the scope block takes an argument). Eager loading instance dependent scopes is not supported. MSG end |
#check_validity! ⇒ Object
373 374 375 |
# File 'lib/formed/reflection.rb', line 373 def check_validity! check_validity_of_inverse! end |
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
399 400 401 |
# File 'lib/formed/reflection.rb', line 399 def collect_join_chain [self] 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.
434 435 436 |
# File 'lib/formed/reflection.rb', line 434 def collection? false end |
#compute_class(name) ⇒ Object
:nodoc:
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/formed/reflection.rb', line 326 def compute_class(name) raise ArgumentError, "Polymorphic associations do not support computing the class." if polymorphic? msg = <<-MSG.squish Formed couldn't find a valid form for #{name} association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an Formed::Base subclass. MSG begin klass = active_form.send(:compute_type, name) raise ArgumentError, msg unless klass < Formed::Base klass rescue NameError raise NameError, msg end end |
#extensions ⇒ Object
481 482 483 |
# File 'lib/formed/reflection.rb', line 481 def extensions Array([:extend]) end |
#foreign_key ⇒ Object
361 362 363 |
# File 'lib/formed/reflection.rb', line 361 def foreign_key @foreign_key ||= -([:foreign_key].to_s || derive_foreign_key) end |
#has_inverse? ⇒ Boolean
411 412 413 |
# File 'lib/formed/reflection.rb', line 411 def has_inverse? inverse_name end |
#has_one? ⇒ Boolean
Returns true
if self
is a has_one
reflection.
457 458 459 |
# File 'lib/formed/reflection.rb', line 457 def has_one? false end |
#has_scope? ⇒ Boolean
407 408 409 |
# File 'lib/formed/reflection.rb', line 407 def has_scope? scope end |
#join_table ⇒ Object
357 358 359 |
# File 'lib/formed/reflection.rb', line 357 def join_table @join_table ||= -([:join_table].to_s || derive_join_table) end |
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
427 428 429 |
# File 'lib/formed/reflection.rb', line 427 def macro raise NotImplementedError end |
#nested? ⇒ Boolean
403 404 405 |
# File 'lib/formed/reflection.rb', line 403 def nested? false end |
#polymorphic? ⇒ Boolean
465 466 467 |
# File 'lib/formed/reflection.rb', line 465 def polymorphic? [:polymorphic] end |
#polymorphic_inverse_of(associated_class) ⇒ Object
415 416 417 418 419 420 421 422 |
# File 'lib/formed/reflection.rb', line 415 def polymorphic_inverse_of(associated_class) return unless has_inverse? unless (inverse_relationship = associated_class._reflect_on_association([:inverse_of])) raise InverseOfAssociationNotFoundError.new(self, associated_class) end inverse_relationship end |
#source_reflection ⇒ Object
393 394 395 |
# File 'lib/formed/reflection.rb', line 393 def source_reflection self end |
#through_reflection ⇒ Object
389 390 391 |
# File 'lib/formed/reflection.rb', line 389 def through_reflection nil 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
447 448 449 |
# File 'lib/formed/reflection.rb', line 447 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || collection?) end |