Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

r3174@asus: jeremy | 2005-11-19 01:53:00 -0800

 Apply [3092] to stable.  Correct boolean handling in generated reader methods.  Closes #2945.


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3093 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit fe09fa278eaa0f2dec51f3e001307a5008592035 1 parent d4039f5
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -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>]
View
13 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -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 =~ /\((.*)\)/
View
3  activerecord/test/abstract_unit.rb
@@ -22,3 +22,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
View
34 activerecord/test/base_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.