# File lib/duck_record/reflection.rb, line 341 def primary_key_type klass.type_for_attribute(klass.primary_key) end
class DuckRecord::Reflection::AssociationReflection
Holds all the metadata about an association as it was specified in the Active Record class.
Attributes
Public Class Methods
DuckRecord::Reflection::MacroReflection::new
# 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
Public Instance Methods
# 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
# File lib/duck_record/reflection.rb, line 555 def add_as_source(seed) seed end
# File lib/duck_record/reflection.rb, line 380 def alias_candidate(name) "#{plural_name}_#{name}" end
# File lib/duck_record/reflection.rb, line 553 def association_class; raise NotImplementedError; end
# File lib/duck_record/reflection.rb, line 458 def association_foreign_key @association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key end
klass option is necessary to support loading polymorphic associations
# File lib/duck_record/reflection.rb, line 463 def association_primary_key(klass = nil) options[:primary_key] || primary_key(klass || self.klass) end
# File lib/duck_record/reflection.rb, line 467 def association_primary_key_type klass.type_for_attribute(association_primary_key.to_s) end
# 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
Returns true
if self
is a belongs_to
reflection.
# File lib/duck_record/reflection.rb, line 548 def belongs_to?; false; end
# File lib/duck_record/reflection.rb, line 384 def chain collect_join_chain end
# 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
# 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
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
# File lib/duck_record/reflection.rb, line 504 def collect_join_chain [self] end
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.
# File lib/duck_record/reflection.rb, line 530 def collection? false end
# File lib/duck_record/reflection.rb, line 408 def compute_class(name) active_record.send(:compute_type, name) end
# File lib/duck_record/reflection.rb, line 376 def constraints chain.map(&:scopes).flatten end
# File lib/duck_record/reflection.rb, line 559 def extensions Array(options[:extend]) end
# File lib/duck_record/reflection.rb, line 454 def foreign_key @foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze end
# File lib/duck_record/reflection.rb, line 388 def get_join_keys(association_klass) JoinKeys.new(join_pk(association_klass), join_fk) end
Returns true
if self
is a has_one
reflection.
# File lib/duck_record/reflection.rb, line 551 def has_one?; false; end
# File lib/duck_record/reflection.rb, line 518 def has_scope? scope end
# File lib/duck_record/reflection.rb, line 347 def join_keys get_join_keys klass end
# File lib/duck_record/reflection.rb, line 450 def join_table @join_table ||= options[:join_table] || derive_join_table end
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.
# File lib/duck_record/reflection.rb, line 404 def klass @klass ||= compute_class(class_name) end
Returns the macro type.
has_many :clients
returns :has_many
# File lib/duck_record/reflection.rb, line 525 def macro; raise NotImplementedError; end
# File lib/duck_record/reflection.rb, line 514 def nested? false end
# File lib/duck_record/reflection.rb, line 337 def quoted_table_name klass.quoted_table_name end
# File lib/duck_record/reflection.rb, line 357 def scope_chain chain.map(&:scopes) end
Returns a list of scopes that should be applied for this Reflection
object when querying the database.
# File lib/duck_record/reflection.rb, line 353 def scopes scope ? [scope] : [] end
# File lib/duck_record/reflection.rb, line 498 def source_reflection self end
# File lib/duck_record/reflection.rb, line 412 def table_name klass.table_name end
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
# File lib/duck_record/reflection.rb, line 543 def validate? !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?) end
Protected Instance Methods
# File lib/duck_record/reflection.rb, line 565 def actual_source_reflection # FIXME: this is a horrible name self end
Private Instance Methods
# File lib/duck_record/reflection.rb, line 579 def calculate_constructable(_macro, _options) false end
# File lib/duck_record/reflection.rb, line 583 def derive_class_name class_name = name.to_s class_name = class_name.singularize if collection? class_name.camelize end
# File lib/duck_record/reflection.rb, line 589 def derive_foreign_key if options[:as] "#{options[:as]}_id" else "#{name}_id" end end
# File lib/duck_record/reflection.rb, line 575 def join_fk active_record_primary_key end
# File lib/duck_record/reflection.rb, line 571 def join_pk(_) foreign_key end
# File lib/duck_record/reflection.rb, line 597 def primary_key(klass) klass.primary_key || raise(ActiveRecord::UnknownPrimaryKey.new(klass)) end