diff options
author | Les Fletcher <les.fletcher@gmail.com> | 2011-07-21 01:36:02 -0700 |
---|---|---|
committer | Gabriel Horner <gabriel.horner@gmail.com> | 2011-07-24 00:31:28 -0400 |
commit | f956759f8ab656484add2d5bc6e8f6f4add79469 (patch) | |
tree | 3783716dfd568e2b9c61dfb4daf310031ebb0ef1 | |
parent | d33eb07543aed1a07f9efe40ff7edc983b401957 (diff) | |
download | rails-f956759f8ab656484add2d5bc6e8f6f4add79469.tar.gz rails-f956759f8ab656484add2d5bc6e8f6f4add79469.tar.bz2 rails-f956759f8ab656484add2d5bc6e8f6f4add79469.zip |
fix after_initialize edge case (close #2074 and close #2175)
fix behavior when after_initialize is defined and a block is passed to Base.create
-rw-r--r-- | activerecord/lib/active_record/base.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 23 | ||||
-rw-r--r-- | activerecord/test/models/wholesale_product.rb | 13 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 5 |
4 files changed, 42 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2c162a6fc8..4136868b39 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -504,8 +504,7 @@ module ActiveRecord #:nodoc: if attributes.is_a?(Array) attributes.collect { |attr| create(attr, options, &block) } else - object = new(attributes, options) - yield(object) if block_given? + object = new(attributes, options, &block) object.save object end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8b4e7dd799..934f0c8f74 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -21,6 +21,7 @@ require 'models/parrot' require 'models/person' require 'models/edge' require 'models/joke' +require 'models/wholesale_product' require 'rexml/document' require 'active_support/core_ext/exception' @@ -260,6 +261,28 @@ class BasicsTest < ActiveRecord::TestCase end end + def test_create_with_after_initialize + wp1 = WholesaleProduct.create(:msrp => 10) + assert_equal(10, wp1.msrp) + assert_equal(5, wp1.wholesale) + + wp2 = WholesaleProduct.create(:wholesale => 10) + assert_equal(20, wp2.msrp) + assert_equal(10, wp2.wholesale) + + wp3 = WholesaleProduct.create do |wp| + wp.msrp = 10 + end + assert_equal(10, wp3.msrp) + assert_equal(5, wp3.wholesale) + + wp4 = WholesaleProduct.create do |wp| + wp.wholesale = 10 + end + assert_equal(20, wp4.msrp) + assert_equal(10, wp4.wholesale) + end + def test_load topics = Topic.find(:all, :order => 'id') assert_equal(4, topics.size) diff --git a/activerecord/test/models/wholesale_product.rb b/activerecord/test/models/wholesale_product.rb new file mode 100644 index 0000000000..e1a1a4c8b1 --- /dev/null +++ b/activerecord/test/models/wholesale_product.rb @@ -0,0 +1,13 @@ +class WholesaleProduct < ActiveRecord::Base + + after_initialize :set_prices + + def set_prices + if msrp.nil? && !wholesale.nil? + self.msrp = 2 * wholesale + elsif !msrp.nil? && wholesale.nil? + self.wholesale = msrp / 2 + end + end + +end
\ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 64e0452100..d2a41ed87a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -678,6 +678,11 @@ ActiveRecord::Schema.define do t.references :wheelable, :polymorphic => true end + create_table :wholesale_products, :force => true do |t| + t.integer :msrp + t.integer :wholesale + end + create_table :zines, :force => true do |t| t.string :title end |