Permalink
Browse files

Wrapping up last changes to make radio_input work properly.

Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information...
josevalim authored and justinfrench committed Mar 6, 2009
1 parent b46532b commit f2973cbb81e774897b1b4d1a0442af0318548d19
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/formtastic.rb
View
@@ -440,11 +440,17 @@ def formatted_collection(collection, label_method, value_method = :id) #:nodoc:
# f.input :author, :as => :radio, :label_method => :display_name
# f.input :author, :as => :radio, :label_method => :to_s
# f.input :author, :as => :radio, :label_method => :label
+ #
+ # Finally, you can set :value_as_class => true if you want that LI wrappers
+ # contains a class with the wrapped radio input value. This is used by
+ # <tt>boolean_radio_input</tt> and you can see an example there.
+ #
def radio_input(method, options)
options[:collection] ||= find_parent_objects_for_column(method)
options[:label_method] ||= detect_label_method(options[:collection])
input_name = generate_association_input_name(method)
+ value_as_class = options.delete(:value_as_class)
choices = formatted_collection(options[:collection], options[:label_method])
template.content_tag(:fieldset,
@@ -454,13 +460,13 @@ def radio_input(method, options)
label = (!c.instance_of?(String)) ? c.first : c
value = (!c.instance_of?(String)) ? c.last : c
- template.content_tag(:li,
- template.content_tag(:label,
- "#{template.radio_button(@object_name, input_name, value, set_options(options))} #{label}",
- :for => generate_html_id(input_name, value)
- ),
- :class => value.to_s
+ li_content = template.content_tag(:label,
+ "#{template.radio_button(@object_name, input_name, value, set_options(options))} #{label}",
+ :for => generate_html_id(input_name, value)
)
+
+ li_options = value_as_class ? { :class => value.to_s } : {}
+ template.content_tag(:li, li_content, li_options)
}
)
)
@@ -656,7 +662,7 @@ def boolean_radio_input(method, options)
options[:false] ||= I18n.t('no', :default => 'No', :scope => [:formtastic]).send(@@label_str_method)
choices = { options.delete(:true) => true, options.delete(:false) => false }
- radio_input(method, options.merge(:collection => choices))
+ radio_input(method, { :collection => choices, :value_as_class => true }.merge(options))
end
def inline_errors(method, options) #:nodoc:

0 comments on commit f2973cb

Please sign in to comment.