-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_helper.rb
121 lines (101 loc) · 3.28 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
# frozen_string_literal: true
module FormProps
module ActionViewExtensions
module FormHelper
def form_props(model: nil, scope: nil, url: nil, format: nil, **options, &block)
json = @__json
options = {
allow_method_names_outside_object: true,
controlled: false
}.merge!(options)
options.delete(:remote)
options[:local] = true
options[:skip_default_ids] = false
if model
if url != false
url ||= if format.nil?
polymorphic_path(model, {})
else
polymorphic_path(model, format: format)
end
end
model = convert_to_model(_object_for_form_builder(model))
scope ||= model_name_from_record_or_class(model).param_key
end
if block
options[:builder] = FormProps::FormBuilder
builder = instantiate_builder(scope, model, options)
json.inputs do
capture(builder, &block)
end
options[:multipart] ||= builder.multipart?
end
html_options = html_options_for_form_with(url, model, **options)
json.extras do
extra_props_for_form(json, html_options)
end
json.props(FormProps::Helper.format_keys(html_options))
end
private
def token_props(json, token = nil, form_options: {})
if token != false && defined?(protect_against_forgery?) && protect_against_forgery?
token =
if token == true || token.nil?
form_authenticity_token(form_options: form_options.merge(authenticity_token: token))
else
token
end
json.set!("csrf") do
json.name request_forgery_protection_token.to_s
json.type "hidden"
json.defaultValue token
json.autoComplete "off"
end
end
end
def method_props(json, method)
json.set!("method") do
json.name "_method"
json.type "hidden"
json.defaultValue method.to_s
json.autoComplete "off"
end
end
def utf8_enforcer_props(json)
json.set!("utf8") do
json.name "utf8"
json.type "hidden"
json.defaultValue "✓"
json.autoComplete "off"
end
end
def extra_props_for_form(json, html_options)
authenticity_token = html_options.delete("authenticity_token")
method = html_options.delete("method").to_s.downcase
case method
when "get"
html_options["method"] = "get"
when "post", ""
html_options["method"] = "post"
token_props(json, authenticity_token, form_options: {
action: html_options["action"],
method: "post"
})
else
html_options["method"] = "post"
method_props(json, method)
token_props(json, authenticity_token, form_options: {
action: html_options["action"],
method: method
})
end
if html_options.delete("enforce_utf8") { default_enforce_utf8 }
utf8_enforcer_props(json)
end
end
end
end
end
ActiveSupport.on_load(:action_view) do
include FormProps::ActionViewExtensions::FormHelper
end