Changeset 925
- Timestamp:
- 11/10/07 12:24:27 (1 year ago)
- Files:
-
- plugins/merb_helpers/lib/form_helpers.rb (modified) (2 diffs)
- plugins/merb_helpers/specs/merb_helpers_spec.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_helpers/lib/form_helpers.rb
r923 r925 77 77 end 78 78 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) 81 83 end 82 private :c b_val_to_bool84 private :col_val_to_bool 83 85 84 86 def checkbox_control(col, attrs = {}) 85 87 errorify_field(attrs, col) 86 attrs.merge!(:checked => "checked") if c b_val_to_bool(@_obj.send(col))88 attrs.merge!(:checked => "checked") if col_val_to_bool(@_obj.send(col)) 87 89 checkbox_field(control_name_value(col, attrs)) 88 90 end … … 91 93 on = attrs.delete(:on) || 1 92 94 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 != "" ) 94 96 attrs.merge!(:type => :checkbox) 95 97 attrs.add_html_class!("checkbox") plugins/merb_helpers/specs/merb_helpers_spec.rb
r923 r925 223 223 end 224 224 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 225 235 it "should render controls with errors if their attribute contains an error" do 226 236 form_for :obj do
