diff --git a/README.txt b/README.txt index c55cd5a..ef81b66 100644 --- a/README.txt +++ b/README.txt @@ -58,3 +58,11 @@ In views: <%= f.enum_select :severity %> <% end %> + To use radio buttons: + + <%= enum_radio(@enumeration, 'severity')%> + or + <%= form_for @enumeration do |f| %> + <%= f.label :severity %> + <%= f.enum_radio :severity %> + <% end %> diff --git a/lib/enum/active_record_helper.rb b/lib/enum/active_record_helper.rb index 0f3e3a5..27c195f 100644 --- a/lib/enum/active_record_helper.rb +++ b/lib/enum/active_record_helper.rb @@ -6,6 +6,7 @@ def field_type_with_enumerated_attribute return (@field_type = :enum_select) if type == :enum field_type_without_enumerated_attribute end + alias_method_chain :field_type, :enumerated_attribute end end @@ -13,51 +14,85 @@ def field_type_with_enumerated_attribute end if defined?(ActionView::Base) - module ActionView - module Helpers - - #form_options_helper.rb - module FormOptionsHelper - #def select - def enum_select(object, method, options={}, html_options={}) - InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options) - end - end - - class InstanceTag - def to_enum_select_tag(options, html_options={}) - if self.object.respond_to?(method_name.to_sym) + module ActionView + module Helpers + + #form_options_helper.rb + module FormOptionsHelper + #def select + def enum_select(object, method, options={}, html_options={}) + InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options) + end + + def enum_radio(object, method, options={}, html_options={}) + InstanceTag.new(object, method, self, options.delete(:object)).to_enum_radio_tag(options, html_options) + end + end + + class InstanceTag + def to_enum_select_tag(options, html_options={}) + if self.object.respond_to?(method_name.to_sym) column = self.object.column_for_attribute(method_name) - if (value = self.object.__send__(method_name.to_sym)) - options[:selected] ||= value.to_s - else + if (value = self.object.__send__(method_name.to_sym)) + options[:selected] ||= value.to_s + else options[:include_blank] = column.null if options[:include_blank].nil? - end - end - to_select_tag(column.limit, options, html_options) - end - - #initialize record_name, method, self - if respond_to?(:to_tag) - def to_tag_with_enumerated_attribute(options={}) - #look for an enum - if (column_type == :enum && self.object.class.respond_to?(method_name.to_sym)) - to_enum_select_tag(options) - else - to_tag_without_enumerated_attribute(options) - end - end - alias_method_chain :to_tag, :enumerated_attribute - end - - end - - class FormBuilder - def enum_select(method, options={}, html_options={}) - @template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options)) - end - end - - end - end + end + end + to_select_tag(column.limit, options, html_options) + end + + def to_enum_radio_tag(options, html_options={}) + if self.object.respond_to?(method_name.to_sym) + column = self.object.column_for_attribute(method_name) + if (value = self.object.__send__(method_name.to_sym)) + options[:selected] ||= value.to_s + else + options[:include_blank] = column.null if options[:include_blank].nil? + end + end + values = column.limit + raise ArgumentError, "No values for enum select tag" unless values + tag_text = '' + template = options.dup + template.delete(:selected) + values.each do |enum| + opts = template.dup + opts['checked'] = 'checked' if options[:selected] and options[:selected] == enum + opts['id'] = "#{opts['id']}_#{enum}" + tag_text << to_radio_button_tag(enum, opts) + tag_text << "" + end + tag_text.html_safe + end + + #initialize record_name, method, self + if respond_to?(:to_tag) + def to_tag_with_enumerated_attribute(options={}) + #look for an enum + if (column_type == :enum && self.object.class.respond_to?(method_name.to_sym)) + to_enum_select_tag(options) + else + to_tag_without_enumerated_attribute(options) + end + end + + alias_method_chain :to_tag, :enumerated_attribute + end + + end + + class FormBuilder + def enum_select(method, options={}, html_options={}) + @template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options)) + end + + def enum_radio(method, options={}, html_options={}) + @template.enum_radio(@object_name, method, objectify_options(options), @default_options.merge(html_options)) + end + end + + end + end end \ No newline at end of file