lib/merb_helpers/form/builder.rb in merb_helpers-0.9.5 vs lib/merb_helpers/form/builder.rb in merb_helpers-0.9.6

- old
+ new

@@ -31,12 +31,12 @@ method = attrs[:method] # Unless the method is :get, fake out the method using :post attrs[:method] = :post unless attrs[:method] == :get # Use a fake PUT if the object is not new, otherwise use the method - # passed in. - method ||= (@obj && !@obj.new_record? ? :put : :post) + # passed in. Defaults to :post if no method is set. + method ||= (@obj.respond_to?(:new_record?) && !@obj.new_record?) || (@obj.respond_to?(:new?) && !@obj.new?) ? :put : :post attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart method == :post || method == :get ? "" : fake_out_method(attrs, method) end @@ -66,11 +66,11 @@ end def update_bound_check_box(method, attrs) raise ArgumentError, ":value can't be used with a bound_check_box" if attrs.has_key?(:value) - attrs[:boolean] ||= true + attrs[:boolean] = attrs.fetch(:boolean, true) val = @obj.send(method) attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val) end @@ -166,10 +166,11 @@ unbound_select({:name => name}.merge(attrs)) end def unbound_select(attrs = {}) update_unbound_controls(attrs, "select") + attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/) tag(:select, options_for(attrs), attrs) end def bound_radio_group(method, arr) val = @obj.send(method) @@ -178,10 +179,19 @@ attrs[:checked] ||= (val == attrs[:value]) radio_group_item(method, attrs) end.join end + def unbound_radio_group(arr, attrs = {}) + arr.map do |ind_attrs| + ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash) + joined = attrs.merge(ind_attrs) + joined.merge!(:label => joined[:label] || joined[:value]) + unbound_radio_button(joined) + end.join + end + def unbound_text_area(contents, attrs) update_unbound_controls(attrs, "text_area") tag(:textarea, contents, attrs) end @@ -275,26 +285,18 @@ add_css_class(attrs, type) end super end - # Provides a generic HTML label. - # - # ==== Parameters - # attrs<Hash>:: HTML attributes - # - # ==== Returns - # String:: HTML - # - # ==== Example - # <%= label :for => "name", :label => "Full Name" %> - # => <label for="name">Full Name</label> - def label(attrs) - attrs ||= {} - for_attr = attrs[:id] ? {:for => attrs[:id]} : {} - if label_text = attrs.delete(:label) - tag(:label, label_text, for_attr) + def label(contents, attrs = {}) + if contents.is_a?(Hash) + attrs = contents + contents = attrs.delete(:label) + end + if contents + for_attr = attrs[:id] ? {:for => attrs[:id]} : {} + tag(:label, contents, for_attr) else "" end end @@ -375,10 +377,10 @@ include Errorifier end module Resourceful def process_form_attrs(attrs) - attrs[:action] ||= url(@name, @obj) if @origin + attrs[:action] ||= @origin.url(@name, @obj) if @origin super end end class ResourcefulForm < Form