Skip to content

Instantly share code, notes, and snippets.

@caironoleto
Forked from ramon/form_builder.rb
Created February 15, 2009 17:18
Show Gist options
  • Save caironoleto/64775 to your computer and use it in GitHub Desktop.
Save caironoleto/64775 to your computer and use it in GitHub Desktop.
module Imoby
class FormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(label check_box radio_button fields_for form_for hidden_field radio_sym select_sym)).each do |selector|
src = <<-end_src
def #{selector}(method, options = {})
make_default_template method,
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
end
end_src
class_eval src, __FILE__, __LINE__
end
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
@template.content_tag("div",
@template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value) +
' ' + label(method, nil, :class => "inline") +
error_message_on(method),
:class => error_class(method))
end
def select(method, choices, options = {}, html_options = {})
make_default_template method,
@template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
end
def select_sym(method, choices = nil, options = {}, html_options = {})
make_default_template method,
@template.select_sym(@object_name, method, choices, options, html_options)
end
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
make_default_template method,
@template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
end
def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
make_default_template method,
@template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options))
end
def date_select(method, options = {}, html_options = {})
make_default_template method,
@template.date_select(@object_name, method, objectify_options(options), html_options)
end
def time_select(method, options = {}, html_options = {})
make_default_template method,
@template.time_select(@object_name, method, objectify_options(options), html_options)
end
def datetime_select(method, options = {}, html_options = {})
make_default_template method,
@template.datetime_select(@object_name, method, objectify_options(options), html_options)
end
def radio_sym(method, choices = nil, options = {})
make_default_template method,
@template.radio_sym(@object_name, method, choices, options)
end
def label(method, text = nil, options = {})
model = /([^\[]+)/.match(@object_name.to_s.singularize)
text = I18n.t("#{model[1]}.#{method}", :scope => "activerecord.models.attributes") if text == nil
@template.label(@object_name, method, text, objectify_options(options))
end
def error_message_on(method, *args)
options = args.extract_options!
options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => '')
@template.error_message_on(@object, method, options)
end
private
def make_default_template(method, method_template)
@template.content_tag("div",
label(method) +
method_template +
error_message_on(method),
:class => error_class(method))
end
def error_class(method)
"error" unless @object.errors.on(method).blank?
end
end
end
module ActionView
module Helpers
class InstanceTag
def to_radio_sym_tag(choices, options)
choices = symbolize_values(choices)
raise ArgumentError, "No values for radio tag" unless choices
add_default_name_and_id(options)
v = value(object)
tag_text = ''
template = options.dup
template.delete('checked')
choices.each do |choice|
opts = template.dup
opts['checked'] = 'checked' if v and v == choice[1]
opts['id'] = "#{opts['id']}_#{choice[1]}"
tag_text << to_radio_button_tag(choice[1], opts)
tag_text << " <label for=\"#{opts['id']}\" class=\"inline\">#{choice[0]}</label>"
end
tag_text
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment