Changeset 758

Show
Ignore:
Timestamp:
10/20/07 15:54:43 (1 year ago)
Author:
wyca..@gmail.com
Message:

fixes up form helper to simplify label issues

Files:

Legend:

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

    r749 r758  
    2020      def obj_from_ivar_or_sym(obj) 
    2121        obj.is_a?(Symbol) ? instance_variable_get("@#{obj}") : obj 
     22      end 
     23       
     24      def tag(tag_name, contents, attrs = {}) 
     25        open_tag(tag_name, attrs) + contents + "</#{tag_name}>" 
    2226      end 
    2327       
     
    7074      def text_field(attrs = {}) 
    7175        attrs.merge!(:type => "text") 
    72         add_field_label(attrs){self_closing_tag("input", attrs)
     76        optional_label(attrs) { self_closing_tag("input", attrs)
    7377      end 
    7478       
     
    8488        attrs.merge!(:type => :checkbox) 
    8589        attrs.add_html_class!("checkbox") 
    86         add_field_label(attrs){self_closing_tag("input", attrs)} 
     90        optional_label(attrs){self_closing_tag("input", attrs)} 
    8791      end 
    8892       
     
    114118        attrs.merge!(:type => "radio") 
    115119        attrs.add_html_class!("radio") 
    116         add_field_label(attrs){self_closing_tag("input", attrs)} 
     120        optional_label(attrs){self_closing_tag("input", attrs)} 
    117121      end 
    118122       
     
    125129      def text_area_field(val, attrs = {}) 
    126130        val ||="" 
    127         add_field_label(attrs) do 
     131        optional_label(attrs) do 
    128132          open_tag("textarea", attrs) + 
    129133          val + 
     
    134138      def submit_button(contents, attrs = {}) 
    135139        attrs.merge!(:type => "submit") 
    136         open_tag("button", attrs) + contents + "</button>" 
     140        tag("button", contents, attrs) 
    137141      end 
    138142 
    139       def errorify_field(attrs, col) 
    140         attrs.add_html_class!("error") if !@_obj.valid? && @_obj.errors.on(col) 
    141       end 
    142        
    143       def add_label(label, &block) 
    144         concat("<label>#{label}", block.binding) 
    145         yield 
    146         concat("</label>", block.binding ) 
    147       end 
    148        
    149        
    150       def add_field_label( attrs, &block ) 
    151         case attrs 
    152         when Hash 
    153           label_name = attrs.delete :label 
    154         when String, Symbol 
    155           label_name = attrs.to_s 
    156         end 
    157         ret = "" 
    158         ret << "<label>#{label_name}" if label_name 
    159         ret << yield 
    160         ret << "</label>" if label_name 
    161         ret 
     143      def label(name, contents = "", attrs = {}) 
     144        tag("label", name.to_s + contents, attrs) 
    162145      end 
    163146       
     
    178161      end 
    179162       
     163      def optional_label(attrs = {}) 
     164        label = attrs.delete(:label) if attrs 
     165        if label 
     166          title = label.is_a?(Hash) ? label.delete(:title) : label 
     167          label(title, yield, label.is_a?(Hash) ? label : {}) 
     168        else 
     169          yield 
     170        end 
     171      end    
     172       
     173      def errorify_field(attrs, col) 
     174        attrs.add_html_class!("error") if !@_obj.valid? && @_obj.errors.on(col) 
     175      end          
     176       
    180177    end 
    181178  end 
  • plugins/merb_helpers/specs/merb_helpers_spec.rb

    r746 r758  
    404404   
    405405  it "should add a label to arbitrary markup in a template" do 
    406     result = add_label("Name:"){ _buffer << text_field(:name => "name_value") } 
     406    result = label("Name:", text_field(:name => "name_value")) 
    407407    result.should == "<label>Name:<input type=\"text\" name=\"name_value\"/></label>" 
    408408     
    409409  end 
    410    
    411   it "should add a label to a text returning helper" do 
    412     form_for(:obj) do 
    413       _buffer << add_field_label({:label => "LABEL"}){ text_control(:foo) } 
    414     end 
    415     _buffer.should match(/<label>LABEL<input .+\/><\/label>/) 
    416   end 
    417    
    418 end 
    419  
     410     
     411end 
     412 
  • trunk/lib/merb/mixins/view_context.rb

    r735 r758  
    358358    # Note: This tag will need to be closed 
    359359    def open_tag(name, attrs = nil) 
    360       "<#{name}#{' ' + attrs.to_html_attributes if attrs}>" 
     360      "<#{name}#{(' ' + attrs.to_html_attributes) if attrs && !attrs.empty?}>" 
    361361    end 
    362362