123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Reflection

Do not use. This module is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Classes:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: activerecord/lib/active_record/reflection.rb

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

DSL Calls

included

[ GitHub ]


12
13
14
15
16
17
# File 'activerecord/lib/active_record/reflection.rb', line 12

included do
  class_attribute :_reflections, instance_writer: false, default: {}.freeze
  class_attribute :aggregate_reflections, instance_writer: false, default: {}.freeze
  class_attribute :automatic_scope_inversing, instance_writer: false, default: false
  class_attribute :automatically_invert_plural_associations, instance_writer: false, default: false
end

Class Method Details

.add_aggregate_reflection(ar, name, reflection)

[ GitHub ]

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

def add_aggregate_reflection(ar, name, reflection)
  name = name.to_sym
  ar.aggregate_reflections = ar.aggregate_reflections.merge(name => reflection).freeze

  # Like associations, pass the new aggregation down to subclasses that
  # hold their own copy, unless a subclass defines that name itself.
  ar.descendants.each do |subclass|
    next if subclass.aggregate_reflections.equal?(subclass.superclass.aggregate_reflections)

    existing = subclass.aggregate_reflections[name]
    next if existing && existing.active_record < ar

    subclass.aggregate_reflections = subclass.aggregate_reflections.merge(name => reflection).freeze
  end
end

.add_reflection(ar, name, reflection)

[ GitHub ]

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

def add_reflection(ar, name, reflection)
  ar.clear_reflections_cache
  name = name.to_sym
  ar._reflections = ar._reflections.except(name).merge!(name => reflection).freeze

  # Subclasses that defined an association of their own hold a copy of
  # the reflections, and every subclass may have cached them, so pass
  # the new one down unless a subclass defines that name itself.
  ar.descendants.each do |subclass|
    subclass.clear_reflections_cache
    next if subclass._reflections.equal?(subclass.superclass._reflections)

    existing = subclass._reflections[name]
    next if existing && existing.active_record < ar

    subclass._reflections = subclass._reflections.except(name).merge!(name => reflection).freeze
  end
end

.create(macro, name, scope, options, ar)

[ GitHub ]

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

def create(macro, name, scope, options, ar)
  reflection = reflection_class_for(macro).new(name, scope, options, ar)
  options[:through] ? ThroughReflection.new(reflection) : reflection
end

.reflection_class_for(macro) (private)

[ GitHub ]

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

def reflection_class_for(macro)
  case macro
  when :composed_of
    AggregateReflection
  when :has_many
    HasManyReflection
  when :has_one
    HasOneReflection
  when :belongs_to
    BelongsToReflection
  else
    raise "Unsupported Macro: #{macro}"
  end
end