Changeset 925

Show
Ignore:
Timestamp:
11/10/07 12:24:27 (1 year ago)
Author:
jimfree..@gmail.com
Message:

Fixed bug introduced into checkbox_control - does not raise when :value => true

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_helpers/lib/form_helpers.rb

    r923 r925  
    7777      end 
    7878       
    79       def cb_val_to_bool(val) 
    80         !(val == false || val.nil?) && val.to_i != 0 
     79      # translate column values from the db to boolean 
     80      # nil, false, 0 and '0' are false. All others are true 
     81      def col_val_to_bool(val) 
     82        !(val == "0" || val == 0 || !val) 
    8183      end 
    82       private :cb_val_to_bool 
     84      private :col_val_to_bool 
    8385 
    8486      def checkbox_control(col, attrs = {}) 
    8587        errorify_field(attrs, col) 
    86         attrs.merge!(:checked => "checked") if cb_val_to_bool(@_obj.send(col)) 
     88        attrs.merge!(:checked => "checked") if col_val_to_bool(@_obj.send(col)) 
    8789        checkbox_field(control_name_value(col, attrs)) 
    8890      end 
     
    9193        on            = attrs.delete(:on)  || 1 
    9294        off           = attrs.delete(:off) || 0 
    93         attrs[:value] = on if (!attrs.has_key?(:value) || attrs[:value].nil?
     95        attrs[:value] = on if ( (v = attrs[:value]).nil? || v != ""
    9496        attrs.merge!(:type => :checkbox) 
    9597        attrs.add_html_class!("checkbox") 
  • plugins/merb_helpers/specs/merb_helpers_spec.rb

    r923 r925  
    223223  end 
    224224   
     225  it "should evaulate nil, false, 0 and '0' to false. All else to true" do 
     226    send(:col_val_to_bool, nil).should   == false 
     227    send(:col_val_to_bool, false).should == false 
     228    send(:col_val_to_bool, 0).should     == false 
     229    send(:col_val_to_bool, '0').should   == false 
     230    send(:col_val_to_bool, 1).should     == true 
     231    send(:col_val_to_bool, '1').should   == true 
     232    send(:col_val_to_bool, true).should  == true 
     233  end 
     234 
    225235  it "should render controls with errors if their attribute contains an error" do 
    226236    form_for :obj do