Class: DuckRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- AbstractReflection
- MacroReflection
- DuckRecord::Reflection::AssociationReflection
- 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
Defined Under Namespace
Classes: JoinKeys
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
#duck_record, #name, #options, #plural_name, #scope
Instance Method Summary collapse
- #active_record_primary_key ⇒ Object
- #add_as_source(seed) ⇒ Object
- #alias_candidate(name) ⇒ Object
- #association_class ⇒ Object
- #association_foreign_key ⇒ Object
-
#association_primary_key(klass = nil) ⇒ Object
klass option is necessary to support loading polymorphic associations.
- #association_primary_key_type ⇒ Object
- #association_scope_cache(conn, owner) ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
trueifselfis abelongs_toreflection. - #chain ⇒ Object
- #check_preloadable! ⇒ Object (also: #check_eager_loadable!)
- #check_validity! ⇒ Object
-
#clear_association_scope_cache ⇒ Object
This is for clearing cache on the reflection.
-
#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
- #constraints ⇒ Object
-
#constructable? ⇒ Boolean
:nodoc:.
- #extensions ⇒ Object
- #foreign_key ⇒ Object
- #get_join_keys(association_klass) ⇒ Object
-
#has_one? ⇒ Boolean
Returns
trueifselfis ahas_onereflection. - #has_scope? ⇒ Boolean
-
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
-
#join_id_for(owner) ⇒ Object
:nodoc:.
- #join_keys ⇒ Object
-
#join_scopes(table, predicate_builder) ⇒ Object
:nodoc:.
- #join_table ⇒ Object
-
#klass ⇒ Object
Returns the target association’s class.
-
#klass_join_scope(table, predicate_builder) ⇒ Object
:nodoc:.
-
#macro ⇒ Object
Returns the macro type.
- #nested? ⇒ Boolean
- #primary_key_type ⇒ Object
- #quoted_table_name ⇒ Object
- #scope_chain ⇒ Object
-
#scopes ⇒ Object
Returns a list of scopes that should be applied for this Reflection object when querying the database.
- #source_reflection ⇒ Object
- #table_name ⇒ 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
#build_association, #class_name
Constructor Details
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/duck_record/reflection.rb', line 419 def initialize(name, scope, , active_record) super @type = [:as] && ([:foreign_type] || "#{[:as]}_type") @foreign_type = [:foreign_type] || "#{name}_type" @constructable = calculate_constructable(macro, ) @association_scope_cache = {} @scope_lock = Mutex.new 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
#foreign_type ⇒ Object (readonly)
Returns the value of attribute foreign_type.
416 417 418 |
# File 'lib/duck_record/reflection.rb', line 416 def foreign_type @foreign_type end |
#parent_reflection ⇒ Object
Reflection
417 418 419 |
# File 'lib/duck_record/reflection.rb', line 417 def parent_reflection @parent_reflection end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
416 417 418 |
# File 'lib/duck_record/reflection.rb', line 416 def type @type end |
Instance Method Details
#active_record_primary_key ⇒ Object
471 472 473 |
# File 'lib/duck_record/reflection.rb', line 471 def active_record_primary_key @active_record_primary_key ||= [:primary_key] || primary_key(active_record) end |
#add_as_source(seed) ⇒ Object
555 556 557 |
# File 'lib/duck_record/reflection.rb', line 555 def add_as_source(seed) seed end |
#alias_candidate(name) ⇒ Object
380 381 382 |
# File 'lib/duck_record/reflection.rb', line 380 def alias_candidate(name) "#{plural_name}_#{name}" end |
#association_class ⇒ Object
553 |
# File 'lib/duck_record/reflection.rb', line 553 def association_class; raise NotImplementedError; end |
#association_foreign_key ⇒ Object
458 459 460 |
# File 'lib/duck_record/reflection.rb', line 458 def association_foreign_key @association_foreign_key ||= [:association_foreign_key] || class_name.foreign_key end |
#association_primary_key(klass = nil) ⇒ Object
klass option is necessary to support loading polymorphic associations
463 464 465 |
# File 'lib/duck_record/reflection.rb', line 463 def association_primary_key(klass = nil) [:primary_key] || primary_key(klass || self.klass) end |
#association_primary_key_type ⇒ Object
467 468 469 |
# File 'lib/duck_record/reflection.rb', line 467 def association_primary_key_type klass.type_for_attribute(association_primary_key.to_s) end |
#association_scope_cache(conn, owner) ⇒ Object
439 440 441 442 443 444 |
# File 'lib/duck_record/reflection.rb', line 439 def association_scope_cache(conn, owner) key = conn.prepared_statements @association_scope_cache[key] ||= @scope_lock.synchronize { @association_scope_cache[key] ||= yield } end |
#belongs_to? ⇒ Boolean
Returns true if self is a belongs_to reflection.
548 |
# File 'lib/duck_record/reflection.rb', line 548 def belongs_to?; false; end |
#chain ⇒ Object
384 385 386 |
# File 'lib/duck_record/reflection.rb', line 384 def chain collect_join_chain end |
#check_preloadable! ⇒ Object Also known as: check_eager_loadable!
481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/duck_record/reflection.rb', line 481 def check_preloadable! return unless scope if scope.arity > 0 raise ArgumentError, <<-MSG.squish The association scope '#{name}' is instance dependent (the scope block takes an argument). Preloading instance dependent scopes is not supported. MSG end end |
#check_validity! ⇒ Object
475 476 477 478 479 |
# File 'lib/duck_record/reflection.rb', line 475 def check_validity! unless klass < ActiveRecord::Base raise ArgumentError, "#{klass} must be inherited from ActiveRecord::Base." end end |
#clear_association_scope_cache ⇒ Object
This is for clearing cache on the reflection. Useful for tests that need to compare SQL queries on associations.
510 511 512 |
# File 'lib/duck_record/reflection.rb', line 510 def clear_association_scope_cache # :nodoc: @association_scope_cache.clear end |
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
504 505 506 |
# File 'lib/duck_record/reflection.rb', line 504 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.
530 531 532 |
# File 'lib/duck_record/reflection.rb', line 530 def collection? false end |
#compute_class(name) ⇒ Object
408 409 410 |
# File 'lib/duck_record/reflection.rb', line 408 def compute_class(name) active_record.send(:compute_type, name) end |
#constraints ⇒ Object
376 377 378 |
# File 'lib/duck_record/reflection.rb', line 376 def constraints chain.map(&:scopes).flatten end |
#constructable? ⇒ Boolean
:nodoc:
446 447 448 |
# File 'lib/duck_record/reflection.rb', line 446 def constructable? # :nodoc: @constructable end |
#extensions ⇒ Object
559 560 561 |
# File 'lib/duck_record/reflection.rb', line 559 def extensions Array([:extend]) end |
#foreign_key ⇒ Object
454 455 456 |
# File 'lib/duck_record/reflection.rb', line 454 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key.freeze end |
#get_join_keys(association_klass) ⇒ Object
388 389 390 |
# File 'lib/duck_record/reflection.rb', line 388 def get_join_keys(association_klass) JoinKeys.new(join_pk(association_klass), join_fk) end |
#has_one? ⇒ Boolean
Returns true if self is a has_one reflection.
551 |
# File 'lib/duck_record/reflection.rb', line 551 def has_one?; false; end |
#has_scope? ⇒ Boolean
518 519 520 |
# File 'lib/duck_record/reflection.rb', line 518 def has_scope? scope end |
#join_id_for(owner) ⇒ Object
:nodoc:
494 495 496 |
# File 'lib/duck_record/reflection.rb', line 494 def join_id_for(owner) # :nodoc: owner[active_record_primary_key] end |
#join_keys ⇒ Object
347 348 349 |
# File 'lib/duck_record/reflection.rb', line 347 def join_keys get_join_keys klass end |
#join_scopes(table, predicate_builder) ⇒ Object
:nodoc:
362 363 364 365 366 367 368 369 |
# File 'lib/duck_record/reflection.rb', line 362 def join_scopes(table, predicate_builder) # :nodoc: if scope [ActiveRecord::Relation.create(klass, table, predicate_builder) .instance_exec(&scope)] else [] end end |
#join_table ⇒ Object
450 451 452 |
# File 'lib/duck_record/reflection.rb', line 450 def join_table @join_table ||= [:join_table] || derive_join_table 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.
404 405 406 |
# File 'lib/duck_record/reflection.rb', line 404 def klass @klass ||= compute_class(class_name) end |
#klass_join_scope(table, predicate_builder) ⇒ Object
:nodoc:
371 372 373 374 |
# File 'lib/duck_record/reflection.rb', line 371 def klass_join_scope(table, predicate_builder) # :nodoc: relation = ActiveRecord::Relation.create(klass, table, predicate_builder) klass.scope_for_association(relation) end |
#macro ⇒ Object
Returns the macro type.
has_many :clients returns :has_many
525 |
# File 'lib/duck_record/reflection.rb', line 525 def macro; raise NotImplementedError; end |
#nested? ⇒ Boolean
514 515 516 |
# File 'lib/duck_record/reflection.rb', line 514 def nested? false end |
#primary_key_type ⇒ Object
341 342 343 |
# File 'lib/duck_record/reflection.rb', line 341 def primary_key_type klass.type_for_attribute(klass.primary_key) end |
#quoted_table_name ⇒ Object
337 338 339 |
# File 'lib/duck_record/reflection.rb', line 337 def quoted_table_name klass.quoted_table_name end |
#scope_chain ⇒ Object
357 358 359 |
# File 'lib/duck_record/reflection.rb', line 357 def scope_chain chain.map(&:scopes) end |
#scopes ⇒ Object
Returns a list of scopes that should be applied for this Reflection object when querying the database.
353 354 355 |
# File 'lib/duck_record/reflection.rb', line 353 def scopes scope ? [scope] : [] end |
#source_reflection ⇒ Object
498 499 500 |
# File 'lib/duck_record/reflection.rb', line 498 def source_reflection self end |
#table_name ⇒ Object
412 413 414 |
# File 'lib/duck_record/reflection.rb', line 412 def table_name klass.table_name 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_manyassociation
543 544 545 |
# File 'lib/duck_record/reflection.rb', line 543 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || collection?) end |