mirror of
https://github.com/github/rails.git
synced 2026-01-27 23:38:11 -05:00
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
This commit is contained in:
@@ -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 @@ module ActiveRecord
|
||||
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 @@ module ActiveRecord
|
||||
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 @@ module ActiveRecord
|
||||
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 @@ class BasicsTest < Test::Unit::TestCase
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user