lib/merb-helpers/form/builder.rb in merb-helpers-0.9.8 vs lib/merb-helpers/form/builder.rb in merb-helpers-0.9.9
- old
+ new
@@ -8,14 +8,10 @@
def initialize(obj, name, origin)
@obj, @origin = obj, origin
@name = name || @obj.class.name.snake_case.split("/").last
end
- def concat(attrs, &blk)
- @origin.concat(@origin.capture(&blk), blk.binding)
- end
-
def form(attrs = {}, &blk)
captured = @origin.capture(&blk)
fake_method_tag = process_form_attrs(attrs)
tag(:form, fake_method_tag + captured, attrs)
end
@@ -29,11 +25,14 @@
%w(text password hidden file).each do |kind|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def bound_#{kind}_field(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "#{kind}")
- unbound_#{kind}_field({:name => name, :value => @obj.send(method)}.merge(attrs))
+ unbound_#{kind}_field({
+ :name => name,
+ :value => control_value(method)
+ }.merge(attrs))
end
def unbound_#{kind}_field(attrs)
update_unbound_controls(attrs, "#{kind}")
self_closing_tag(:input, {:type => "#{kind}"}.merge(attrs))
@@ -59,20 +58,20 @@
end
def bound_radio_button(method, attrs = {})
name = control_name(method)
update_bound_controls(method, attrs, "radio")
- unbound_radio_button({:name => name, :value => @obj.send(method)}.merge(attrs))
+ unbound_radio_button({:name => name, :value => control_value(method)}.merge(attrs))
end
def unbound_radio_button(attrs)
update_unbound_controls(attrs, "radio")
self_closing_tag(:input, {:type => "radio"}.merge(attrs))
end
def bound_radio_group(method, arr)
- val = @obj.send(method)
+ val = control_value(method)
arr.map do |attrs|
attrs = {:value => attrs} unless attrs.is_a?(Hash)
attrs[:checked] ||= (val == attrs[:value])
radio_group_item(method, attrs)
end.join
@@ -100,11 +99,11 @@
end
def bound_text_area(method, attrs = {})
name = "#{@name}[#{method}]"
update_bound_controls(method, attrs, "text_area")
- unbound_text_area(@obj.send(method), {:name => name}.merge(attrs))
+ unbound_text_area(control_value(method), {:name => name}.merge(attrs))
end
def unbound_text_area(contents, attrs)
update_unbound_controls(attrs, "text_area")
tag(:textarea, contents, attrs)
@@ -156,18 +155,18 @@
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] = attrs.fetch(:boolean, true)
- val = @obj.send(method)
+ val = control_value(method)
attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val)
end
def update_bound_select(method, attrs)
attrs[:value_method] ||= method
attrs[:text_method] ||= attrs[:value_method] || :to_s
- attrs[:selected] ||= @obj.send(attrs[:value_method])
+ attrs[:selected] ||= control_value(attrs[:value_method])
end
def update_unbound_controls(attrs, type)
case type
when "checkbox"
@@ -266,12 +265,16 @@
def considered_true?(value)
value && value != "0" && value != 0
end
def control_name(method)
- "#{@name}[#{method}]"
+ @obj ? "#{@name}[#{method}]" : method
end
+
+ def control_value(method)
+ @obj ? @obj.send(method) : @origin.params[method]
+ end
def add_css_class(attrs, new_class)
attrs[:class] = attrs[:class] ? "#{attrs[:class]} #{new_class}" : new_class
end
end
@@ -376,10 +379,10 @@
end
private
def update_bound_controls(method, attrs, type)
- if @obj.errors.on(method.to_sym)
+ if @obj && !@obj.errors.on(method.to_sym).blank?
add_css_class(attrs, "error")
end
super
end
end