diff options
author | Max Lapshin <max@maxidoors.ru> | 2009-01-14 16:09:23 +0300 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-02-06 13:39:08 +1300 |
commit | 1fe9d6cc88dbad26e865c7c2bfd83da25796e2f1 (patch) | |
tree | c0e930054c46d346ed46653bdcd0a7200e1bf9b5 | |
parent | 9c6bde58c52f63d9e40979b89bc5db0c786159da (diff) | |
download | rails-1fe9d6cc88dbad26e865c7c2bfd83da25796e2f1.tar.gz rails-1fe9d6cc88dbad26e865c7c2bfd83da25796e2f1.tar.bz2 rails-1fe9d6cc88dbad26e865c7c2bfd83da25796e2f1.zip |
Support true/false in query_attribute for calculated columns
Signed-off-by: Tarmo Tänav <tarmo@itech.ee>
Signed-off-by: Michael Koziarski <michael@koziarski.com>
3 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 177d156834..3ffc48941c 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -324,6 +324,7 @@ module ActiveRecord if Numeric === value || value !~ /[^0-9]/ !value.to_i.zero? else + return false if ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value) !value.blank? end elsif column.number? diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 273f823e7f..24c734cddb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -8,6 +8,7 @@ module ActiveRecord # An abstract definition of a column in a table. class Column TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set + FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set module Format ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 77ee8d8fc4..759f9f3872 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -56,6 +56,18 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_equal myobj, topic.content end + def test_typecast_attribute_from_select_to_false + topic = Topic.create(:title => 'Budget') + topic = Topic.find(:first, :select => "topics.*, 1=2 as is_test") + assert !topic.is_test? + end + + def test_typecast_attribute_from_select_to_true + topic = Topic.create(:title => 'Budget') + topic = Topic.find(:first, :select => "topics.*, 2=2 as is_test") + assert topic.is_test? + end + def test_kernel_methods_not_implemented_in_activerecord %w(test name display y).each do |method| assert !ActiveRecord::Base.instance_method_already_implemented?(method), "##{method} is defined" |