Skip to content

Commit

Permalink
r3174@asus: jeremy | 2005-11-19 01:53:00 -0800
Browse files Browse the repository at this point in the history
 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
jeremy committed Nov 19, 2005
1 parent d4039f5 commit fe09fa2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 =~ /\((.*)\)/
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions activerecord/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe09fa2

Please sign in to comment.