Changeset 758
- Timestamp:
- 10/20/07 15:54:43 (1 year ago)
- Files:
-
- plugins/merb_helpers/lib/form_helpers.rb (modified) (7 diffs)
- plugins/merb_helpers/specs/merb_helpers_spec.rb (modified) (1 diff)
- trunk/lib/merb/mixins/view_context.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_helpers/lib/form_helpers.rb
r749 r758 20 20 def obj_from_ivar_or_sym(obj) 21 21 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}>" 22 26 end 23 27 … … 70 74 def text_field(attrs = {}) 71 75 attrs.merge!(:type => "text") 72 add_field_label(attrs){self_closing_tag("input", attrs)}76 optional_label(attrs) { self_closing_tag("input", attrs) } 73 77 end 74 78 … … 84 88 attrs.merge!(:type => :checkbox) 85 89 attrs.add_html_class!("checkbox") 86 add_field_label(attrs){self_closing_tag("input", attrs)}90 optional_label(attrs){self_closing_tag("input", attrs)} 87 91 end 88 92 … … 114 118 attrs.merge!(:type => "radio") 115 119 attrs.add_html_class!("radio") 116 add_field_label(attrs){self_closing_tag("input", attrs)}120 optional_label(attrs){self_closing_tag("input", attrs)} 117 121 end 118 122 … … 125 129 def text_area_field(val, attrs = {}) 126 130 val ||="" 127 add_field_label(attrs) do131 optional_label(attrs) do 128 132 open_tag("textarea", attrs) + 129 133 val + … … 134 138 def submit_button(contents, attrs = {}) 135 139 attrs.merge!(:type => "submit") 136 open_tag("button", attrs) + contents + "</button>"140 tag("button", contents, attrs) 137 141 end 138 142 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) 162 145 end 163 146 … … 178 161 end 179 162 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 180 177 end 181 178 end plugins/merb_helpers/specs/merb_helpers_spec.rb
r746 r758 404 404 405 405 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")) 407 407 result.should == "<label>Name:<input type=\"text\" name=\"name_value\"/></label>" 408 408 409 409 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 411 end 412 trunk/lib/merb/mixins/view_context.rb
r735 r758 358 358 # Note: This tag will need to be closed 359 359 def open_tag(name, attrs = nil) 360 "<#{name}#{ ' ' + attrs.to_html_attributes if attrs}>"360 "<#{name}#{(' ' + attrs.to_html_attributes) if attrs && !attrs.empty?}>" 361 361 end 362 362
