-
-
Notifications
You must be signed in to change notification settings - Fork 789
/
form_builder.rb
278 lines (240 loc) · 8.48 KB
/
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'action_view'
module ActionView::Helpers::Tags
# TODO: Find a better way to solve this issue!
# This patch is needed since this Rails commit:
# https://github.com/rails/rails/commit/c1a118a
class Base
private
if defined? ::ActiveRecord
def value
if @allow_method_names_outside_object
object.send @method_name if object && object.respond_to?(@method_name, true)
else
object.send @method_name if object
end
end
end
end
end
RANSACK_FORM_BUILDER = 'RANSACK_FORM_BUILDER'.freeze
require 'simple_form' if
(ENV[RANSACK_FORM_BUILDER] || ''.freeze).match('SimpleForm'.freeze)
module Ransack
module Helpers
class FormBuilder < (ENV[RANSACK_FORM_BUILDER].try(:constantize) ||
ActionView::Helpers::FormBuilder)
def label(method, *args, &block)
options = args.extract_options!
text = args.first
i18n = options[:i18n] || {}
text ||= object.translate(
method, i18n.reverse_merge(include_associations: true)
) if object.respond_to? :translate
super(method, text, options, &block)
end
def submit(value = nil, options = {})
value, options = nil, value if value.is_a?(Hash)
value ||= Translate.word(:search).titleize
super(value, options)
end
def attribute_select(options = nil, html_options = nil, action = nil)
options ||= {}
html_options ||= {}
action ||= Constants::SEARCH
default = options.delete(:default)
raise ArgumentError, formbuilder_error_message(
"#{action}_select") unless object.respond_to?(:context)
options[:include_blank] = true unless options.has_key?(:include_blank)
bases = [''.freeze].freeze + association_array(options[:associations])
if bases.size > 1
collection = attribute_collection_for_bases(action, bases)
object.name ||= default if can_use_default?(
default, :name, mapped_values(collection.flatten(2))
)
template_grouped_collection_select(collection, options, html_options)
else
collection = collection_for_base(action, bases.first)
object.name ||= default if can_use_default?(
default, :name, mapped_values(collection)
)
template_collection_select(:name, collection, options, html_options)
end
end
def sort_direction_select(options = {}, html_options = {})
unless object.respond_to?(:context)
raise ArgumentError,
formbuilder_error_message('sort_direction'.freeze)
end
template_collection_select(:dir, sort_array, options, html_options)
end
def sort_select(options = {}, html_options = {})
attribute_select(options, html_options, 'sort'.freeze) +
sort_direction_select(options, html_options)
end
def sort_fields(*args, &block)
search_fields(:s, args, block)
end
def sort_link(attribute, *args)
@template.sort_link @object, attribute, *args
end
def sort_url(attribute, *args)
@template.sort_url @object, attribute, *args
end
def condition_fields(*args, &block)
search_fields(:c, args, block)
end
def grouping_fields(*args, &block)
search_fields(:g, args, block)
end
def attribute_fields(*args, &block)
search_fields(:a, args, block)
end
def predicate_fields(*args, &block)
search_fields(:p, args, block)
end
def value_fields(*args, &block)
search_fields(:v, args, block)
end
def search_fields(name, args, block)
args << {} unless args.last.is_a?(Hash)
args.last[:builder] ||= options[:builder]
args.last[:parent_builder] = self
options = args.extract_options!
objects = args.shift
objects ||= @object.send(name)
objects = [objects] unless Array === objects
name = "#{options[:object_name] || object_name}[#{name}]"
objects.inject(ActiveSupport::SafeBuffer.new) do |output, child|
output << @template.fields_for("#{name}[#{options[:child_index] ||
nested_child_index(name)}]", child, options, &block)
end
end
def predicate_select(options = {}, html_options = {})
options[:compounds] = true if options[:compounds].nil?
default = options.delete(:default) || Constants::CONT
keys =
if options[:compounds]
Predicate.names
else
Predicate.names.reject { |k| k.match(/_(any|all)$/) }
end
if only = options[:only]
if only.respond_to? :call
keys = keys.select { |k| only.call(k) }
else
only = Array.wrap(only).map(&:to_s)
keys = keys.select {
|k| only.include? k.sub(/_(any|all)$/, ''.freeze)
}
end
end
collection = keys.map { |k| [k, Translate.predicate(k)] }
object.predicate ||= Predicate.named(default) if
can_use_default?(default, :predicate, keys)
template_collection_select(:p, collection, options, html_options)
end
def combinator_select(options = {}, html_options = {})
template_collection_select(
:m, combinator_choices, options, html_options)
end
private
def template_grouped_collection_select(collection, options, html_options)
@template.grouped_collection_select(
@object_name, :name, collection, :last, :first, :first, :last,
objectify_options(options), @default_options.merge(html_options)
)
end
def template_collection_select(name, collection, options, html_options)
@template.collection_select(
@object_name, name, collection, :first, :last,
objectify_options(options), @default_options.merge(html_options)
)
end
def can_use_default?(default, attribute, values)
object.respond_to?("#{attribute}=") && default &&
values.include?(default.to_s)
end
def mapped_values(values)
values.map { |v| v.is_a?(Array) ? v.first : nil }.compact
end
def sort_array
[
['asc'.freeze, object.translate('asc'.freeze)].freeze,
['desc'.freeze, object.translate('desc'.freeze)].freeze
].freeze
end
def combinator_choices
if Nodes::Condition === object
[
[Constants::OR, Translate.word(:any)],
[Constants::AND, Translate.word(:all)]
]
else
[
[Constants::AND, Translate.word(:all)],
[Constants::OR, Translate.word(:any)]
]
end
end
def association_array(obj, prefix = nil)
([prefix] + association_object(obj))
.compact
.flat_map { |v| [prefix, v].compact.join(Constants::UNDERSCORE) }
end
def association_object(obj)
case obj
when Array
obj
when Hash
association_hash(obj)
else
[obj]
end
end
def association_hash(obj)
obj.map do |key, value|
case value
when Array, Hash
association_array(value, key.to_s)
else
[key.to_s, [key, value].join(Constants::UNDERSCORE)]
end
end
end
def attribute_collection_for_bases(action, bases)
bases.map { |base| get_attribute_element(action, base) }.compact
end
def get_attribute_element(action, base)
begin
[
Translate.association(base, context: object.context),
collection_for_base(action, base)
]
rescue UntraversableAssociationError
nil
end
end
def attribute_collection_for_base(attributes, base = nil)
attributes.map do |c|
[
attr_from_base_and_column(base, c),
Translate.attribute(
attr_from_base_and_column(base, c), context: object.context
)
]
end
end
def collection_for_base(action, base)
attribute_collection_for_base(
object.context.send("#{action}able_attributes", base), base)
end
def attr_from_base_and_column(base, column)
[base, column].reject(&:blank?).join(Constants::UNDERSCORE)
end
def formbuilder_error_message(action)
"#{action.sub(Constants::SEARCH, Constants::ATTRIBUTE)
} must be called inside a search FormBuilder!"
end
end
end
end