Changeset 1169

Show
Ignore:
Timestamp:
01/05/08 08:57:20 (8 months ago)
Author:
rogelio.samo..@gmail.com
Message:

- Allows checkbox_field to have values other than 1/0 when "checked"
- All specs pass and completely backwards compatible. Closes #402 [gtdev@spearhead.de]

Files:

Legend:

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

    r1168 r1169  
    224224 
    225225      # Provides a generic HTML checkbox input tag. 
     226      # There are two ways this tag can be generated, based on the 
     227      # option :boolean. If not set to true, a "magic" input is generated. 
     228      # Otherwise, an input is created that can be easily used for passing 
     229      # an array of values to the application. 
    226230      # 
    227231      # ==== Example 
    228232      #     <% checkbox_field :name => "is_activated", :value => "1" %> 
     233      # 
     234      #     <% checkbox_field :name => "choices[]", :boolean => false, :value => "dog" %> 
     235      #     <% checkbox_field :name => "choices[]", :boolean => false, :value => "cat" %> 
     236      #     <% checkbox_field :name => "choices[]", :boolean => false, :value => "weasle" %> 
    229237      def checkbox_field(attrs = {}, hidden_attrs={}) 
    230         on            = attrs.delete(:on)  || 1 
    231         off           = attrs.delete(:off) || 0 
    232         attrs[:value] = on if ( (v = attrs[:value]).nil? || v != "" ) 
     238        boolbox = true 
     239        boolbox = false if ( attrs.has_key?(:boolean) and !attrs[:boolean] ) 
     240        attrs.delete(:boolean) 
     241 
     242        if( boolbox ) 
     243                on            = attrs.delete(:on)  || 1 
     244                off           = attrs.delete(:off) || 0 
     245                attrs[:value] = on if ( (v = attrs[:value]).nil? || v != "" ) 
     246        else 
     247                # HTML-escape the value attribute 
     248                attrs[:value] = escape_xml( attrs[:value] ) 
     249        end 
     250 
    233251        attrs.merge!(:type => :checkbox) 
    234252        attrs.add_html_class!("checkbox") 
    235         hidden_field({:name => attrs[:name], :value => off}.merge(hidden_attrs)) + optional_label(attrs){self_closing_tag("input", attrs)} 
    236       end 
    237        
     253        (boolbox ? hidden_field({:name => attrs[:name], :value => off}.merge(hidden_attrs)) : '') + optional_label(attrs){self_closing_tag("input", attrs)} 
     254      end 
     255 
    238256      # Returns a hidden input tag tailored for accessing a specified attribute (identified by +col+) on an object 
    239257      # resource within a +form_for+ resource block. Additional options on the input tag can be passed as a 
  • plugins/merb_helpers/spec/merb_helpers_spec.rb

    r1168 r1169  
    283283    checkbox_field(:name => "foo", :checked => "checked").should match_tag(:input, :class => "checkbox", :name => "foo", :checked => "checked") 
    284284  end 
     285 
     286  it "should return a basic checkbox with :boolean => true" do 
     287    checkbox_field(:name => "foo", :checked => "checked", :boolean => true).should match_tag(:input, :class => "checkbox", :name => "foo", :checked => "checked") 
     288  end 
     289 
     290  it "should return a basic checkbox with a value other than 1/0" do 
     291    checkbox_field(:name => "choices[]", :boolean => false, :value => "dog").should match_tag(:input, :class => "checkbox", :name => "choices[]", :value => "dog") 
     292  end 
    285293   
    286294  it "should provide an additional label tag if the :label option is passed in" do