Class: DuckRecord::Reflection::AssociationReflection

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.

Defined Under Namespace

Classes: JoinKeys

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

#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, options, active_record)
  super
  @type         = options[:as] && (options[:foreign_type] || "#{options[:as]}_type")
  @foreign_type = options[:foreign_type] || "#{name}_type"
  @constructable = calculate_constructable(macro, options)
  @association_scope_cache = {}
  @scope_lock = Mutex.new

  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

#foreign_typeObject (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_reflectionObject

Reflection



417
418
419
# File 'lib/duck_record/reflection.rb', line 417

def parent_reflection
  @parent_reflection
end

#typeObject (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_keyObject



471
472
473
# File 'lib/duck_record/reflection.rb', line 471

def active_record_primary_key
  @active_record_primary_key ||= options[: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_classObject

Raises:

  • (NotImplementedError)


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

def association_class; raise NotImplementedError; end

#association_foreign_keyObject



458
459
460
# File 'lib/duck_record/reflection.rb', line 458

def association_foreign_key
  @association_foreign_key ||= options[: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)
  options[:primary_key] || primary_key(klass || self.klass)
end

#association_primary_key_typeObject



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.

Returns:

  • (Boolean)


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

def belongs_to?; false; end

#chainObject



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_cacheObject

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_chainObject

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.

Returns:

  • (Boolean)


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

#constraintsObject



376
377
378
# File 'lib/duck_record/reflection.rb', line 376

def constraints
  chain.map(&:scopes).flatten
end

#constructable?Boolean

:nodoc:

Returns:

  • (Boolean)


446
447
448
# File 'lib/duck_record/reflection.rb', line 446

def constructable? # :nodoc:
  @constructable
end

#extensionsObject



559
560
561
# File 'lib/duck_record/reflection.rb', line 559

def extensions
  Array(options[:extend])
end

#foreign_keyObject



454
455
456
# File 'lib/duck_record/reflection.rb', line 454

def foreign_key
  @foreign_key ||= options[: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.

Returns:

  • (Boolean)


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

def has_one?; false; end

#has_scope?Boolean

Returns:

  • (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_keysObject



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_tableObject



450
451
452
# File 'lib/duck_record/reflection.rb', line 450

def join_table
  @join_table ||= options[:join_table] || derive_join_table
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.



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

#macroObject

Returns the macro type.

has_many :clients returns :has_many

Raises:

  • (NotImplementedError)


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

def macro; raise NotImplementedError; end

#nested?Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/duck_record/reflection.rb', line 514

def nested?
  false
end

#primary_key_typeObject



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_nameObject



337
338
339
# File 'lib/duck_record/reflection.rb', line 337

def quoted_table_name
  klass.quoted_table_name
end

#scope_chainObject



357
358
359
# File 'lib/duck_record/reflection.rb', line 357

def scope_chain
  chain.map(&:scopes)
end

#scopesObject

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_reflectionObject



498
499
500
# File 'lib/duck_record/reflection.rb', line 498

def source_reflection
  self
end

#table_nameObject



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_many association

Returns:

  • (Boolean)


543
544
545
# File 'lib/duck_record/reflection.rb', line 543

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?)
end