Skip to content

Instantly share code, notes, and snippets.

@zykadelic
Forked from calvincorreli/set_default_form_builder.rb
Last active December 24, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zykadelic/6807022 to your computer and use it in GitHub Desktop.
Save zykadelic/6807022 to your computer and use it in GitHub Desktop.
Set the default FormBuilder without having to restart the server on each change made to the DefaultFormBuilder class
# in app/helpers
class DefaultFormBuilder < ActionView::Helpers::FormBuilder
# Example of an added method that wraps custom textfields, where type can be
# :email_field, :text_field, etc.
def textfield(type, method, attributes = {})
@template.content_tag :div, class: 'textfield' do
@template.send(type, @object_name, method, objectify_options(attributes))
end
end
# Example of overriding an existing method that wraps submit buttons and uses
# the button tag instead of the input tag for the button
def submit(value = nil, options = {})
value, options = nil, value if value.is_a?(Hash)
value ||= submit_default_value
@template.button_tag(value, options)
end
end
# in config/initializers
module ActionView
module Helpers
module FormHelper
def form_for_with_defaults(record, options = {}, &proc)
form_for_without_defaults record, options.reverse_merge(builder: DefaultFormBuilder), &proc
end
def fields_for_with_defaults(record_name, record_object = nil, options = {}, &block)
fields_for_without_defaults record_name, record_object, options.reverse_merge(builder: DefaultFormBuilder), &block
end
alias_method_chain :form_for, :defaults
alias_method_chain :fields_for, :defaults
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment