Changeset 1028

Show
Ignore:
Timestamp:
11/20/07 06:20:45 (11 months ago)
Author:
jimfree..@gmail.com
Message:

Added submit_field and ability to set attributes on the checkbox auto added input.

Files:

Legend:

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

    r1027 r1028  
    166166      def text_control(col, attrs = {}) 
    167167        errorify_field(attrs, col) 
     168        attrs.merge!(:id => control_id(col)) 
    168169        text_field(control_name_value(col, attrs)) 
    169170      end 
     
    214215      # ==== Example 
    215216      #     <%= checkbox_control :is_activated, :label => "Activated?" %> 
    216       def checkbox_control(col, attrs = {}
     217      def checkbox_control(col, attrs = {}, hidden_attrs={}
    217218        errorify_field(attrs, col) 
    218219        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) 
    220222      end 
    221223 
     
    224226      # ==== Example 
    225227      #     <% checkbox_field :name => "is_activated", :value => "1" %> 
    226       def checkbox_field(attrs = {}
     228      def checkbox_field(attrs = {}, hidden_attrs={}
    227229        on            = attrs.delete(:on)  || 1 
    228230        off           = attrs.delete(:off) || 0 
     
    230232        attrs.merge!(:type => :checkbox) 
    231233        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)} 
    233235      end 
    234236       
     
    244246        attrs.delete(:label) 
    245247        errorify_field(attrs, col) 
     248        attrs[:class] ||= "hidden" 
    246249        hidden_field(control_name_value(col, attrs)) 
    247250      end 
     
    486489      end 
    487490       
     491      def submit_field(attrs = {}) 
     492        attrs.merge!(:type => :submit) 
     493        attrs[:name] ||= "submit" 
     494        self_closing_tag("input", attrs) 
     495      end 
     496       
    488497      private 
    489498      # Fake out the browser to send back the method for RESTful stuff. 
     
    506515        if label 
    507516          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) + yield 
     517          named = attrs[:id].blank? ? {} : {:for => attrs[:id]} 
     518          label(title, '', label.is_a?(Hash) ? label.merge(named) : named) + yield 
    510519        else 
    511520          yield 
  • plugins/merb_helpers/specs/merb_helpers_spec.rb

    r1009 r1028  
    249249  it "should take a string and return a useful checkbox control" do 
    250250    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"
    252252      checkbox_control(:bat).should match_tag(:input, :type =>"checkbox", :name => "fake_model[bat]", :class => "checkbox", :value => "0") 
    253253    end 
     
    559559end 
    560560 
     561describe "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 
     573end