1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Tidy up previous commit.

This commit is contained in:
José Valim 2010-08-02 16:51:08 +02:00
parent f8b53f35b9
commit e1344bf504
3 changed files with 16 additions and 18 deletions

View file

@ -1437,11 +1437,11 @@ module ActiveRecord
association.replace(new_value)
association
end
redefine_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
pk_column = reflection.klass.columns.find{|c| c.name == reflection.klass.primary_key }
pk_column = reflection.primary_key_column
ids = (new_value || []).reject { |nid| nid.blank? }
ids.map!{|i| pk_column.type_cast(i)}
ids.map!{ |i| pk_column.type_cast(i) }
send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
end
end

View file

@ -91,25 +91,19 @@ module ActiveRecord
#
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>:balance</tt>
# <tt>has_many :clients</tt> returns <tt>:clients</tt>
def name
@name
end
attr_reader :name
# Returns the macro type.
#
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>:composed_of</tt>
# <tt>has_many :clients</tt> returns <tt>:has_many</tt>
def macro
@macro
end
attr_reader :macro
# Returns the hash of options used for the macro.
#
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>{ :class_name => "Money" }</tt>
# <tt>has_many :clients</tt> returns +{}+
def options
@options
end
attr_reader :options
# Returns the class for the macro.
#
@ -137,11 +131,6 @@ module ActiveRecord
@sanitized_conditions ||= klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
end
# Returns +true+ if +self+ is a +belongs_to+ reflection.
def belongs_to?
macro == :belongs_to
end
private
def derive_class_name
name.to_s.camelize
@ -213,6 +202,10 @@ module ActiveRecord
@primary_key_name ||= options[:foreign_key] || derive_primary_key_name
end
def primary_key_column
@primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
end
def association_foreign_key
@association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end
@ -307,6 +300,11 @@ module ActiveRecord
dependent_conditions
end
# Returns +true+ if +self+ is a +belongs_to+ reflection.
def belongs_to?
macro == :belongs_to
end
private
def derive_class_name
class_name = name.to_s.camelize

View file

@ -21,7 +21,7 @@ require 'models/subscription'
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors,
:owners, :pets, :toys, :jobs, :references, :companies,
:subscribers, :books, :subscriptions
:subscribers, :books, :subscriptions, :developers
# Dummies to force column loads so query counts are clean.
def setup