Skip to content

adding radio buttons #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
125 changes: 80 additions & 45 deletions lib/enum/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,93 @@ 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
end
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 << "<label>#{enum} "
tag_text << "</label>"
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