Changeset 1028
- Timestamp:
- 11/20/07 06:20:45 (11 months ago)
- Files:
-
- plugins/merb_helpers/lib/form_helpers.rb (modified) (7 diffs)
- plugins/merb_helpers/specs/merb_helpers_spec.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_helpers/lib/form_helpers.rb
r1027 r1028 166 166 def text_control(col, attrs = {}) 167 167 errorify_field(attrs, col) 168 attrs.merge!(:id => control_id(col)) 168 169 text_field(control_name_value(col, attrs)) 169 170 end … … 214 215 # ==== Example 215 216 # <%= checkbox_control :is_activated, :label => "Activated?" %> 216 def checkbox_control(col, attrs = {} )217 def checkbox_control(col, attrs = {}, hidden_attrs={}) 217 218 errorify_field(attrs, col) 218 219 attrs.merge!(:checked => "checked") if col_val_to_bool(@_obj.send(col)) 219 checkbox_field(control_name_value(col, attrs)) 220 attrs.merge!(:id => control_id(col)) 221 checkbox_field(control_name_value(col, attrs), hidden_attrs) 220 222 end 221 223 … … 224 226 # ==== Example 225 227 # <% checkbox_field :name => "is_activated", :value => "1" %> 226 def checkbox_field(attrs = {} )228 def checkbox_field(attrs = {}, hidden_attrs={}) 227 229 on = attrs.delete(:on) || 1 228 230 off = attrs.delete(:off) || 0 … … 230 232 attrs.merge!(:type => :checkbox) 231 233 attrs.add_html_class!("checkbox") 232 hidden_field( :name => attrs[:name], :value => off) + optional_label(attrs){self_closing_tag("input", attrs)}234 hidden_field({:name => attrs[:name], :value => off}.merge(hidden_attrs)) + optional_label(attrs){self_closing_tag("input", attrs)} 233 235 end 234 236 … … 244 246 attrs.delete(:label) 245 247 errorify_field(attrs, col) 248 attrs[:class] ||= "hidden" 246 249 hidden_field(control_name_value(col, attrs)) 247 250 end … … 486 489 end 487 490 491 def submit_field(attrs = {}) 492 attrs.merge!(:type => :submit) 493 attrs[:name] ||= "submit" 494 self_closing_tag("input", attrs) 495 end 496 488 497 private 489 498 # Fake out the browser to send back the method for RESTful stuff. … … 506 515 if label 507 516 title = label.is_a?(Hash) ? label.delete(:title) : label 508 named = attrs[: name].blank? ? {} : {:for => attrs[:name]}509 label(title, '', label.is_a?(Hash) ? label : named) + yield517 named = attrs[:id].blank? ? {} : {:for => attrs[:id]} 518 label(title, '', label.is_a?(Hash) ? label.merge(named) : named) + yield 510 519 else 511 520 yield plugins/merb_helpers/specs/merb_helpers_spec.rb
r1009 r1028 249 249 it "should take a string and return a useful checkbox control" do 250 250 form_for :obj do 251 checkbox_control(:baz).should match_tag(:input, :type =>"checkbox", :name => "fake_model[baz]", :class => "checkbox", :value => "1", :checked => "checked" )251 checkbox_control(:baz).should match_tag(:input, :type =>"checkbox", :name => "fake_model[baz]", :class => "checkbox", :value => "1", :checked => "checked", :id => "fake_model_baz") 252 252 checkbox_control(:bat).should match_tag(:input, :type =>"checkbox", :name => "fake_model[bat]", :class => "checkbox", :value => "0") 253 253 end … … 559 559 end 560 560 561 describe "submit_field (basic)" do 562 it "should return a basic submit input based on the values passed in" do 563 submit_field(:name => "foo", :value => "bar").should match_tag(:input, :type => "submit", :name => "foo", :value => "bar") 564 end 565 566 it "should provide an additional label tag if the :label option is passed in" do 567 result = submit_field(:value => "foo", :label => "LABEL") 568 result.should match(/<input.*type="submit"/) 569 result.should match(/<input.*name="submit"/) 570 result.should match(/<input.*value="foo"/) 571 result.should match(/<input.*label="LABEL"/) 572 end 573 end
