diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-11-06 01:27:29 -0800 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-11-06 01:27:29 -0800 |
commit | 62512b9fb9750b0dda4acb65e4fc368260e3b66c (patch) | |
tree | 22a9f2da5f5667741438346857301469cd0f97d1 | |
parent | e7b7b4412380e7ce2d8e6ae402cb7fe02d7666b8 (diff) | |
parent | c7d20785965d5f0877d0c13d0d9929f2fce50a8b (diff) | |
download | rails-62512b9fb9750b0dda4acb65e4fc368260e3b66c.tar.gz rails-62512b9fb9750b0dda4acb65e4fc368260e3b66c.tar.bz2 rails-62512b9fb9750b0dda4acb65e4fc368260e3b66c.zip |
Merge pull request #3535 from jmazzi/master
Update ActiveRecord#attribute_present? to work as documented
-rw-r--r-- | activerecord/lib/active_record/base.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 18 |
2 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 455b299270..feeebb7131 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1771,7 +1771,8 @@ MSG # Returns true if the specified +attribute+ has been set by the user or by a database load and is neither # nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings). def attribute_present?(attribute) - !_read_attribute(attribute).blank? + value = _read_attribute(attribute) + !value.nil? || (value.respond_to?(:empty?) && !value.empty?) end # Returns the column object for the named attribute. diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index b1b41fed0d..9300c57819 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -35,6 +35,24 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert !t.attribute_present?("content") end + def test_attribute_present_with_booleans + b1 = Boolean.new + b1.value = false + assert b1.attribute_present?(:value) + + b2 = Boolean.new + b2.value = true + assert b2.attribute_present?(:value) + + b3 = Boolean.new + assert !b3.attribute_present?(:value) + + b4 = Boolean.new + b4.value = false + b4.save! + assert Boolean.find(b4.id).attribute_present?(:value) + end + def test_attribute_keys_on_new_instance t = Topic.new assert_equal nil, t.title, "The topics table has a title column, so it should be nil" |