-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_form_builder.rb
236 lines (202 loc) · 7.37 KB
/
app_form_builder.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# To be used with Bootstrap 5
class AppFormBuilder < ActionView::Helpers::FormBuilder
delegate :tag, :safe_join, to: :@template
def input(method, options = {})
@form_options = options
object_type = object_type_for_method(method)
input_type = if options[:as]
options[:as]
elsif options[:collection]
:select
else
object_type
end
override_input_type = case input_type
when :date then :string
when :integer then :string
end
send("#{override_input_type || input_type}_input", method, options)
end
def self.with_blank_error_proc(&block)
old_error_proc = ActionView::Base.field_error_proc
begin
ActionView::Base.field_error_proc = Proc.new do |tag, instance|
tag
end
block.call
ensure
ActionView::Base.field_error_proc = old_error_proc
end
end
def label(method, label = nil, options = {})
super(method, label, merge_input_options({class: "form-label"}, options))
end
private
def form_group(method, options = {}, &block)
tag.div class: "mb-3 #{method}" do
safe_join [
block.call,
hint_text(options[:hint]),
error_text(method),
].compact
end
end
def hint_text(text)
return if text.nil?
tag.small text, class: "form-text text-muted"
end
def error_text(method)
return unless has_error?(method)
tag.div(load_error(method).join("<br />").html_safe, class: "invalid-feedback")
end
def has_error?(method)
return false unless @object.respond_to?(:errors)
association = find_association(method)
normalized_method = association ? association.name : method
@object.errors.key?(normalized_method)
end
def load_error(method)
association = find_association(method)
normalized_method = association ? association.name : method
@object.errors[normalized_method]
end
def object_type_for_method(method)
result = if @object.respond_to?(:type_for_attribute) && @object.has_attribute?(method)
@object.type_for_attribute(method.to_s).try(:type)
elsif @object.respond_to?(:column_for_attribute) && @object.has_attribute?(method)
@object.column_for_attribute(method).try(:type)
end
result || :string
end
# Inputs and helpers
def string_input(method, options = {})
form_group(method, options) do
safe_join [
(label(method, options[:label]) unless options[:label] == false),
string_field(method, merge_input_options({input_html: {class: "form-control #{"is-invalid" if has_error?(method)}"}}, options)),
]
end
end
def text_input(method, options = {})
form_group(method, options) do
safe_join [
(label(method, options[:label]) unless options[:label] == false),
text_area(method, merge_input_options({class: "form-control #{"is-invalid" if has_error?(method)}"}, options[:input_html])),
]
end
end
def boolean_input(method, options = {})
form_group(method, options) do
tag.div(class: "form-check") do
safe_join [
check_box(method, merge_input_options({class: "form-check-input"}, options[:input_html])),
label(method, options[:label], class: "form-check-label"),
]
end
end
end
def collection_input(method, options, &block)
form_group(method, options) do
safe_join [
label(method, options[:label]),
block.call,
]
end
end
def select_input(method, options = {})
value_method = options[:value_method] || :to_s
text_method = options[:text_method] || :to_s
input_options = options[:input_html] || {}
multiple = input_options[:multiple]
collection_input(method, options) do
collection_select(method, options[:collection], value_method, text_method, options, merge_input_options({class: "#{"custom-select" unless multiple} form-control #{"is-invalid" if has_error?(method)}"}, options[:input_html]))
end
end
def grouped_select_input(method, options = {})
# We probably need to go back later and adjust this for more customization
collection_input(method, options) do
grouped_collection_select(method, options[:collection], :last, :first, :to_s, :to_s, options, merge_input_options({class: "custom-select form-control #{"is-invalid" if has_error?(method)}"}, options[:input_html]))
end
end
def file_input(method, options = {})
form_group(method, options) do
safe_join [
(label(method, options[:label]) unless options[:label] == false),
file_field(method, merge_input_options({class: "form-control"}, options))
]
end
end
def collection_of(input_type, method, options = {})
form_builder_method, custom_class, input_builder_method = case input_type
when :radio_buttons then [:collection_radio_buttons, "custom-radio", :radio_button]
when :check_boxes then [:collection_check_boxes, "custom-checkbox", :check_box]
else raise "Invalid input_type for collection_of, valid input_types are \":radio_buttons\", \":check_boxes\""
end
form_group(method, options) do
safe_join [
label(method, options[:label]),
tag.br,
(send(form_builder_method, method, options[:collection], options[:value_method], options[:text_method]) do |b|
tag.div(class: "custom-control #{custom_class}") {
safe_join [
b.send(input_builder_method, class: "custom-control-input"),
b.label(class: "custom-control-label"),
]
}
end),
]
end
end
def radio_buttons_input(method, options = {})
collection_of(:radio_buttons, method, options)
end
def check_boxes_input(method, options = {})
collection_of(:check_boxes, method, options)
end
def string_field(method, options = {})
html_options = options[:input_html]
case options[:as] || object_type_for_method(method)
when :date then
birthday = method.to_s =~ /birth/
safe_join [
date_field(method, merge_input_options(html_options, {data: {datepicker: true}})),
tag.div {
date_select(method, {
order: [:month, :day, :year],
start_year: birthday ? 1900 : Date.today.year - 5,
end_year: birthday ? Date.today.year : Date.today.year + 5,
}, {data: {date_select: true}})
},
]
when :integer then number_field(method, html_options)
when :string
case method.to_s
when /password/ then password_field(method, html_options)
# when /time_zone/ then :time_zone
# when /country/ then :country
when /email/ then email_field(method, html_options)
when /phone/ then telephone_field(method, html_options)
when /url/ then url_field(method, html_options)
else
text_field(method, html_options)
end
else
text_field(method, html_options)
end
end
def merge_input_options(options, user_options)
return options if user_options.nil?
options.deep_merge(user_options) do |key, val1, val2|
# When we pass in classes or stimulus related attributes we will try to merge them by concatenation rather than overwriting.
if [:action, :controller, :class].include?(key)
@template.token_list(val1, val2)
else
val2
end
end
end
def find_association(method)
# Converts the method #author_id to #author in order to look up the association
object.class.reflect_on_association(method.to_s.gsub("_id", "").to_sym)
end
end