Created
January 16, 2012 17:41
-
-
Save mdarby/1621973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CcwFormBuilder < ActionView::Helpers::FormBuilder | |
helpers = field_helpers + | |
%w(date_select datetime_select time_select) + | |
%w(collection_select select country_select time_zone_select) - | |
%w(hidden_field label fields_for) | |
helpers.each do |name| | |
define_method(name) do |field, *args| | |
options = args.extract_options! | |
html_options = args.extract_options! | |
# Get a handle on the class that the form is for. | |
klass = object_name.to_s.classify.constantize | |
# Figure out if the field we're dealing with is a required field | |
mandatory = defined?(klass::REQ_ATTR) && klass::REQ_ATTR.include?(field.to_s) ? :mandatory : nil | |
# If we're dealing with a select box | |
if name =~ /select$/ | |
if mandatory | |
# Manually delete the :include_blank select option if the field is mandatory | |
html_options.delete(:include_blank) if html_options.has_key?(:include_blank) | |
options.delete(:include_blank) if options.has_key?(:include_blank) | |
else | |
# Manually include a blank entry if the field is optional | |
html_options.merge!(:include_blank => true) if name == "collection_select" | |
options.merge!(:include_blank => true) if name == "select" | |
end | |
end | |
# I like to have smaller text_areas than the massive default | |
if name == "text_area" | |
options.delete(:size) if options.has_key?(:size) | |
options.merge!(:size => "50x10") | |
end | |
# The default label is the field's name, unless options[:label] exists | |
if options.has_key?(:label) | |
label_text = options[:label] | |
# Nix the label option so that it isn't rendered in the source | |
options.delete(:label) | |
else | |
label_text = field.to_s | |
end | |
if options.has_key?(:message) | |
msg = "<div class='input_message'>#{options.delete(:message)}</div>".html_safe | |
end | |
# Push the html_options hash onto the end of the args list | |
args.push(html_options) unless html_options.blank? | |
# Push the options hash onto the end of the args list | |
args.push(options) | |
# Generate a label field for the form control | |
label_field = "<label for=\"#{object_name}_#{field}\"></label>".html_safe | |
# Push a "box" into the template with selective styling and title | |
@template.box(label_text, mandatory) do | |
@template.concat(label_field) | |
@template.concat(msg) if msg.present? | |
@template.concat(super(field, *args)) | |
end | |
end | |
end | |
def errors | |
if error_messages.present? | |
errors = error_messages(:header_message => nil) | |
@template.error_box("Something is awry…".html_safe, :content => errors) | |
end | |
end | |
end | |
## It would be used like this: | |
= form_for(@job, :url => jobs_path, :builder => CcwFormBuilder) do |f| | |
= f.errors | |
= f.text_field :name, :size => 50, :label => 'name' | |
= f.collection_select :client_contact_company_id, ContactCompany.nicknamed, :id, :display_name, {:include_blank => true}, :label => 'client' | |
= f.collection_select :parent_job_id, Job.top_level, :id, :plain_display_name, {:include_blank => true}, :label => 'parent job' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment