From 427fb035a3d03b23e903ca499abf3c1bfa53b10a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 20 Nov 2006 11:22:38 +0000 Subject: Simplify query_attribute by typecasting the attribute value and checking whether it's nil, false, zero or blank. Closes #6659. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5593 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 22 +++++++--------------- activerecord/test/base_test.rb | 29 ++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 5f6daf66ff..759d7b7784 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Simplify query_attribute by typecasting the attribute value and checking whether it's nil, false, zero or blank. #6659 [Jonathan Viney] + * validates_numericality_of uses \A \Z to ensure the entire string matches rather than ^ $ which may match one valid line of a multiline string. #5716 [Andreas Schwarz] * Run validations in the order they were declared. #6657 [obrie] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 864cf43ef3..99c276ef8b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1976,23 +1976,15 @@ module ActiveRecord #:nodoc: end def query_attribute(attr_name) - attribute = @attributes[attr_name] - if attribute.kind_of?(Fixnum) && attribute == 0 - false - elsif attribute.kind_of?(String) && attribute == "0" - false - elsif attribute.kind_of?(String) && attribute.empty? - false - elsif attribute.nil? - false - elsif attribute == false - false - elsif attribute == "f" - false - elsif attribute == "false" + unless value = read_attribute(attr_name) false else - true + column = self.class.columns_hash[attr_name] + if column.number? + !value.zero? + else + !value.blank? + end end end diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index bddfa882b9..bb057e75f8 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -269,7 +269,34 @@ class BasicsTest < Test::Unit::TestCase assert topic.approved?, "approved should be true" # puts "" end - + + def test_query_attribute_string + [nil, "", " "].each do |value| + assert_equal false, Topic.new(:author_name => value).author_name? + end + + assert_equal true, Topic.new(:author_name => "Name").author_name? + end + + def test_query_attribute_number + [nil, 0, "0"].each do |value| + assert_equal false, Developer.new(:salary => value).salary? + end + + assert_equal true, Developer.new(:salary => 1).salary? + assert_equal true, Developer.new(:salary => "1").salary? + end + + def test_query_attribute_boolean + [nil, "", false, "false", "f", 0].each do |value| + assert_equal false, Topic.new(:approved => value).approved? + end + + [true, "true", "1", 1].each do |value| + assert_equal true, Topic.new(:approved => value).approved? + end + end + def test_reader_generation Topic.find(:first).title Firm.find(:first).name -- cgit v1.2.3