Permalink
Browse files

implemented RadioInput off NewBase (i18n label specs aren't passing yet)

  • Loading branch information...
1 parent b9bb864 commit aee41a965ac49e275fb96ed37cd6c2c2a2bafbfa @justinfrench committed Mar 17, 2011
Showing with 61 additions and 33 deletions.
  1. +0 −1 lib/formtastic/form_builder.rb
  2. +1 −1 lib/formtastic/helpers/inputs_helper.rb
  3. +60 −31 lib/formtastic/inputs/radio_input.rb
View
1 lib/formtastic/form_builder.rb
@@ -69,7 +69,6 @@ def self.configure(name, value = nil)
include Formtastic::Helpers::ErrorsHelper
include Formtastic::Inputs::CheckBoxesInput
- include Formtastic::Inputs::RadioInput
end
View
2 lib/formtastic/helpers/inputs_helper.rb
@@ -247,7 +247,7 @@ def input(method, options = {})
options[:as] ||= default_input_type(method, options)
- [:string, :country, :boolean, :time_zone, :date, :datetime, :time, :select, :phone, :search, :numeric, :email, :file, :hidden, :password, :text, :url].each do |i|
+ [:string, :country, :boolean, :time_zone, :date, :datetime, :time, :select, :phone, :search, :numeric, :email, :file, :hidden, :password, :text, :url, :radio].each do |i|
klass = "Formtastic::Inputs::#{options[:as].to_s.camelize}Input".constantize
return klass.new(self, template, @object, @object_name, method, options).to_html if options[:as] == i
end
View
91 lib/formtastic/inputs/radio_input.rb
@@ -1,3 +1,6 @@
+require 'inputs/new_base'
+require 'inputs/new_base/collections'
+
module Formtastic
module Inputs
@@ -105,41 +108,67 @@ module Inputs
#
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
# @see Formtastic::Inputs::Select RadioInput as an alternative for `belongs_to` associations
- module RadioInput
- include Formtastic::Inputs::Base
+ class RadioInput
+ include NewBase
+ include NewBase::Collections
- # Finally, you can set :value_as_class => true if you want the li wrapper around each radio
- # button / label combination to contain a class with the value of the radio button (useful for
- # applying specific CSS or Javascript to a particular radio button).
- def radio_input(method, options)
- collection = find_collection_for_column(method, options)
- html_options = strip_formtastic_options(options).merge(options.delete(:input_html) || {})
-
- input_name = generate_association_input_name(method)
- value_as_class = options.delete(:value_as_class)
- input_ids = []
-
- list_item_content = collection.map do |c|
- label = c.is_a?(Array) ? c.first : c
- value = c.is_a?(Array) ? c.last : c
- input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
- input_ids << input_id
-
- html_options[:id] = input_id
-
- li_content = template.content_tag(:label,
- Formtastic::Util.html_safe("#{radio_button(input_name, value, html_options)} #{escape_html_entities(label)}"),
- :for => input_id
+ def to_html
+ input_wrapping do
+ template.content_tag(:fieldset,
+ legend_html <<
+ template.content_tag(:ol,
+ collection.map { |choice|
+
+ label = choice.is_a?(Array) ? choice.first : choice
+ value = choice.is_a?(Array) ? choice.last : choice
+
+ html_safe_value = value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase
+ radio_input_id = "#{sanitized_object_name}_#{input_name}_#{html_safe_value}"
+ radio_input_id = "#{builder.custom_namespace}_#{radio_input_id}" unless builder.custom_namespace.blank?
+
+ template.content_tag(:li,
+ template.content_tag(:label,
+ builder.radio_button(input_name, value, input_html_options.merge(:id => radio_input_id)) << label,
+ :for => radio_input_id
+ ),
+ :class => (value_as_class? ? "#{sanitized_method_name}_#{html_safe_value}" : nil)
+ )
+ }.join.html_safe
+ )
)
-
- li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
- template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options)
end
-
- template.content_tag(:fieldset,
- legend_tag(method, options) << template.content_tag(:ol, Formtastic::Util.html_safe(list_item_content.join))
- )
end
+
+ def value_as_class?
+ options[:value_as_class]
+ end
+
+ def legend_html
+ if options[:label] == false
+ ""
+ else
+ template.content_tag(:legend,
+ template.content_tag(:label, label_text, label_html_options),
+ :class => "label"
+ )
+ end
+ end
+
+ # Override to remove the for attribute since this isn't associated with any element, as it's
+ # nested inside the legend.
+ def label_html_options
+ super.merge(:for => nil)
+ end
+
+ # TODO move to NewBase
+ # TODO i18n
+ # abbr tag from config
+ def label_text
+ text = method.to_s.humanize
+ text << template.content_tag(:abbr, "*")
+ text.html_safe
+ end
+
end
end
end

0 comments on commit aee41a9

Please sign in to comment.