Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix after_initialize and Base.create edge case #2236

Merged
merged 3 commits into from

5 participants

@cldwalker

This time rebased off master as requested in #2194

hmcfletch and others added some commits
activerecord/test/models/wholesale_product.rb
@@ -0,0 +1,9 @@
+class WholesaleProduct < ActiveRecord::Base
@guilleiguaran Owner

Actually the ActiveRecord maintainers would prefer that you use existing models for new tests instead of add new models

I poked around existing models to see if there was one I could use for this test, but I didn't see one that I could easily repurpose. I didn't want to mess with one that looked they were being used for specific test cases, which most of the ones I looked at seemed to be. I could have put it at the top of the test file, but none of the other classes up there had method declarations. It's my first submitted patch, so I just tried to not mess with things that were already in place too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@cldwalker

@guilleiguaran Updated tests to use existing model. Personally, I don't see it as a good testing practice to be using one model for multiple unrelated tests. Leads to unnecessary coupling and hard to find bugs

@guilleiguaran

I agree with you but we must try to avoid defining extra models since we already have tonnes of them and if every patch add a new model it would quickly get out of hand.

Your code and tests looks great, thanks!!!

@spastorino spastorino merged commit 64affc9 into from
@bboe

Should a similar change also be applied to create! in validations.rb? I'm not familiar enough with the rails code to know if there are other similar places where code should be changed.

@vijaydev vijaydev referenced this pull request
Closed

3 0 stable #2175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 24, 2011
  1. @hmcfletch @cldwalker

    fix after_initialize edge case (close #2074 and close #2175)

    hmcfletch authored cldwalker committed
    fix behavior when after_initialize is defined and a block is passed to Base.create
  2. @cldwalker
  3. @cldwalker
This page is out of date. Refresh to see the latest.
View
3  activerecord/lib/active_record/base.rb
@@ -504,8 +504,7 @@ def create(attributes = nil, options = {}, &block)
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
View
13 activerecord/test/cases/base_test.rb
@@ -21,6 +21,7 @@
require 'models/person'
require 'models/edge'
require 'models/joke'
+require 'models/bulb'
require 'rexml/document'
require 'active_support/core_ext/exception'
@@ -260,6 +261,18 @@ def test_initialize_with_invalid_attribute
end
end
+ def test_create_after_initialize_without_block
+ cb = CustomBulb.create(:name => 'Dude')
+ assert_equal('Dude', cb.name)
+ assert_equal(true, cb.frickinawesome)
+ end
+
+ def test_create_after_initialize_with_block
+ cb = CustomBulb.create {|c| c.name = 'Dude' }
+ assert_equal('Dude', cb.name)
+ assert_equal(true, cb.frickinawesome)
+ end
+
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(4, topics.size)
View
5 activerecord/test/models/bulb.rb
@@ -33,4 +33,9 @@ def self.new(attributes = {}, options = {}, &block)
end
class CustomBulb < Bulb
+ after_initialize :set_awesomeness
+
+ def set_awesomeness
+ self.frickinawesome = true if name == 'Dude'
+ end
end
Something went wrong with that request. Please try again.