/
form_helper.rb
133 lines (111 loc) · 4.56 KB
/
form_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
module ClientSideValidations::ActionView::Helpers
module FormHelper
class Error < StandardError; end
def form_for(record, *args, &proc)
options = args.extract_options!
if options[:validate]
# Always turn off HTML5 Validations
options[:html] ||= {}
options[:html][:novalidate] = 'novalidate'
case record
when String, Symbol
raise ClientSideValidations::ActionView::Helpers::FormHelper::Error, 'Using form_for(:name, @resource) is not supported with ClientSideValidations. Please use form_for(@resource, :as => :name) instead.'
else
object = record.is_a?(Array) ? record.last : record
end
end
@validators = {}
# Order matters here. Rails mutates the options object
html_id = options[:html][:id] if options[:html]
form = super(record, *(args << options), &proc)
options[:id] = html_id if html_id
script = client_side_form_settings(object, options)
# Because of the load order requirement above this sub is necessary
# Would be nice to not do this
script = insert_validators_into_script(script)
if assign_script_to_content_for(options[:validate], script)
form.html_safe
else
"#{form}#{script}".html_safe
end
end
def assign_script_to_content_for(name, script)
if name && name != true
content_for(name) { script.html_safe }
true
end
end
def apply_form_for_options!(object_or_array, options)
super
options[:html][:validate] = true if options[:validate]
end
def fields_for(record_or_name_or_array, record_object = nil, options = {}, &block)
output = super
if @validators
options[:validators].each do |key, value|
if @validators.key?(key)
@validators[key].merge! value
else
@validators[key] = value
end
end
end
output
end
private
def insert_validators_into_script(script)
# There is probably a more performant way of doing this
# But using String#sub has some issues. Undocumented "features"
if script
script = script.split(/"validator_hash"/)
script = "#{script[0]}#{construct_validators.to_json}#{script[1]}"
end
script
end
def construct_validators
@validators.inject({}) do |validator_hash, object_opts|
option_hash = object_opts[1].inject({}) do |option_hash, attr|
option_hash.merge!(attr[0] => attr[1][:options])
end
if object_opts[0].respond_to?(:client_side_validation_hash)
validation_hash = object_opts[0].client_side_validation_hash(option_hash)
else
validation_hash = {}
end
option_hash.each_key do |attr|
if validation_hash[attr]
validator_hash.merge!(object_opts[1][attr][:name] => validation_hash[attr])
end
end
validator_hash
end
end
def client_side_form_settings(object, options)
if options[:validate]
builder = options[:parent_builder]
if options[:id]
var_name = options[:id]
else
if Rails.version >= '3.2.0'
var_name = if object.respond_to?(:persisted?) && object.persisted?
options[:as] ? "edit_#{options[:as]}" : [options[:namespace], dom_id(object, :edit)].compact.join("_")
else
options[:as] ? "new_#{options[:as]}" : [options[:namespace], dom_id(object)].compact.join("_")
end
else
# This is to maintain backward compatibility with Rails 3.1
# see: https://github.com/rails/rails/commit/e29773f885fd500189ffd964550ae20061d745ba#commitcomment-948052
var_name = if object.respond_to?(:persisted?) && object.persisted?
options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
else
options[:as] ? "#{options[:as]}_new" : dom_id(object)
end
end
end
content_tag(:script) do
"//<![CDATA[\nif(window.ClientSideValidations==undefined)window.ClientSideValidations={};if(window.ClientSideValidations.remote_validators_prefix==undefined)window.ClientSideValidations.remote_validators_prefix='#{(root_path||"").sub(/(\/)+$/,'')}';if(window.ClientSideValidations.forms==undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};\n//]]>".html_safe
end
end
end
end
end