Created
June 7, 2010 03:40
-
-
Save hakanai/428201 to your computer and use it in GitHub Desktop.
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
#config/initializers/application_form_builder.rb: | |
ActionView::Base.class_eval do | |
def self.default_form_builder | |
Forms::ApplicationFormBuilder | |
end | |
end | |
#helpers/organisations_helper.rb: | |
class OrganisationsHelper | |
# ...elided... | |
end | |
Forms::ApplicationFormBuilder.graft(OrganisationsHelper) | |
#lib/forms/application_form_builder.rb: | |
# Customised FormBuilder which adds the ability to customise its methods. | |
# The actual customisations are in module Forms::ApplicationHelper. | |
class Forms::ApplicationFormBuilder < ActionView::Helpers::FormBuilder | |
# Grafts methods in a module onto the helper so that they can be called from a template. | |
def self.graft(grafted_module) | |
# Copied from FormBuilder. FormBuilder looks like it has some bright | |
# engineering ideas but never finished implementing them. This *should* | |
# be automated by defining "self.field_helpers", but it's used before | |
# this class is loaded. | |
grafted_module.instance_methods.each do |selector| | |
src = <<-end_src | |
def #{selector}(method, options = {}) | |
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options)) | |
end | |
end_src | |
class_eval src, __FILE__, __LINE__ | |
end | |
end | |
# This particular helper we graft on always. | |
graft(Forms::ApplicationHelper) | |
private | |
def objectify_options(options) | |
@default_options.merge(options.merge(:object => @object)) | |
end | |
end | |
#lib/forms/application_helper.rb: | |
module Forms::ApplicationHelper | |
# date_field helper, delegate to the one we already have. | |
def date_field(object_name, method, options = {}) | |
super(object_name, method, options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment