Forked from calvincorreli/set_default_form_builder.rb
Last active
December 24, 2015 13:39
-
-
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
This file contains 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
# 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 |
This file contains 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
# 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