123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Reflection::MacroReflection

Overview

Class Method Summary

Instance Attribute Summary

AbstractReflection - Inherited

Instance Method Summary

AbstractReflection - Inherited

#alias_candidate,
#build_association

Returns a new, unsaved instance of the associated class.

#build_scope, #chain, #check_validity_of_inverse!,
#class_name

Returns the class name for the macro.

#constraints, #counter_cache_column, #inverse_of,
#inverse_which_updates_counter_cache

We need to avoid the following situation:

#join_scope, #join_scopes, #klass_join_scope,
#scopes

Returns a list of scopes that should be applied for this ::ActiveRecord::Reflection object when querying the database.

#strict_loading_violation_message, #table_name,
#actual_source_reflection

FIXME: this is a horrible name.

#ensure_option_not_given_as_class!, #predicate_builder, #primary_key

Constructor Details

.new(name, scope, options, active_record) ⇒ MacroReflection

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 376

def initialize(name, scope, options, active_record)
  super()
  @name          = name
  @scope         = scope
  @options       = options
  @active_record = active_record
  @klass         = options[:anonymous_class]
  @plural_name   = active_record.pluralize_table_names ?
                      name.to_s.pluralize : name.to_s
  validate_reflection!
end

Instance Attribute Details

#active_record (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 372

attr_reader :active_record

#autosave=(autosave) (writeonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 388

def autosave=(autosave)
  @options[:autosave] = autosave
  parent_reflection = self.parent_reflection
  if parent_reflection
    parent_reflection.autosave = autosave
  end
end

#name (readonly)

Returns the name of the macro.

composed_of :balance, class_name: 'Money' returns :balance has_many :clients returns :clients

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 362

attr_reader :name

#options (readonly)

Returns the hash of options used for the macro.

composed_of :balance, class_name: 'Money' returns { class_name: "Money" } has_many :clients returns {}

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 370

attr_reader :options

#plural_name (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 374

attr_reader :plural_name # :nodoc:

#scope (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 364

attr_reader :scope

Instance Method Details

#==(other_aggregation)

Returns true if self and other_aggregation have the same #name attribute, #active_record attribute, and other_aggregation has an options hash assigned to it.

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 421

def ==(other_aggregation)
  super ||
    other_aggregation.kind_of?(self.class) &&
    name == other_aggregation.name &&
    !other_aggregation.options.nil? &&
    active_record == other_aggregation.active_record
end

#compute_class(name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 415

def compute_class(name)
  name.constantize
end

#derive_class_name (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 434

def derive_class_name
  name.to_s.camelize
end

#klass

Returns the class for the macro.

composed_of :balance, class_name: 'Money' returns the Money class has_many :clients returns the Client class

class Company < ActiveRecord::Base
  has_many :clients
end

Company.reflect_on_association(:clients).klass
# => Client

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.

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 411

def klass
  @klass ||= compute_class(class_name)
end

#scope_for(relation, owner = nil)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 429

def scope_for(relation, owner = nil)
  relation.instance_exec(owner, &scope) || relation
end

#validate_reflection! (private)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 438

def validate_reflection!
  return unless options[:foreign_key].is_a?(Array)

  message = <<~MSG.squish
    Passing #{options[:foreign_key]} array to :foreign_key option
    on the #{active_record}##{name} association is not supported.
    Use the query_constraints: #{options[:foreign_key]} option instead to represent a composite foreign key.
  MSG
  raise ArgumentError, message
end