Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Injecting the form builder code into the built-in formbuilder instead…

… of creating a separate one. This is so that custom form builders that subclasses the built-in one are able to use live validations.
  • Loading branch information...
commit a544e26c1111f0806aab56de910ea72c328c7e96 1 parent 86a4c9d
@augustl authored
View
5 init.rb
@@ -7,4 +7,7 @@
ActiveRecord::Base.class_eval { include LiveValidations::ActiveRecordHooks }
# Hook view helpers
-ActionView::Base.class_eval { include LiveValidations::ViewHelpers }
+ActionView::Base.class_eval { include LiveValidations::ViewHelpers }
+
+## Hook into the default form builder
+ActionView::Helpers::FormBuilder.class_eval { include LiveValidations::FormBuilder }
View
1  lib/live_validations/adapter.rb
@@ -65,7 +65,6 @@ def prefix
end
def handle_form_for_options(options)
- options.merge!(:builder => LiveValidations::FormBuilder)
self.class.form_for_options_proc.call(options) if self.class.form_for_options_proc
end
View
95 lib/live_validations/form_builder.rb
@@ -1,52 +1,59 @@
module LiveValidations
- class FormBuilder < ActionView::Helpers::FormBuilder
- # Where the html options are foo_field(:fieldname, :html => {})
- helpers_with_one_option_hash = field_helpers + %w(hidden_field fields_for) - %w(label)
-
- # Where the HTML options are foo_field(:fieldname, {:options => 'here'}, {:html_options => 'here'})
- helpers_with_two_option_hashes = %w(date_select datetime_select time_select) +
- %w(collection_select select country_select time_zone_select)
-
-
- helpers_with_one_option_hash.each do |helper|
- define_method(helper) do |attribute, *args|
- tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
+ module FormBuilder
+ def self.included(base)
+ base.instance_eval {
+ # Where the html options are foo_field(:fieldname, :html => {})
+ helpers_with_one_option_hash = field_helpers - %w(label apply_form_for_options! fields_for)
- if tag_attributes
- options = args.extract_options!
- options.merge!(tag_attributes)
- super(attribute, *(args << options))
- else
- super
- end
- end
- end
-
- helpers_with_two_option_hashes.each do |helper|
- define_method(helper) do |attribute, *args|
- tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
+ # Where the HTML options are foo_field(:fieldname, {:options => 'here'}, {:html_options => 'here'})
+ helpers_with_two_option_hashes = %w(date_select datetime_select time_select) +
+ %w(collection_select select time_zone_select)
- if tag_attributes
- # We have both options and html_options
- if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
- html_options = args.pop
- options = args.pop
-
- html_options.merge!(tag_attributes)
- args << options
- args << html_options
- super(attribute, *args)
- # No html_options was specified
- else
- html_options = tag_attributes
- args << {} unless args[-1].is_a?(Hash)
- args << html_options
- super(attribute, *args)
+ helpers_with_one_option_hash.each do |helper|
+ define_method("#{helper}_with_live_validations") do |attribute, *args|
+ tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
+
+ if tag_attributes
+ options = args.extract_options!
+ options.merge!(tag_attributes)
+ __send__("#{helper}_without_live_validations", attribute, *(args << options))
+ else
+ __send__("#{helper}_without_live_validations", attribute, *args)
+ end
end
- else
- super
+
+ alias_method_chain helper, "live_validations"
+ end
+
+ helpers_with_two_option_hashes.each do |helper|
+ define_method("#{helper}_with_live_validations") do |attribute, *args|
+ tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
+
+ if tag_attributes
+ # We have both options and html_options
+ if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
+ html_options = args.pop
+ options = args.pop
+
+ html_options.merge!(tag_attributes)
+ args << options
+ args << html_options
+ __send__("#{helper}_without_live_validations", *args)
+ # No html_options was specified
+ else
+ html_options = tag_attributes
+ args << {} unless args[-1].is_a?(Hash)
+ args << html_options
+ __send__("#{helper}_without_live_validations", attribute, *args)
+ end
+ else
+ __send__("#{helper}_without_live_validations", attribute, *args)
+ end
+ end
+
+ alias_method_chain helper, "live_validations"
end
- end
+ }
end
end
end

0 comments on commit a544e26

Please sign in to comment.
Something went wrong with that request. Please try again.