2010-11-19 12:29:33 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
|
|
|
|
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
|
|
|
|
class_attribute :reflections
|
|
|
|
self.reflections = {}
|
|
|
|
end
|
|
|
|
|
2010-07-30 14:43:13 -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
|
|
|
#
|
2010-07-30 14:43:13 -04:00
|
|
|
# MacroReflection class has info for AggregateReflection and AssociationReflection
|
|
|
|
# classes.
|
2004-11-23 20:04:44 -05:00
|
|
|
module ClassMethods
|
2005-12-02 23:29:55 -05:00
|
|
|
def create_reflection(macro, name, options, active_record)
|
|
|
|
case macro
|
|
|
|
when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
|
2008-10-04 11:53:13 -04:00
|
|
|
klass = options[:through] ? ThroughReflection : AssociationReflection
|
|
|
|
reflection = klass.new(macro, name, options, active_record)
|
2005-12-02 23:29:55 -05:00
|
|
|
when :composed_of
|
2006-02-25 18:32:24 -05:00
|
|
|
reflection = AggregateReflection.new(macro, name, options, active_record)
|
2005-12-02 23:29:55 -05:00
|
|
|
end
|
2008-10-04 11:53:13 -04:00
|
|
|
|
2010-11-19 12:29:33 -05:00
|
|
|
self.reflections = self.reflections.merge(name => reflection)
|
|
|
|
reflection
|
2005-12-02 23:29:55 -05:00
|
|
|
end
|
2008-10-04 11:53:13 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
# Returns an array of AggregateReflection objects for all the aggregations in the class.
|
|
|
|
def reflect_on_all_aggregations
|
2010-10-20 19:41:50 -04:00
|
|
|
reflections.values.grep(AggregateReflection)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the AggregateReflection object for the named +aggregation+ (use the symbol).
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2010-08-12 11:09:58 -04:00
|
|
|
# Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
def reflect_on_aggregation(aggregation)
|
2005-12-02 23:29:55 -05:00
|
|
|
reflections[aggregation].is_a?(AggregateReflection) ? reflections[aggregation] : nil
|
2004-11-23 20:04:44 -05: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)
|
2010-10-20 19:41:50 -04:00
|
|
|
association_reflections = reflections.values.grep(AssociationReflection)
|
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)
|
2005-12-02 23:29:55 -05:00
|
|
|
reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
|
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
|
|
|
|
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Abstract base class for AggregateReflection and AssociationReflection. Objects of
|
2010-06-16 13:45:04 -04:00
|
|
|
# AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
|
2004-11-23 20:04:44 -05:00
|
|
|
class MacroReflection
|
|
|
|
attr_reader :active_record
|
2007-01-14 07:08:44 -05:00
|
|
|
|
2005-02-27 12:18:35 -05:00
|
|
|
def initialize(macro, name, options, active_record)
|
|
|
|
@macro, @name, @options, @active_record = macro, name, options, active_record
|
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 name of the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -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
|
|
|
#
|
|
|
|
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>:composed_of</tt>
|
|
|
|
# <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
|
|
|
|
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
|
|
|
#
|
|
|
|
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>{ :class_name => "Money" }</tt>
|
|
|
|
# <tt>has_many :clients</tt> returns +{}+
|
2010-08-02 10:51:08 -04:00
|
|
|
attr_reader :options
|
2005-07-10 00:22:08 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Returns the class for the macro.
|
2010-07-30 14:43:13 -04:00
|
|
|
#
|
|
|
|
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns the Money class
|
|
|
|
# <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
|
|
|
#
|
|
|
|
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>'Money'</tt>
|
|
|
|
# <tt>has_many :clients</tt> returns <tt>'Client'</tt>
|
2005-12-02 23:29:55 -05:00
|
|
|
def class_name
|
2007-01-14 07:08:44 -05:00
|
|
|
@class_name ||= options[:class_name] || derive_class_name
|
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)
|
2008-08-15 16:51:57 -04:00
|
|
|
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
|
|
|
|
2008-06-26 21:47:30 -04:00
|
|
|
def sanitized_conditions #:nodoc:
|
|
|
|
@sanitized_conditions ||= klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Holds all the meta-data about an aggregation 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 AggregateReflection < MacroReflection #:nodoc:
|
|
|
|
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
|
|
|
|
2010-03-14 18:46:03 -04:00
|
|
|
def initialize(macro, name, options, active_record)
|
|
|
|
super
|
|
|
|
@collection = [:has_many, :has_and_belongs_to_many].include?(macro)
|
|
|
|
end
|
|
|
|
|
2008-09-06 13:43:14 -04:00
|
|
|
# Returns a new, unsaved instance of the associated class. +options+ will
|
|
|
|
# be passed to the class's constructor.
|
|
|
|
def build_association(*options)
|
|
|
|
klass.new(*options)
|
|
|
|
end
|
|
|
|
|
2010-06-11 06:15:34 -04:00
|
|
|
# Creates a new instance of the associated class, and immediately saves it
|
2008-09-06 13:43:14 -04:00
|
|
|
# with ActiveRecord::Base#save. +options+ will be passed to the class's
|
|
|
|
# creation method. Returns the newly created object.
|
|
|
|
def create_association(*options)
|
|
|
|
klass.create(*options)
|
|
|
|
end
|
|
|
|
|
2010-06-11 06:15:34 -04:00
|
|
|
# Creates a new instance of the associated class, and immediately saves it
|
2008-09-06 13:43:14 -04:00
|
|
|
# with ActiveRecord::Base#save!. +options+ will be passed to the class's
|
|
|
|
# creation method. If the created record doesn't pass validations, then an
|
|
|
|
# exception will be raised.
|
|
|
|
#
|
|
|
|
# Returns the newly created object.
|
|
|
|
def create_association!(*options)
|
|
|
|
klass.create!(*options)
|
|
|
|
end
|
|
|
|
|
2005-04-18 14:49:34 -04:00
|
|
|
def table_name
|
|
|
|
@table_name ||= klass.table_name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2008-01-05 09:58:28 -05:00
|
|
|
def quoted_table_name
|
|
|
|
@quoted_table_name ||= klass.quoted_table_name
|
|
|
|
end
|
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
def primary_key_name
|
2007-01-14 07:08:44 -05:00
|
|
|
@primary_key_name ||= options[:foreign_key] || derive_primary_key_name
|
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
|
|
|
|
@primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
|
|
|
|
end
|
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
def association_foreign_key
|
|
|
|
@association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
|
|
|
|
end
|
|
|
|
|
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]
|
|
|
|
options[:counter_cache]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-16 14:04:19 -05:00
|
|
|
def columns(tbl_name, log_msg)
|
|
|
|
@columns ||= klass.connection.columns(tbl_name, log_msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_column_information
|
|
|
|
@columns = nil
|
|
|
|
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
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2008-10-04 12:41:27 -04:00
|
|
|
def through_reflection_primary_key_name
|
|
|
|
end
|
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
def source_reflection
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2009-05-01 11:01:13 -04:00
|
|
|
def has_inverse?
|
|
|
|
!@options[:inverse_of].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def inverse_of
|
|
|
|
if has_inverse?
|
|
|
|
@inverse_of ||= klass.reflect_on_association(options[:inverse_of])
|
|
|
|
end
|
|
|
|
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
|
2010-07-30 14:43:13 -04:00
|
|
|
# <tt>:validate => false</tt>, validation will take place when:
|
2010-01-03 15:45:08 -05:00
|
|
|
#
|
2010-06-11 06:15:34 -04:00
|
|
|
# * you explicitly enable validation; <tt>:validate => true</tt>
|
2010-01-03 15:45:08 -05:00
|
|
|
# * you use autosave; <tt>:autosave => true</tt>
|
|
|
|
# * the association is a +has_many+ association
|
|
|
|
def validate?
|
|
|
|
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
|
|
|
|
end
|
|
|
|
|
2010-04-03 12:54:15 -04:00
|
|
|
def dependent_conditions(record, base_class, extra_conditions)
|
2010-04-03 01:33:57 -04:00
|
|
|
dependent_conditions = []
|
|
|
|
dependent_conditions << "#{primary_key_name} = #{record.send(name).send(:owner_quoted_id)}"
|
|
|
|
dependent_conditions << "#{options[:as]}_type = '#{base_class.name}'" if options[:as]
|
|
|
|
dependent_conditions << klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
|
2010-04-03 12:54:15 -04:00
|
|
|
dependent_conditions << extra_conditions if extra_conditions
|
2010-04-03 01:33:57 -04:00
|
|
|
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
|
|
|
|
dependent_conditions = dependent_conditions.gsub('@', '\@')
|
|
|
|
dependent_conditions
|
|
|
|
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
|
|
|
|
|
2008-10-04 11:53:13 -04:00
|
|
|
private
|
|
|
|
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
|
|
|
|
|
|
|
|
def derive_primary_key_name
|
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
|
|
|
|
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:
|
2010-06-16 13:45:04 -04:00
|
|
|
# Gets the source of the through reflection. It checks both a singularized
|
|
|
|
# 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
|
|
|
|
# has_many :tags, :through => :taggings
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def source_reflection
|
|
|
|
@source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first
|
|
|
|
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
|
|
|
|
# has_many :tags, :through => :taggings
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# tags_reflection = Post.reflect_on_association(:tags)
|
|
|
|
# taggings_reflection = tags_reflection.through_reflection
|
|
|
|
#
|
2006-03-18 17:38:49 -05:00
|
|
|
def through_reflection
|
2008-10-04 11:53:13 -04:00
|
|
|
@through_reflection ||= active_record.reflect_on_association(options[:through])
|
2006-03-18 17:38:49 -05:00
|
|
|
end
|
|
|
|
|
2008-05-02 09:45:23 -04:00
|
|
|
# Gets an array of possible <tt>:through</tt> source reflection names:
|
2006-03-19 02:14:48 -05:00
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# [:singularized, :pluralized]
|
2007-01-05 15:22:41 -05:00
|
|
|
#
|
2006-03-19 02:14:48 -05:00
|
|
|
def source_reflection_names
|
2006-03-24 09:46:17 -05:00
|
|
|
@source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }
|
2006-03-18 19:53:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
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?
|
|
|
|
raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection)
|
|
|
|
end
|
|
|
|
|
2009-08-09 15:53:22 -04:00
|
|
|
unless [:belongs_to, :has_many, :has_one].include?(source_reflection.macro) && source_reflection.options[:through].nil?
|
2008-10-04 11:53:13 -04:00
|
|
|
raise HasManyThroughSourceAssociationMacroError.new(self)
|
2006-03-18 19:53:24 -05:00
|
|
|
end
|
2009-05-01 11:01:13 -04:00
|
|
|
|
|
|
|
check_validity_of_inverse!
|
2006-03-18 19:53:24 -05:00
|
|
|
end
|
|
|
|
|
2008-10-04 12:41:27 -04:00
|
|
|
def through_reflection_primary_key
|
|
|
|
through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.primary_key_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def through_reflection_primary_key_name
|
|
|
|
through_reflection.primary_key_name if through_reflection.belongs_to?
|
|
|
|
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
|