Skip to content

Commit

Permalink
added an alias for new to build to the AR collection proxy, this corr…
Browse files Browse the repository at this point in the history
…ects an issue where the collection proxies were not consistent
  • Loading branch information
joshk committed May 31, 2011
1 parent 64d7348 commit 22b02cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
10 changes: 2 additions & 8 deletions activerecord/lib/active_record/associations/collection_proxy.rb
Expand Up @@ -56,6 +56,8 @@ def initialize(association)
Array.wrap(association.options[:extend]).each { |ext| proxy_extend(ext) } Array.wrap(association.options[:extend]).each { |ext| proxy_extend(ext) }
end end


alias_method :new, :build

def respond_to?(*args) def respond_to?(*args)
super || super ||
(load_target && target.respond_to?(*args)) || (load_target && target.respond_to?(*args)) ||
Expand Down Expand Up @@ -116,14 +118,6 @@ def reload
self self
end end


def new(*args, &block)
if @association.is_a?(HasManyThroughAssociation)
@association.build(*args, &block)
else
method_missing(:new, *args, &block)
end
end

def proxy_owner def proxy_owner
ActiveSupport::Deprecation.warn( ActiveSupport::Deprecation.warn(
"Calling record.#{@association.reflection.name}.proxy_owner is deprecated. Please use " \ "Calling record.#{@association.reflection.name}.proxy_owner is deprecated. Please use " \
Expand Down
Expand Up @@ -6,8 +6,6 @@ module Associations
class HasManyThroughAssociation < HasManyAssociation #:nodoc: class HasManyThroughAssociation < HasManyAssociation #:nodoc:
include ThroughAssociation include ThroughAssociation


alias_method :new, :build

# Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been
# loaded and calling collection.size if it has. If it's more likely than not that the collection does # loaded and calling collection.size if it has. If it's more likely than not that the collection does
# have a size larger than zero, and you need to fetch that collection afterwards, it'll take one fewer # have a size larger than zero, and you need to fetch that collection afterwards, it'll take one fewer
Expand Down
Expand Up @@ -235,6 +235,21 @@ def test_build
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end end


def test_new_aliased_to_build
devel = Developer.find(1)
proj = assert_no_queries { devel.projects.new("name" => "Projekt") }
assert !devel.projects.loaded?

assert_equal devel.projects.last, proj
assert devel.projects.loaded?

assert !proj.persisted?
devel.save
assert proj.persisted?
assert_equal devel.projects.last, proj
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end

def test_build_by_new_record def test_build_by_new_record
devel = Developer.new(:name => "Marcel", :salary => 75000) devel = Developer.new(:name => "Marcel", :salary => 75000)
devel.projects.build(:name => "Make bed") devel.projects.build(:name => "Make bed")
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -537,6 +537,16 @@ def test_adding_a_collection
assert_equal 3, companies(:first_firm).clients_of_firm(true).size assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end end


def test_new_aliased_to_build
company = companies(:first_firm)
new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") }
assert !company.clients_of_firm.loaded?

assert_equal "Another Client", new_client.name
assert !new_client.persisted?
assert_equal new_client, company.clients_of_firm.last
end

def test_build def test_build
company = companies(:first_firm) company = companies(:first_firm)
new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") } new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
Expand Down

0 comments on commit 22b02cb

Please sign in to comment.