Permalink
Browse files

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>
  • Loading branch information...
1 parent 9c6bde5 commit 1fe9d6cc88dbad26e865c7c2bfd83da25796e2f1 @maxlapshin maxlapshin committed with NZKoz Jan 14, 2009
@@ -324,6 +324,7 @@ def query_attribute(attr_name)
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?
@@ -8,6 +8,7 @@ module ConnectionAdapters #:nodoc:
# 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/
@@ -56,6 +56,18 @@ def test_should_unserialize_attributes_for_frozen_records
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"

0 comments on commit 1fe9d6c

Please sign in to comment.