Permalink
Browse files

Correct boolean handling in generated reader methods. References #2945.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3092 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
jeremy committed Nov 19, 2005
1 parent d777e50 commit 2ed6d365bbb2a3e0138add33be964d1cc1705b76
View
@@ -1,5 +1,7 @@
*SVN*
+* Correct boolean handling in generated reader methods. #2945 [don.park@gmail.com, Stefan Kaes]
+
* Don't generate read methods for columns whose names are not valid ruby method names. #2946 [Stefan Kaes]
* Document :force option to create_table. #2921 [Blair Zajac <blair@orcaware.com>]
@@ -59,7 +59,7 @@ def type_cast(value)
when :time then self.class.string_to_dummy_time(value)
when :date then self.class.string_to_date(value)
when :binary then self.class.binary_to_string(value)
- when :boolean then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1'
+ when :boolean then self.class.value_to_boolean(value)
else value
end
end
@@ -75,7 +75,7 @@ def type_cast_code(var_name)
when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})"
when :date then "#{self.class.name}.string_to_date(#{var_name})"
when :binary then "#{self.class.name}.binary_to_string(#{var_name})"
- when :boolean then "(#{var_name} == true or (#{var_name} =~ /^t(?:true)?$/i) == 0 or #{var_name}.to_s == '1')"
+ when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
else nil
end
end
@@ -120,6 +120,15 @@ def self.string_to_dummy_time(string)
Time.send(Base.default_timezone, *time_array) rescue nil
end
+ # convert something to a boolean
+ def self.value_to_boolean(value)
+ return value if value==true || value==false
+ case value.to_s.downcase
+ when "true", "t", "1" then true
+ else false
+ end
+ end
+
private
def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/
@@ -24,3 +24,6 @@ def current_adapter?(type)
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
end
+
+#ActiveRecord::Base.logger = Logger.new(STDOUT)
+#ActiveRecord::Base.colorize_logging = false
@@ -206,6 +206,40 @@ def test_read_attribute_when_false
topic = topics(:first)
topic.approved = false
assert !topic.approved?, "approved should be false"
+ topic.approved = "false"
+ assert !topic.approved?, "approved should be false"
+ end
+
+ def test_read_attribute_when_true
+ topic = topics(:first)
+ topic.approved = true
+ assert topic.approved?, "approved should be true"
+ topic.approved = "true"
+ assert topic.approved?, "approved should be true"
+ end
+
+ def test_read_write_boolean_attribute
+ topic = Topic.new
+ # puts ""
+ # puts "New Topic"
+ # puts topic.inspect
+ topic.approved = "false"
+ # puts "Expecting false"
+ # puts topic.inspect
+ assert !topic.approved, "approved should be false"
+ topic.approved = "false"
+ # puts "Expecting false"
+ # puts topic.inspect
+ assert !topic.approved, "approved should be false"
+ topic.approved = "true"
+ # puts "Expecting true"
+ # puts topic.inspect
+ assert topic.approved, "approved should be true"
+ topic.approved = "true"
+ # puts "Expecting true"
+ # puts topic.inspect
+ assert topic.approved, "approved should be true"
+ # puts ""
end
def test_reader_generation

0 comments on commit 2ed6d36

Please sign in to comment.