Skip to content
This repository
Browse code

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
August Lilleaas authored
5 init.rb
@@ -7,4 +7,7 @@
7 7 ActiveRecord::Base.class_eval { include LiveValidations::ActiveRecordHooks }
8 8
9 9 # Hook view helpers
10   -ActionView::Base.class_eval { include LiveValidations::ViewHelpers }
  10 +ActionView::Base.class_eval { include LiveValidations::ViewHelpers }
  11 +
  12 +## Hook into the default form builder
  13 +ActionView::Helpers::FormBuilder.class_eval { include LiveValidations::FormBuilder }
1  lib/live_validations/adapter.rb
@@ -65,7 +65,6 @@ def prefix
65 65 end
66 66
67 67 def handle_form_for_options(options)
68   - options.merge!(:builder => LiveValidations::FormBuilder)
69 68 self.class.form_for_options_proc.call(options) if self.class.form_for_options_proc
70 69 end
71 70
95 lib/live_validations/form_builder.rb
... ... @@ -1,52 +1,59 @@
1 1 module LiveValidations
2   - class FormBuilder < ActionView::Helpers::FormBuilder
3   - # Where the html options are foo_field(:fieldname, :html => {})
4   - helpers_with_one_option_hash = field_helpers + %w(hidden_field fields_for) - %w(label)
5   -
6   - # Where the HTML options are foo_field(:fieldname, {:options => 'here'}, {:html_options => 'here'})
7   - helpers_with_two_option_hashes = %w(date_select datetime_select time_select) +
8   - %w(collection_select select country_select time_zone_select)
9   -
10   -
11   - helpers_with_one_option_hash.each do |helper|
12   - define_method(helper) do |attribute, *args|
13   - tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
  2 + module FormBuilder
  3 + def self.included(base)
  4 + base.instance_eval {
  5 + # Where the html options are foo_field(:fieldname, :html => {})
  6 + helpers_with_one_option_hash = field_helpers - %w(label apply_form_for_options! fields_for)
14 7
15   - if tag_attributes
16   - options = args.extract_options!
17   - options.merge!(tag_attributes)
18   - super(attribute, *(args << options))
19   - else
20   - super
21   - end
22   - end
23   - end
24   -
25   - helpers_with_two_option_hashes.each do |helper|
26   - define_method(helper) do |attribute, *args|
27   - tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
  8 + # Where the HTML options are foo_field(:fieldname, {:options => 'here'}, {:html_options => 'here'})
  9 + helpers_with_two_option_hashes = %w(date_select datetime_select time_select) +
  10 + %w(collection_select select time_zone_select)
28 11
29   - if tag_attributes
30   - # We have both options and html_options
31   - if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
32   - html_options = args.pop
33   - options = args.pop
34   -
35   - html_options.merge!(tag_attributes)
36   - args << options
37   - args << html_options
38   - super(attribute, *args)
39   - # No html_options was specified
40   - else
41   - html_options = tag_attributes
42   - args << {} unless args[-1].is_a?(Hash)
43   - args << html_options
44   - super(attribute, *args)
  12 + helpers_with_one_option_hash.each do |helper|
  13 + define_method("#{helper}_with_live_validations") do |attribute, *args|
  14 + tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
  15 +
  16 + if tag_attributes
  17 + options = args.extract_options!
  18 + options.merge!(tag_attributes)
  19 + __send__("#{helper}_without_live_validations", attribute, *(args << options))
  20 + else
  21 + __send__("#{helper}_without_live_validations", attribute, *args)
  22 + end
45 23 end
46   - else
47   - super
  24 +
  25 + alias_method_chain helper, "live_validations"
  26 + end
  27 +
  28 + helpers_with_two_option_hashes.each do |helper|
  29 + define_method("#{helper}_with_live_validations") do |attribute, *args|
  30 + tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
  31 +
  32 + if tag_attributes
  33 + # We have both options and html_options
  34 + if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
  35 + html_options = args.pop
  36 + options = args.pop
  37 +
  38 + html_options.merge!(tag_attributes)
  39 + args << options
  40 + args << html_options
  41 + __send__("#{helper}_without_live_validations", *args)
  42 + # No html_options was specified
  43 + else
  44 + html_options = tag_attributes
  45 + args << {} unless args[-1].is_a?(Hash)
  46 + args << html_options
  47 + __send__("#{helper}_without_live_validations", attribute, *args)
  48 + end
  49 + else
  50 + __send__("#{helper}_without_live_validations", attribute, *args)
  51 + end
  52 + end
  53 +
  54 + alias_method_chain helper, "live_validations"
48 55 end
49   - end
  56 + }
50 57 end
51 58 end
52 59 end

0 comments on commit a544e26

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