Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3092

Show
Ignore:
Timestamp:
11/19/05 09:53:36 (3 years ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r3089 r3092  
    11*SVN* 
     2 
     3* Correct boolean handling in generated reader methods.  #2945 [don.park@gmail.com, Stefan Kaes] 
    24 
    35* Don't generate read methods for columns whose names are not valid ruby method names.  #2946 [Stefan Kaes] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    r2866 r3092  
    6060          when :date      then self.class.string_to_date(value) 
    6161          when :binary    then self.class.binary_to_string(value) 
    62           when :boolean   then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1' 
     62          when :boolean   then self.class.value_to_boolean(value) 
    6363          else value 
    6464        end 
     
    7676          when :date      then "#{self.class.name}.string_to_date(#{var_name})" 
    7777          when :binary    then "#{self.class.name}.binary_to_string(#{var_name})" 
    78           when :boolean   then "(#{var_name} == true or (#{var_name} =~ /^t(?:true)?$/i) == 0 or #{var_name}.to_s == '1')" 
     78          when :boolean   then "#{self.class.name}.value_to_boolean(#{var_name})" 
    7979          else nil 
    8080        end 
     
    119119        time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; 
    120120        Time.send(Base.default_timezone, *time_array) rescue nil 
     121      end 
     122 
     123      # convert something to a boolean 
     124      def self.value_to_boolean(value) 
     125        return value if value==true || value==false 
     126        case value.to_s.downcase 
     127        when "true", "t", "1" then true 
     128        else false 
     129        end 
    121130      end 
    122131 
  • trunk/activerecord/test/abstract_unit.rb

    r3051 r3092  
    2525    ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type)) 
    2626end 
     27 
     28#ActiveRecord::Base.logger = Logger.new(STDOUT) 
     29#ActiveRecord::Base.colorize_logging = false 
  • trunk/activerecord/test/base_test.rb

    r3089 r3092  
    207207    topic.approved = false 
    208208    assert !topic.approved?, "approved should be false" 
     209    topic.approved = "false" 
     210    assert !topic.approved?, "approved should be false" 
     211  end 
     212 
     213  def test_read_attribute_when_true 
     214    topic = topics(:first) 
     215    topic.approved = true 
     216    assert topic.approved?, "approved should be true" 
     217    topic.approved = "true" 
     218    assert topic.approved?, "approved should be true" 
     219  end 
     220 
     221  def test_read_write_boolean_attribute 
     222    topic = Topic.new 
     223    # puts "" 
     224    # puts "New Topic" 
     225    # puts topic.inspect 
     226    topic.approved = "false" 
     227    # puts "Expecting false" 
     228    # puts topic.inspect 
     229    assert !topic.approved, "approved should be false" 
     230    topic.approved = "false" 
     231    # puts "Expecting false" 
     232    # puts topic.inspect 
     233    assert !topic.approved, "approved should be false" 
     234    topic.approved = "true" 
     235    # puts "Expecting true" 
     236    # puts topic.inspect 
     237    assert topic.approved, "approved should be true" 
     238    topic.approved = "true" 
     239    # puts "Expecting true" 
     240    # puts topic.inspect 
     241    assert topic.approved, "approved should be true" 
     242    # puts "" 
    209243  end 
    210244