2004-11-23 20:04:44 -05:00
|
|
|
module ActiveRecord
|
2010-06-16 13:45:04 -04:00
|
|
|
# = Active Record Reflection
|
2004-11-23 20:04:44 -05:00
|
|
|
module Reflection # :nodoc:
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-11-19 12:29:33 -05:00
|
|
|
included do
|
2012-05-25 10:58:16 -04:00
|
|
|
class_attribute :reflections
|
2013-06-12 19:58:22 -04:00
|
|
|
class_attribute :aggregate_reflections
|
2010-11-19 12:29:33 -05:00
|
|
|
self.reflections = {}
|
2013-06-12 19:58:22 -04:00
|
|
|
self.aggregate_reflections = {}
|
2010-11-19 12:29:33 -05:00
|
|
|
end
|
|
|
|
|
2013-07-22 21:28:54 -04:00
|
|
|
def self.create(macro, name, scope, options, ar)
|
|
|
|
case macro
|
2013-07-23 01:27:25 -04:00
|
|
|
when :has_many, :belongs_to, :has_one
|
2013-07-22 21:28:54 -04:00
|
|
|
klass = options[:through] ? ThroughReflection : AssociationReflection
|
|
|
|
when :composed_of
|
|
|
|
klass = AggregateReflection
|
|
|
|
end
|
|
|
|
|
2013-07-23 16:12:25 -04:00
|
|
|
klass.new(macro, name, scope, options, ar)
|
2013-07-23 16:06:57 -04:00
|
|
|
end
|
2013-07-22 21:28:54 -04:00
|
|
|
|
2013-07-23 16:06:57 -04:00
|
|
|
def self.add_reflection(ar, name, reflection)
|
2013-09-11 20:24:59 -04:00
|
|
|
ar.reflections = ar.reflections.merge(name => reflection)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_aggregate_reflection(ar, name, reflection)
|
|
|
|
ar.aggregate_reflections = ar.aggregate_reflections.merge(name => reflection)
|
2013-07-22 21:28:54 -04:00
|
|
|
end
|
|
|
|
|
2013-05-12 00:03:43 -04:00
|
|
|
# \Reflection enables to interrogate Active Record classes and objects
|
2010-08-14 01:13:00 -04:00
|
|
|
# about their associations and aggregations. This information can,
|
2010-07-30 14:43:13 -04:00
|
|
|
# for example, be used in a form builder that takes an Active Record object
|
|
|
|
# and creates input fields for all of the attributes depending on their type
|
|
|
|
# and displays the associations to other objects.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2012-07-27 17:21:29 -04:00
|
|
|
# MacroReflection class has info for AggregateReflection and AssociationReflection
|
|
|
|
# classes.
|
2004-11-23 20:04:44 -05:00
|
|
|
module ClassMethods
|
2012-07-27 17:21:29 -04:00
|
|
|
# Returns an array of AggregateReflection objects for all the aggregations in the class.
|
|
|
|
def reflect_on_all_aggregations
|
2013-06-12 19:58:22 -04:00
|
|
|
aggregate_reflections.values
|
2012-07-27 17:21:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the AggregateReflection object for the named +aggregation+ (use the symbol).
|
|
|
|
#
|
|
|
|
# Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
|
|
|
|
#
|
|
|
|
def reflect_on_aggregation(aggregation)
|
2013-06-12 19:58:22 -04:00
|
|
|
aggregate_reflections[aggregation]
|
2012-07-27 17:21:29 -04:00
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns an array of AssociationReflection objects for all the
|
|
|
|
# associations in the class. If you only want to reflect on a certain
|
|
|
|
# association type, pass in the symbol (<tt>:has_many</tt>, <tt>:has_one</tt>,
|
2010-07-24 00:45:53 -04:00
|
|
|
# <tt>:belongs_to</tt>) as the first parameter.
|
2010-06-16 13:45:04 -04:00
|
|
|
#
|
2007-01-05 15:22:41 -05:00
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Account.reflect_on_all_associations # returns an array of all associations
|
|
|
|
# Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
|
|
|
|
#
|
2005-12-27 00:41:23 -05:00
|
|
|
def reflect_on_all_associations(macro = nil)
|
2013-06-12 19:58:22 -04:00
|
|
|
association_reflections = reflections.values
|
2005-12-27 00:41:23 -05:00
|
|
|
macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2010-07-24 00:45:53 -04:00
|
|
|
# Returns the AssociationReflection object for the +association+ (use the symbol).
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2010-07-24 00:45:53 -04:00
|
|
|
# Account.reflect_on_association(:owner) # returns the owner AssociationReflection
|
2006-02-28 15:13:37 -05:00
|
|
|
# Invoice.reflect_on_association(:line_items).macro # returns :has_many
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
def reflect_on_association(association)
|
2013-06-12 19:36:36 -04:00
|
|
|
reflections[association]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
# Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
|
|
|
|
def reflect_on_all_autosave_associations
|
|
|
|
reflections.values.select { |reflection| reflection.options[:autosave] }
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2013-03-30 08:12:53 -04:00
|
|
|
# Base class for AggregateReflection and AssociationReflection. Objects of
|
2012-07-27 17:21:29 -04:00
|
|
|
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
|
2013-03-30 08:12:53 -04:00
|
|
|
#
|
|
|
|
# MacroReflection
|
|
|
|
# AggregateReflection
|
|
|
|
# AssociationReflection
|
|
|
|
# ThroughReflection
|
2004-11-23 20:04:44 -05:00
|
|
|
class MacroReflection
|
2010-07-24 00:45:53 -04:00
|
|
|
# Returns the name of the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>:balance</tt>
|
2010-07-30 14:43:13 -04:00
|
|
|
# <tt>has_many :clients</tt> returns <tt>:clients</tt>
|
2010-08-02 10:51:08 -04:00
|
|
|
attr_reader :name
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the macro type.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>:composed_of</tt>
|
2010-07-30 14:43:13 -04:00
|
|
|
# <tt>has_many :clients</tt> returns <tt>:has_many</tt>
|
2010-08-02 10:51:08 -04:00
|
|
|
attr_reader :macro
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2012-06-22 06:40:13 -04:00
|
|
|
attr_reader :scope
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the hash of options used for the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>{ class_name: "Money" }</tt>
|
2013-05-12 00:03:43 -04:00
|
|
|
# <tt>has_many :clients</tt> returns <tt>{}</tt>
|
2010-08-02 10:51:08 -04:00
|
|
|
attr_reader :options
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2011-06-30 14:24:39 -04:00
|
|
|
attr_reader :active_record
|
|
|
|
|
|
|
|
attr_reader :plural_name # :nodoc:
|
|
|
|
|
2012-06-22 06:40:13 -04:00
|
|
|
def initialize(macro, name, scope, options, active_record)
|
2011-06-30 14:24:39 -04:00
|
|
|
@macro = macro
|
|
|
|
@name = name
|
2012-06-22 06:40:13 -04:00
|
|
|
@scope = scope
|
2011-06-30 14:24:39 -04:00
|
|
|
@options = options
|
|
|
|
@active_record = active_record
|
2013-09-03 17:16:29 -04:00
|
|
|
@klass = options[:class]
|
2011-06-30 14:24:39 -04:00
|
|
|
@plural_name = active_record.pluralize_table_names ?
|
|
|
|
name.to_s.pluralize : name.to_s
|
|
|
|
end
|
|
|
|
|
2013-06-13 13:59:11 -04:00
|
|
|
def autosave=(autosave)
|
|
|
|
@automatic_inverse_of = false
|
|
|
|
@options[:autosave] = autosave
|
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the class for the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns the Money class
|
2010-07-30 14:43:13 -04:00
|
|
|
# <tt>has_many :clients</tt> returns the Client class
|
2007-01-14 07:08:44 -05:00
|
|
|
def klass
|
|
|
|
@klass ||= class_name.constantize
|
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the class name for the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
|
2010-07-30 14:43:13 -04:00
|
|
|
# <tt>has_many :clients</tt> returns <tt>'Client'</tt>
|
2005-12-02 23:29:55 -05:00
|
|
|
def class_name
|
2011-11-04 07:16:55 -04:00
|
|
|
@class_name ||= (options[:class_name] || derive_class_name).to_s
|
2005-12-02 23:29:55 -05:00
|
|
|
end
|
|
|
|
|
2008-04-04 23:52:58 -04:00
|
|
|
# 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.
|
2004-11-23 20:04:44 -05:00
|
|
|
def ==(other_aggregation)
|
2011-07-01 17:37:31 -04:00
|
|
|
super ||
|
|
|
|
other_aggregation.kind_of?(self.class) &&
|
|
|
|
name == other_aggregation.name &&
|
|
|
|
other_aggregation.options &&
|
|
|
|
active_record == other_aggregation.active_record
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-01-14 07:08:44 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
def derive_class_name
|
|
|
|
name.to_s.camelize
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2012-07-27 17:21:29 -04:00
|
|
|
|
|
|
|
# Holds all the meta-data about an aggregation as it was specified in the
|
|
|
|
# Active Record class.
|
|
|
|
class AggregateReflection < MacroReflection #:nodoc:
|
|
|
|
def mapping
|
|
|
|
mapping = options[:mapping] || [name, name]
|
|
|
|
mapping.first.is_a?(Array) ? mapping : [mapping]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Holds all the meta-data about an association as it was specified in the
|
2010-06-16 13:45:04 -04:00
|
|
|
# Active Record class.
|
2004-11-23 20:04:44 -05:00
|
|
|
class AssociationReflection < MacroReflection #:nodoc:
|
2010-07-30 14:43:13 -04:00
|
|
|
# Returns the target association's class.
|
2008-09-06 13:43:14 -04:00
|
|
|
#
|
|
|
|
# class Author < ActiveRecord::Base
|
|
|
|
# has_many :books
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Author.reflect_on_association(:books).klass
|
|
|
|
# # => Book
|
|
|
|
#
|
2010-07-30 14:43:13 -04:00
|
|
|
# <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
|
2008-09-06 13:43:14 -04:00
|
|
|
# a new association object. Use +build_association+ or +create_association+
|
|
|
|
# instead. This allows plugins to hook into association object creation.
|
2004-11-23 20:04:44 -05:00
|
|
|
def klass
|
2005-12-02 23:29:55 -05:00
|
|
|
@klass ||= active_record.send(:compute_type, class_name)
|
2005-04-18 14:49:34 -04:00
|
|
|
end
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2013-06-13 19:03:40 -04:00
|
|
|
attr_reader :type, :foreign_type
|
|
|
|
|
2013-07-23 01:27:25 -04:00
|
|
|
def initialize(macro, name, scope, options, active_record)
|
2010-03-14 18:46:03 -04:00
|
|
|
super
|
2013-10-02 18:59:13 -04:00
|
|
|
@collection = :has_many == macro
|
2013-03-01 16:49:33 -05:00
|
|
|
@automatic_inverse_of = nil
|
2013-06-13 19:03:40 -04:00
|
|
|
@type = options[:as] && "#{options[:as]}_type"
|
|
|
|
@foreign_type = options[:foreign_type] || "#{name}_type"
|
2013-10-02 17:52:59 -04:00
|
|
|
@constructable = calculate_constructable(macro, options)
|
2010-03-14 18:46:03 -04:00
|
|
|
end
|
|
|
|
|
2012-04-05 15:21:48 -04:00
|
|
|
# Returns a new, unsaved instance of the associated class. +attributes+ will
|
2008-09-06 13:43:14 -04:00
|
|
|
# be passed to the class's constructor.
|
2012-09-12 12:35:05 -04:00
|
|
|
def build_association(attributes, &block)
|
|
|
|
klass.new(attributes, &block)
|
2008-09-06 13:43:14 -04:00
|
|
|
end
|
|
|
|
|
2013-10-02 17:52:59 -04:00
|
|
|
def constructable? # :nodoc:
|
|
|
|
@constructable
|
|
|
|
end
|
|
|
|
|
2005-04-18 14:49:34 -04:00
|
|
|
def table_name
|
2013-06-13 19:06:41 -04:00
|
|
|
klass.table_name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2008-01-05 09:58:28 -05:00
|
|
|
def quoted_table_name
|
2013-06-13 19:07:11 -04:00
|
|
|
klass.quoted_table_name
|
2008-01-05 09:58:28 -05:00
|
|
|
end
|
|
|
|
|
2012-06-21 15:47:12 -04:00
|
|
|
def join_table
|
|
|
|
@join_table ||= options[:join_table] || derive_join_table
|
|
|
|
end
|
|
|
|
|
2010-12-31 15:00:24 -05:00
|
|
|
def foreign_key
|
|
|
|
@foreign_key ||= options[:foreign_key] || derive_foreign_key
|
2005-12-02 23:29:55 -05:00
|
|
|
end
|
2007-01-14 07:08:44 -05:00
|
|
|
|
2010-08-02 10:51:08 -04:00
|
|
|
def primary_key_column
|
2013-06-13 18:57:48 -04:00
|
|
|
klass.columns_hash[klass.primary_key]
|
2010-08-02 10:51:08 -04:00
|
|
|
end
|
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
def association_foreign_key
|
2010-12-15 18:27:15 -05:00
|
|
|
@association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
|
2005-12-02 23:29:55 -05:00
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2011-09-26 10:41:31 -04:00
|
|
|
# klass option is necessary to support loading polymorphic associations
|
2011-09-29 12:59:40 -04:00
|
|
|
def association_primary_key(klass = nil)
|
2011-10-05 15:20:04 -04:00
|
|
|
options[:primary_key] || primary_key(klass || self.klass)
|
2010-10-19 09:14:06 -04:00
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2010-10-19 09:14:06 -04:00
|
|
|
def active_record_primary_key
|
2011-10-05 15:20:04 -04:00
|
|
|
@active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
|
2010-10-19 09:14:06 -04:00
|
|
|
end
|
2005-12-02 23:29:55 -05:00
|
|
|
|
2006-03-04 14:42:41 -05:00
|
|
|
def counter_cache_column
|
|
|
|
if options[:counter_cache] == true
|
2008-12-31 04:43:13 -05:00
|
|
|
"#{active_record.name.demodulize.underscore.pluralize}_count"
|
2006-03-04 14:42:41 -05:00
|
|
|
elsif options[:counter_cache]
|
2011-09-06 10:58:07 -04:00
|
|
|
options[:counter_cache].to_s
|
2006-03-04 14:42:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
def check_validity!
|
2009-05-01 11:01:13 -04:00
|
|
|
check_validity_of_inverse!
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_validity_of_inverse!
|
2009-11-29 00:46:09 -05:00
|
|
|
unless options[:polymorphic]
|
|
|
|
if has_inverse? && inverse_of.nil?
|
|
|
|
raise InverseOfAssociationNotFoundError.new(self)
|
|
|
|
end
|
2009-05-01 11:01:13 -04:00
|
|
|
end
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def through_reflection
|
2011-03-10 14:28:26 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_reflection
|
2013-07-23 20:02:04 -04:00
|
|
|
self
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2011-03-10 14:28:26 -05:00
|
|
|
# A chain of reflections from this one back to the owner. For more see the explanation in
|
|
|
|
# ThroughReflection.
|
|
|
|
def chain
|
2010-10-02 13:50:17 -04:00
|
|
|
[self]
|
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2011-12-14 12:25:51 -05:00
|
|
|
def nested?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2012-07-06 13:20:20 -04:00
|
|
|
# An array of arrays of scopes. Each item in the outside array corresponds to a reflection
|
|
|
|
# in the #chain.
|
|
|
|
def scope_chain
|
|
|
|
scope ? [[scope]] : [[]]
|
2010-10-18 19:27:40 -04:00
|
|
|
end
|
2008-10-04 11:53:13 -04:00
|
|
|
|
2011-03-06 18:38:10 -05:00
|
|
|
alias :source_macro :macro
|
|
|
|
|
2009-05-01 11:01:13 -04:00
|
|
|
def has_inverse?
|
2013-06-13 13:49:29 -04:00
|
|
|
inverse_name
|
2009-05-01 11:01:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inverse_of
|
2013-06-13 13:49:29 -04:00
|
|
|
return unless inverse_name
|
|
|
|
|
|
|
|
@inverse_of ||= klass.reflect_on_association inverse_name
|
2009-05-01 11:01:13 -04:00
|
|
|
end
|
|
|
|
|
2009-11-29 00:46:09 -05:00
|
|
|
def polymorphic_inverse_of(associated_class)
|
|
|
|
if has_inverse?
|
2009-12-28 08:13:33 -05:00
|
|
|
if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
|
|
|
|
inverse_relationship
|
|
|
|
else
|
|
|
|
raise InverseOfAssociationNotFoundError.new(self, associated_class)
|
|
|
|
end
|
2009-11-29 00:46:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-31 08:03:04 -05:00
|
|
|
# Returns whether or not this association reflection is for a collection
|
2010-07-30 14:43:13 -04:00
|
|
|
# association. Returns +true+ if the +macro+ is either +has_many+ or
|
2009-12-31 08:03:04 -05:00
|
|
|
# +has_and_belongs_to_many+, +false+ otherwise.
|
2010-01-08 15:44:06 -05:00
|
|
|
def collection?
|
|
|
|
@collection
|
2009-12-31 08:03:04 -05:00
|
|
|
end
|
|
|
|
|
2010-01-03 15:45:08 -05:00
|
|
|
# Returns whether or not the association should be validated as part of
|
|
|
|
# the parent's validation.
|
|
|
|
#
|
2010-06-11 06:15:34 -04:00
|
|
|
# Unless you explicitly disable validation with
|
2012-10-23 07:02:34 -04:00
|
|
|
# <tt>validate: false</tt>, validation will take place when:
|
2010-01-03 15:45:08 -05:00
|
|
|
#
|
2012-10-23 07:02:34 -04:00
|
|
|
# * you explicitly enable validation; <tt>validate: true</tt>
|
|
|
|
# * you use autosave; <tt>autosave: true</tt>
|
2010-01-03 15:45:08 -05:00
|
|
|
# * the association is a +has_many+ association
|
|
|
|
def validate?
|
|
|
|
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
|
|
|
|
end
|
|
|
|
|
2010-08-02 10:51:08 -04:00
|
|
|
# Returns +true+ if +self+ is a +belongs_to+ reflection.
|
|
|
|
def belongs_to?
|
|
|
|
macro == :belongs_to
|
|
|
|
end
|
|
|
|
|
2011-02-17 18:55:05 -05:00
|
|
|
def association_class
|
2011-01-09 12:13:05 -05:00
|
|
|
case macro
|
|
|
|
when :belongs_to
|
|
|
|
if options[:polymorphic]
|
|
|
|
Associations::BelongsToPolymorphicAssociation
|
|
|
|
else
|
|
|
|
Associations::BelongsToAssociation
|
|
|
|
end
|
|
|
|
when :has_many
|
|
|
|
if options[:through]
|
|
|
|
Associations::HasManyThroughAssociation
|
|
|
|
else
|
|
|
|
Associations::HasManyAssociation
|
|
|
|
end
|
|
|
|
when :has_one
|
|
|
|
if options[:through]
|
|
|
|
Associations::HasOneThroughAssociation
|
|
|
|
else
|
|
|
|
Associations::HasOneAssociation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-12 18:51:30 -04:00
|
|
|
def polymorphic?
|
|
|
|
options.key? :polymorphic
|
|
|
|
end
|
|
|
|
|
2013-03-01 16:49:33 -05:00
|
|
|
VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_one, :belongs_to]
|
|
|
|
INVALID_AUTOMATIC_INVERSE_OPTIONS = [:conditions, :through, :polymorphic, :foreign_key]
|
|
|
|
|
2013-07-23 20:00:15 -04:00
|
|
|
protected
|
|
|
|
|
2013-10-08 22:26:15 -04:00
|
|
|
def actual_source_reflection # FIXME: this is a horrible name
|
|
|
|
self
|
|
|
|
end
|
2013-07-23 20:00:15 -04:00
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
private
|
2013-10-08 22:26:15 -04:00
|
|
|
|
|
|
|
def calculate_constructable(macro, options)
|
|
|
|
case macro
|
|
|
|
when :belongs_to
|
|
|
|
!options[:polymorphic]
|
|
|
|
when :has_one
|
|
|
|
!options[:through]
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2013-10-02 17:52:59 -04:00
|
|
|
end
|
|
|
|
|
2013-06-13 13:49:29 -04:00
|
|
|
# Attempts to find the inverse association name automatically.
|
|
|
|
# If it cannot find a suitable inverse association name, it returns
|
2013-03-01 16:49:33 -05:00
|
|
|
# nil.
|
2013-06-13 13:49:29 -04:00
|
|
|
def inverse_name
|
|
|
|
options.fetch(:inverse_of) do
|
|
|
|
if @automatic_inverse_of == false
|
|
|
|
nil
|
|
|
|
else
|
2013-06-13 14:00:02 -04:00
|
|
|
@automatic_inverse_of ||= automatic_inverse_of
|
2013-06-13 13:49:29 -04:00
|
|
|
end
|
2013-03-01 16:49:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 13:49:29 -04:00
|
|
|
# returns either nil or the inverse association name that it finds.
|
|
|
|
def automatic_inverse_of
|
2013-03-01 16:49:33 -05:00
|
|
|
if can_find_inverse_of_automatically?(self)
|
2013-08-15 00:50:49 -04:00
|
|
|
inverse_name = ActiveSupport::Inflector.underscore(active_record.name).to_sym
|
2013-03-01 16:49:33 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
reflection = klass.reflect_on_association(inverse_name)
|
|
|
|
rescue NameError
|
|
|
|
# Give up: we couldn't compute the klass type so we won't be able
|
|
|
|
# to find any associations either.
|
|
|
|
reflection = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if valid_inverse_reflection?(reflection)
|
2013-06-13 14:01:27 -04:00
|
|
|
return inverse_name
|
2013-03-01 16:49:33 -05:00
|
|
|
end
|
|
|
|
end
|
2013-06-13 14:00:02 -04:00
|
|
|
|
|
|
|
false
|
2013-03-01 16:49:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the inverse reflection that is returned from the
|
2013-08-15 00:50:49 -04:00
|
|
|
# +automatic_inverse_of+ method is a valid reflection. We must
|
2013-03-01 16:49:33 -05:00
|
|
|
# make sure that the reflection's active_record name matches up
|
|
|
|
# with the current reflection's klass name.
|
|
|
|
#
|
|
|
|
# Note: klass will always be valid because when there's a NameError
|
|
|
|
# from calling +klass+, +reflection+ will already be set to false.
|
|
|
|
def valid_inverse_reflection?(reflection)
|
|
|
|
reflection &&
|
2013-06-13 13:23:15 -04:00
|
|
|
klass.name == reflection.active_record.name &&
|
2013-03-01 16:49:33 -05:00
|
|
|
can_find_inverse_of_automatically?(reflection)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks to see if the reflection doesn't have any options that prevent
|
|
|
|
# us from being able to guess the inverse automatically. First, the
|
2013-06-07 23:59:27 -04:00
|
|
|
# <tt>inverse_of</tt> option cannot be set to false. Second, we must
|
|
|
|
# have <tt>has_many</tt>, <tt>has_one</tt>, <tt>belongs_to</tt> associations.
|
|
|
|
# Third, we must not have options such as <tt>:polymorphic</tt> or
|
|
|
|
# <tt>:foreign_key</tt> which prevent us from correctly guessing the
|
|
|
|
# inverse association.
|
2013-03-01 16:49:33 -05:00
|
|
|
#
|
|
|
|
# Anything with a scope can additionally ruin our attempt at finding an
|
|
|
|
# inverse, so we exclude reflections with scopes.
|
|
|
|
def can_find_inverse_of_automatically?(reflection)
|
2013-06-07 23:59:27 -04:00
|
|
|
reflection.options[:inverse_of] != false &&
|
2013-03-01 16:49:33 -05:00
|
|
|
VALID_AUTOMATIC_INVERSE_MACROS.include?(reflection.macro) &&
|
|
|
|
!INVALID_AUTOMATIC_INVERSE_OPTIONS.any? { |opt| reflection.options[opt] } &&
|
|
|
|
!reflection.scope
|
|
|
|
end
|
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
def derive_class_name
|
|
|
|
class_name = name.to_s.camelize
|
2010-01-08 15:44:06 -05:00
|
|
|
class_name = class_name.singularize if collection?
|
2008-10-04 11:53:13 -04:00
|
|
|
class_name
|
|
|
|
end
|
|
|
|
|
2010-12-31 15:00:24 -05:00
|
|
|
def derive_foreign_key
|
2008-10-04 12:41:27 -04:00
|
|
|
if belongs_to?
|
2008-10-04 11:53:13 -04:00
|
|
|
"#{name}_id"
|
|
|
|
elsif options[:as]
|
|
|
|
"#{options[:as]}_id"
|
|
|
|
else
|
|
|
|
active_record.name.foreign_key
|
|
|
|
end
|
|
|
|
end
|
2011-10-05 15:20:04 -04:00
|
|
|
|
2012-06-21 15:47:12 -04:00
|
|
|
def derive_join_table
|
|
|
|
[active_record.table_name, klass.table_name].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').gsub("\0", "_")
|
|
|
|
end
|
|
|
|
|
2011-10-05 15:20:04 -04:00
|
|
|
def primary_key(klass)
|
|
|
|
klass.primary_key || raise(UnknownPrimaryKey.new(klass))
|
|
|
|
end
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
|
|
|
|
2010-06-16 13:45:04 -04:00
|
|
|
# Holds all the meta-data about a :through association as it was specified
|
|
|
|
# in the Active Record class.
|
2008-10-04 11:53:13 -04:00
|
|
|
class ThroughReflection < AssociationReflection #:nodoc:
|
2011-05-16 12:07:21 -04:00
|
|
|
delegate :foreign_key, :foreign_type, :association_foreign_key,
|
|
|
|
:active_record_primary_key, :type, :to => :source_reflection
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2013-06-13 12:46:30 -04:00
|
|
|
def initialize(macro, name, scope, options, active_record)
|
|
|
|
super
|
2013-06-13 13:09:50 -04:00
|
|
|
@source_reflection_name = options[:source]
|
2013-06-13 12:46:30 -04:00
|
|
|
end
|
|
|
|
|
2013-05-09 02:06:03 -04:00
|
|
|
# Returns the source of the through reflection. It checks both a singularized
|
2010-06-16 13:45:04 -04:00
|
|
|
# and pluralized form for <tt>:belongs_to</tt> or <tt>:has_many</tt>.
|
|
|
|
#
|
2008-10-04 11:53:13 -04:00
|
|
|
# class Post < ActiveRecord::Base
|
|
|
|
# has_many :taggings
|
2012-10-23 07:02:34 -04:00
|
|
|
# has_many :tags, through: :taggings
|
2008-10-04 11:53:13 -04:00
|
|
|
# end
|
|
|
|
#
|
2013-04-03 11:53:02 -04:00
|
|
|
# class Tagging < ActiveRecord::Base
|
|
|
|
# belongs_to :post
|
|
|
|
# belongs_to :tag
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# tags_reflection = Post.reflect_on_association(:tags)
|
2013-05-09 02:06:03 -04:00
|
|
|
# tags_reflection.source_reflection
|
2013-04-04 18:45:19 -04:00
|
|
|
# # => <ActiveRecord::Reflection::AssociationReflection: @macro=:belongs_to, @name=:tag, @active_record=Tagging, @plural_name="tags">
|
2013-04-03 11:53:02 -04:00
|
|
|
#
|
2008-10-04 11:53:13 -04:00
|
|
|
def source_reflection
|
2013-06-13 13:09:50 -04:00
|
|
|
through_reflection.klass.reflect_on_association(source_reflection_name)
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
|
|
|
|
2008-05-09 05:38:02 -04:00
|
|
|
# Returns the AssociationReflection object specified in the <tt>:through</tt> option
|
2010-07-30 14:43:13 -04:00
|
|
|
# of a HasManyThrough or HasOneThrough association.
|
2008-05-09 05:38:02 -04:00
|
|
|
#
|
|
|
|
# class Post < ActiveRecord::Base
|
|
|
|
# has_many :taggings
|
2012-10-23 07:02:34 -04:00
|
|
|
# has_many :tags, through: :taggings
|
2008-05-09 05:38:02 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# tags_reflection = Post.reflect_on_association(:tags)
|
2013-05-09 02:06:03 -04:00
|
|
|
# tags_reflection.through_reflection
|
|
|
|
# # => <ActiveRecord::Reflection::AssociationReflection: @macro=:has_many, @name=:taggings, @active_record=Post, @plural_name="taggings">
|
2008-05-09 05:38:02 -04:00
|
|
|
#
|
2006-03-18 17:38:49 -05:00
|
|
|
def through_reflection
|
2013-06-12 20:07:35 -04:00
|
|
|
active_record.reflect_on_association(options[:through])
|
2006-03-18 17:38:49 -05:00
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2011-03-10 14:28:26 -05:00
|
|
|
# Returns an array of reflections which are involved in this association. Each item in the
|
|
|
|
# array corresponds to a table which will be part of the query for this association.
|
2010-10-31 07:21:28 -04:00
|
|
|
#
|
2011-03-10 14:28:26 -05:00
|
|
|
# The chain is built by recursively calling #chain on the source reflection and the through
|
|
|
|
# reflection. The base case for the recursion is a normal association, which just returns
|
|
|
|
# [self] as its #chain.
|
2013-04-04 16:46:04 -04:00
|
|
|
#
|
|
|
|
# class Post < ActiveRecord::Base
|
|
|
|
# has_many :taggings
|
|
|
|
# has_many :tags, through: :taggings
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# tags_reflection = Post.reflect_on_association(:tags)
|
|
|
|
# tags_reflection.chain
|
2013-04-04 18:45:19 -04:00
|
|
|
# # => [<ActiveRecord::Reflection::ThroughReflection: @macro=:has_many, @name=:tags, @options={:through=>:taggings}, @active_record=Post>,
|
|
|
|
# <ActiveRecord::Reflection::AssociationReflection: @macro=:has_many, @name=:taggings, @options={}, @active_record=Post>]
|
2013-04-04 16:46:04 -04:00
|
|
|
#
|
2011-03-10 14:28:26 -05:00
|
|
|
def chain
|
|
|
|
@chain ||= begin
|
2013-07-18 18:05:39 -04:00
|
|
|
a = source_reflection.chain
|
|
|
|
b = through_reflection.chain
|
|
|
|
chain = a + b
|
2011-03-10 19:51:57 -05:00
|
|
|
chain[0] = self # Use self so we don't lose the information from :source_type
|
2010-10-02 13:50:17 -04:00
|
|
|
chain
|
2010-10-01 08:10:41 -04:00
|
|
|
end
|
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2010-10-18 19:27:40 -04:00
|
|
|
# Consider the following example:
|
2010-10-31 07:21:28 -04:00
|
|
|
#
|
2010-10-18 19:27:40 -04:00
|
|
|
# class Person
|
|
|
|
# has_many :articles
|
2012-10-23 07:02:34 -04:00
|
|
|
# has_many :comment_tags, through: :articles
|
2010-10-18 19:27:40 -04:00
|
|
|
# end
|
2010-10-31 07:21:28 -04:00
|
|
|
#
|
2010-10-18 19:27:40 -04:00
|
|
|
# class Article
|
|
|
|
# has_many :comments
|
2012-10-23 07:02:34 -04:00
|
|
|
# has_many :comment_tags, through: :comments, source: :tags
|
2010-10-18 19:27:40 -04:00
|
|
|
# end
|
2010-10-31 07:21:28 -04:00
|
|
|
#
|
2010-10-18 19:27:40 -04:00
|
|
|
# class Comment
|
|
|
|
# has_many :tags
|
|
|
|
# end
|
2010-10-31 07:21:28 -04:00
|
|
|
#
|
2012-07-06 13:20:20 -04:00
|
|
|
# There may be scopes on Person.comment_tags, Article.comment_tags and/or Comment.tags,
|
2011-03-10 14:28:26 -05:00
|
|
|
# but only Comment.tags will be represented in the #chain. So this method creates an array
|
2012-07-06 13:20:20 -04:00
|
|
|
# of scopes corresponding to the chain.
|
|
|
|
def scope_chain
|
|
|
|
@scope_chain ||= begin
|
2013-07-31 21:08:58 -04:00
|
|
|
scope_chain = source_reflection.scope_chain.map(&:dup)
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2012-07-06 13:20:20 -04:00
|
|
|
# Add to it the scope from this reflection (if any)
|
|
|
|
scope_chain.first << scope if scope
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2013-05-09 13:08:44 -04:00
|
|
|
through_scope_chain = through_reflection.scope_chain.map(&:dup)
|
2011-03-05 17:07:30 -05:00
|
|
|
|
|
|
|
if options[:source_type]
|
2012-07-06 13:20:20 -04:00
|
|
|
through_scope_chain.first <<
|
|
|
|
through_reflection.klass.where(foreign_type => options[:source_type])
|
2011-03-05 17:07:30 -05:00
|
|
|
end
|
|
|
|
|
2010-10-18 19:27:40 -04:00
|
|
|
# Recursively fill out the rest of the array from the through reflection
|
2012-07-06 13:20:20 -04:00
|
|
|
scope_chain + through_scope_chain
|
2010-10-18 19:27:40 -04:00
|
|
|
end
|
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2011-03-10 14:28:26 -05:00
|
|
|
# The macro used by the source association
|
2011-03-06 18:38:10 -05:00
|
|
|
def source_macro
|
|
|
|
source_reflection.source_macro
|
|
|
|
end
|
|
|
|
|
2011-12-14 11:53:39 -05:00
|
|
|
# A through association is nested if there would be more than one join table
|
2010-10-15 12:46:09 -04:00
|
|
|
def nested?
|
2013-10-02 18:59:13 -04:00
|
|
|
chain.length > 2
|
2010-10-15 12:46:09 -04:00
|
|
|
end
|
2010-10-31 07:21:28 -04:00
|
|
|
|
2010-10-19 10:24:30 -04:00
|
|
|
# We want to use the klass from this reflection, rather than just delegate straight to
|
|
|
|
# the source_reflection, because the source_reflection may be polymorphic. We still
|
|
|
|
# need to respect the source_reflection's :primary_key option, though.
|
2013-07-23 20:08:04 -04:00
|
|
|
def association_primary_key(klass = nil)
|
2011-09-26 10:41:31 -04:00
|
|
|
# Get the "actual" source reflection if the immediate source reflection has a
|
|
|
|
# source reflection itself
|
2013-07-23 20:00:15 -04:00
|
|
|
actual_source_reflection.options[:primary_key] || primary_key(klass || self.klass)
|
2010-10-19 10:24:30 -04:00
|
|
|
end
|
2006-03-18 17:38:49 -05:00
|
|
|
|
2013-04-04 16:46:04 -04:00
|
|
|
# Gets an array of possible <tt>:through</tt> source reflection names in both singular and plural form.
|
2006-03-19 02:14:48 -05:00
|
|
|
#
|
2013-04-04 16:46:04 -04:00
|
|
|
# class Post < ActiveRecord::Base
|
|
|
|
# has_many :taggings
|
|
|
|
# has_many :tags, through: :taggings
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# tags_reflection = Post.reflect_on_association(:tags)
|
|
|
|
# tags_reflection.source_reflection_names
|
2013-04-04 18:45:19 -04:00
|
|
|
# # => [:tag, :tags]
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2006-03-19 02:14:48 -05:00
|
|
|
def source_reflection_names
|
2013-06-13 13:09:50 -04:00
|
|
|
(options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_reflection_name # :nodoc:
|
|
|
|
return @source_reflection_name if @source_reflection_name
|
|
|
|
|
|
|
|
names = [name.to_s.singularize, name].collect { |n| n.to_sym }.uniq
|
|
|
|
names = names.find_all { |n|
|
|
|
|
through_reflection.klass.reflect_on_association(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if names.length > 1
|
|
|
|
example_options = options.dup
|
|
|
|
example_options[:source] = source_reflection_names.first
|
|
|
|
ActiveSupport::Deprecation.warn <<-eowarn
|
|
|
|
Ambiguous source reflection for through association. Please specify a :source
|
|
|
|
directive on your declaration like:
|
|
|
|
|
|
|
|
class #{active_record.name} < ActiveRecord::Base
|
|
|
|
#{macro} :#{name}, #{example_options}
|
|
|
|
end
|
|
|
|
|
|
|
|
eowarn
|
|
|
|
end
|
|
|
|
|
|
|
|
@source_reflection_name = names.first
|
2006-03-18 19:53:24 -05:00
|
|
|
end
|
|
|
|
|
2011-02-20 15:42:35 -05:00
|
|
|
def source_options
|
|
|
|
source_reflection.options
|
|
|
|
end
|
|
|
|
|
|
|
|
def through_options
|
|
|
|
through_reflection.options
|
|
|
|
end
|
|
|
|
|
2006-03-18 19:53:24 -05:00
|
|
|
def check_validity!
|
2008-10-04 11:53:13 -04:00
|
|
|
if through_reflection.nil?
|
|
|
|
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)
|
|
|
|
end
|
2007-03-13 01:23:18 -04:00
|
|
|
|
2010-12-23 09:19:08 -05:00
|
|
|
if through_reflection.options[:polymorphic]
|
|
|
|
raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self)
|
|
|
|
end
|
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
if source_reflection.nil?
|
|
|
|
raise HasManyThroughSourceAssociationNotFoundError.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:source_type] && source_reflection.options[:polymorphic].nil?
|
|
|
|
raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection)
|
|
|
|
end
|
|
|
|
|
|
|
|
if source_reflection.options[:polymorphic] && options[:source_type].nil?
|
2010-12-23 09:19:08 -05:00
|
|
|
raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection)
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 09:17:29 -05:00
|
|
|
if macro == :has_one && through_reflection.collection?
|
|
|
|
raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection)
|
2008-10-04 11:53:13 -04:00
|
|
|
end
|
|
|
|
|
2009-05-01 11:01:13 -04:00
|
|
|
check_validity_of_inverse!
|
2006-03-18 19:53:24 -05:00
|
|
|
end
|
|
|
|
|
2013-07-23 20:00:15 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def actual_source_reflection # FIXME: this is a horrible name
|
|
|
|
source_reflection.actual_source_reflection
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
2007-01-14 07:08:44 -05:00
|
|
|
def derive_class_name
|
|
|
|
# get the class_name of the belongs_to association of the through reflection
|
2008-10-04 11:53:13 -04:00
|
|
|
options[:source_type] || source_reflection.class_name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-10 00:22:08 -04:00
|
|
|
end
|