Object
# File lib/merb-helpers/form/builder.rb, line 45 45: def bound_check_box(method, attrs = {}) 46: name = control_name(method) 47: update_bound_controls(method, attrs, "checkbox") 48: unbound_check_box({:name => name}.merge(attrs)) 49: end
# File lib/merb-helpers/form/builder.rb, line 73 73: def bound_radio_group(method, arr) 74: val = control_value(method) 75: arr.map do |attrs| 76: attrs = {:value => attrs} unless attrs.is_a?(Hash) 77: attrs[:checked] = true if (val == attrs[:value]) 78: radio_group_item(method, attrs) 79: end.join 80: end
# File lib/merb-helpers/form/builder.rb, line 91 91: def bound_select(method, attrs = {}) 92: name = control_name(method) 93: update_bound_controls(method, attrs, "select") 94: unbound_select({:name => name}.merge(attrs)) 95: end
# File lib/merb-helpers/form/builder.rb, line 103 103: def bound_text_area(method, attrs = {}) 104: name = "#{@name}[#{method}]" 105: update_bound_controls(method, attrs, "text_area") 106: unbound_text_area(control_value(method), {:name => name}.merge(attrs)) 107: end
# File lib/merb-helpers/form/builder.rb, line 21 21: def fieldset(attrs, &blk) 22: legend = (l_attr = attrs.delete(:legend)) ? tag(:legend, l_attr) : "" 23: tag(:fieldset, legend + @origin.capture(&blk), attrs) 24: # @origin.concat(contents, blk.binding) 25: end
# File lib/merb-helpers/form/builder.rb, line 15 15: def form(attrs = {}, &blk) 16: captured = @origin.capture(&blk) 17: fake_method_tag = process_form_attrs(attrs) 18: tag(:form, fake_method_tag + captured, attrs) 19: end
# File lib/merb-helpers/form/builder.rb, line 119 119: def submit(value, attrs) 120: attrs[:type] ||= "submit" 121: attrs[:name] ||= "form_submit" 122: attrs[:value] ||= value 123: update_unbound_controls(attrs, "submit") 124: self_closing_tag(:input, attrs) 125: end
# File lib/merb-helpers/form/builder.rb, line 51 51: def unbound_check_box(attrs) 52: update_unbound_controls(attrs, "checkbox") 53: if attrs.delete(:boolean) 54: on, off = attrs.delete(:on), attrs.delete(:off) 55: unbound_hidden_field(:name => attrs[:name], :value => off) << 56: self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs)) 57: else 58: self_closing_tag(:input, {:type => "checkbox"}.merge(attrs)) 59: end 60: end
# File lib/merb-helpers/form/builder.rb, line 82 82: def unbound_radio_group(arr, attrs = {}) 83: arr.map do |ind_attrs| 84: ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash) 85: joined = attrs.merge(ind_attrs) 86: joined.merge!(:label => joined[:label] || joined[:value]) 87: unbound_radio_button(joined) 88: end.join 89: end
# File lib/merb-helpers/form/builder.rb, line 97 97: def unbound_select(attrs = {}) 98: update_unbound_controls(attrs, "select") 99: attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/) 100: tag(:select, options_for(attrs), attrs) 101: end
# File lib/merb-helpers/form/builder.rb, line 295 295: def add_css_class(attrs, new_class) 296: attrs[:class] = attrs[:class] ? "#{attrs[:class]} #{new_class}" : new_class 297: end
# File lib/merb-helpers/form/builder.rb, line 278 278: def considered_true?(value) 279: value && value != "false" && value != "0" && value != 0 280: end
# File lib/merb-helpers/form/builder.rb, line 282 282: def control_name(method) 283: @obj ? "#{@name}[#{method}]" : method 284: end
# File lib/merb-helpers/form/builder.rb, line 286 286: def control_value(method) 287: value = @obj ? @obj.send(method) : @origin.params[method] 288: if Merb.disabled?(:merb_helper_escaping) 289: value.to_s 290: else 291: Merb::Parse.escape_xml(value.to_s) 292: end 293: end
This can be overridden to use another method to fake out methods
# File lib/merb-helpers/form/builder.rb, line 144 144: def fake_out_method(attrs, method) 145: unbound_hidden_field(:name => "_method", :value => method) 146: end
# File lib/merb-helpers/form/builder.rb, line 250 250: def options(col, text_meth, value_meth, sel, b = nil) 251: ([b] + col.map do |item| 252: text_meth = text_meth && item.respond_to?(text_meth) ? text_meth : :last 253: value_meth = value_meth && item.respond_to?(value_meth) ? value_meth : :first 254: 255: text = item.is_a?(String) ? item : item.send(text_meth) 256: value = item.is_a?(String) ? item : item.send(value_meth) 257: 258: unless Merb.disabled?(:merb_helper_escaping) 259: text = Merb::Parse.escape_xml(text) 260: value = Merb::Parse.escape_xml(value) 261: end 262: 263: option_attrs = {:value => value} 264: if sel.is_a?(Array) 265: option_attrs.merge!(:selected => "selected") if value.in? sel.map {|e| e.to_s } 266: else 267: option_attrs.merge!(:selected => "selected") if value == sel.to_s 268: end 269: tag(:option, text, option_attrs) 270: end).join 271: end
Accepts a collection (hash, array, enumerable, your type) and returns a string of option tags. Given a collection where the elements respond to first and last (such as a two-element array), the “lasts” serve as option values and the “firsts” as option text. Hashes are turned into this form automatically, so the keys become “firsts” and values become lasts. If selected is specified, the matching “last” or element will get the selected option-tag. Selected may also be an array of values to be selected when using a multiple select.
attrs | HTML attributes and options |
selected | The value of a selected object, which may be either a string or an array. |
prompt | Adds an addtional option tag with the provided string with no value. |
include_blank | Adds an additional blank option tag with no value. |
String | HTML |
<%= options_for [["apple", "Apple Pie"], ["orange", "Orange Juice"]], :selected => "orange" => <option value="apple">Apple Pie</option><option value="orange" selected="selected">Orange Juice</option> <%= options_for [["apple", "Apple Pie"], ["orange", "Orange Juice"]], :selected => ["orange", "apple"], :prompt => "Select One" => <option value="">Select One</option><option value="apple" selected="selected">Apple Pie</option><option value="orange" selected="selected">Orange Juice</option>
# File lib/merb-helpers/form/builder.rb, line 232 232: def options_for(attrs) 233: blank, prompt = attrs.delete(:include_blank), attrs.delete(:prompt) 234: b = blank || prompt ? tag(:option, prompt || "", :value => "") : "" 235: 236: # yank out the options attrs 237: collection, selected, text_method, value_method = 238: attrs.extract!(:collection, :selected, :text_method, :value_method) 239: 240: # if the collection is a Hash, optgroups are a-coming 241: if collection.is_a?(Hash) 242: ([b] + collection.map do |g,col| 243: tag(:optgroup, options(col, text_method, value_method, selected), :label => g) 244: end).join 245: else 246: options(collection || [], text_method, value_method, selected, b) 247: end 248: end
# File lib/merb-helpers/form/builder.rb, line 129 129: def process_form_attrs(attrs) 130: method = attrs[:method] 131: 132: # Unless the method is :get, fake out the method using :post 133: attrs[:method] = :post unless attrs[:method] == :get 134: # Use a fake PUT if the object is not new, otherwise use the method 135: # passed in. Defaults to :post if no method is set. 136: method ||= (@obj.respond_to?(:new_record?) && !@obj.new_record?) ? :put : :post 137: 138: attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart 139: 140: method == :post || method == :get ? "" : fake_out_method(attrs, method) 141: end
# File lib/merb-helpers/form/builder.rb, line 273 273: def radio_group_item(method, attrs) 274: attrs.merge!(:checked => "checked") if attrs[:checked] 275: bound_radio_button(method, attrs) 276: end
# File lib/merb-helpers/form/builder.rb, line 157 157: def update_bound_check_box(method, attrs) 158: raise ArgumentError, ":value can't be used with a bound_check_box" if attrs.has_key?(:value) 159: 160: attrs[:boolean] = attrs.fetch(:boolean, true) 161: 162: val = control_value(method) 163: attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val) 164: end
# File lib/merb-helpers/form/builder.rb, line 148 148: def update_bound_controls(method, attrs, type) 149: case type 150: when "checkbox" 151: update_bound_check_box(method, attrs) 152: when "select" 153: update_bound_select(method, attrs) 154: end 155: end
# File lib/merb-helpers/form/builder.rb, line 166 166: def update_bound_select(method, attrs) 167: attrs[:value_method] ||= method 168: attrs[:text_method] ||= attrs[:value_method] || :to_s 169: attrs[:selected] ||= control_value(method) 170: end
# File lib/merb-helpers/form/builder.rb, line 185 185: def update_unbound_check_box(attrs) 186: boolean = attrs[:boolean] || (attrs[:on] && attrs[:off]) ? true : false 187: 188: case 189: when attrs.key?(:on) ^ attrs.key?(:off) 190: raise ArgumentError, ":on and :off must be specified together" 191: when (attrs[:boolean] == false) && (attrs.key?(:on)) 192: raise ArgumentError, ":boolean => false cannot be used with :on and :off" 193: when boolean && attrs.key?(:value) 194: raise ArgumentError, ":value can't be used with a boolean checkbox" 195: end 196: 197: if attrs[:boolean] = boolean 198: attrs[:on] ||= "1"; attrs[:off] ||= "0" 199: end 200: 201: attrs[:checked] = "checked" if attrs.delete(:checked) 202: end
# File lib/merb-helpers/form/builder.rb, line 172 172: def update_unbound_controls(attrs, type) 173: case type 174: when "checkbox" 175: update_unbound_check_box(attrs) 176: when "radio" 177: update_unbound_radio_button(attrs) 178: when "file" 179: @multipart = true 180: end 181: 182: attrs[:disabled] ? attrs[:disabled] = "disabled" : attrs.delete(:disabled) 183: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.