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

Merge pull request #5316 from Jacobkg/master

Update ActiveRecord::AttributeMethods#attribute_present? to return false for empty strings
This commit is contained in:
José Valim 2012-03-07 07:03:59 -08:00
commit 3da31b948f
2 changed files with 4 additions and 1 deletions

View file

@ -189,7 +189,7 @@ module ActiveRecord
# nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
def attribute_present?(attribute)
value = read_attribute(attribute)
!value.nil? || (value.respond_to?(:empty?) && !value.empty?)
!value.nil? && !(value.respond_to?(:empty?) && value.empty?)
end
# Returns the column object for the named attribute.

View file

@ -30,9 +30,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase
t = Topic.new
t.title = "hello there!"
t.written_on = Time.now
t.author_name = ""
assert t.attribute_present?("title")
assert t.attribute_present?("written_on")
assert !t.attribute_present?("content")
assert !t.attribute_present?("author_name")
end
def test_attribute_present_with_booleans