padrino-helpers 0.10.2 → 0.10.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +3 -3
- data/.yardopts +1 -0
- data/{LICENSE → LICENSE.txt} +0 -0
- data/README.rdoc +3 -3
- data/lib/padrino-helpers.rb +21 -14
- data/lib/padrino-helpers/asset_tag_helpers.rb +163 -34
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +1 -1
- data/lib/padrino-helpers/form_helpers.rb +287 -134
- data/lib/padrino-helpers/format_helpers.rb +135 -17
- data/lib/padrino-helpers/locale/lv.yml +103 -0
- data/lib/padrino-helpers/locale/zh_cn.yml +1 -1
- data/lib/padrino-helpers/number_helpers.rb +73 -59
- data/lib/padrino-helpers/output_helpers.rb +59 -15
- data/lib/padrino-helpers/output_helpers/abstract_handler.rb +15 -20
- data/lib/padrino-helpers/output_helpers/erb_handler.rb +12 -15
- data/lib/padrino-helpers/output_helpers/haml_handler.rb +10 -14
- data/lib/padrino-helpers/output_helpers/slim_handler.rb +10 -14
- data/lib/padrino-helpers/render_helpers.rb +23 -7
- data/lib/padrino-helpers/tag_helpers.rb +41 -16
- data/lib/padrino-helpers/translation_helpers.rb +14 -0
- data/test/fixtures/markup_app/views/content_for.erb +3 -0
- data/test/fixtures/markup_app/views/content_for.haml +3 -0
- data/test/fixtures/markup_app/views/content_for.slim +3 -0
- data/test/helper.rb +3 -15
- data/test/test_asset_tag_helpers.rb +1 -1
- data/test/test_form_builder.rb +3 -5
- data/test/test_form_helpers.rb +1 -1
- data/test/test_format_helpers.rb +1 -1
- data/test/test_locale.rb +1 -1
- data/test/test_number_helpers.rb +1 -1
- data/test/test_output_helpers.rb +22 -2
- data/test/test_render_helpers.rb +1 -1
- data/test/test_tag_helpers.rb +1 -1
- metadata +10 -8
@@ -4,11 +4,26 @@ module Padrino
|
|
4
4
|
##
|
5
5
|
# Constructs a form for object using given or default form_builder
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
7
|
+
# @param [Object] object
|
8
|
+
# The object for which the form is being built.
|
9
|
+
# @param [String] url
|
10
|
+
# The url this form will submit to.
|
11
|
+
# @param [Hash] settings
|
12
|
+
# The settings associated with this form. Accepts html options.
|
13
|
+
# @option settings [String] :builder ("StandardFormBuilder")
|
14
|
+
# The FormBuilder class to use such as StandardFormBuilder.
|
15
|
+
# @param [Proc] block
|
16
|
+
# The fields and content inside this form.
|
17
|
+
#
|
18
|
+
# @yield [AbstractFormBuilder] The form builder used to compose fields.
|
19
|
+
#
|
20
|
+
# @return [String] The html object-backed form with the specified options and input fields.
|
21
|
+
#
|
22
|
+
# @example
|
9
23
|
# form_for :user, '/register' do |f| ... end
|
10
24
|
# form_for @user, '/register', :id => 'register' do |f| ... end
|
11
25
|
#
|
26
|
+
# @api public
|
12
27
|
def form_for(object, url, settings={}, &block)
|
13
28
|
form_html = capture_html(builder_instance(object, settings), &block)
|
14
29
|
form_tag(url, settings) { form_html }
|
@@ -18,11 +33,20 @@ module Padrino
|
|
18
33
|
# Constructs form fields for an object using given or default form_builder
|
19
34
|
# Used within an existing form to allow alternate objects within one form
|
20
35
|
#
|
21
|
-
#
|
36
|
+
# @param [Object] object
|
37
|
+
# The object for which the fields are being built.
|
38
|
+
# @param [Hash] settings
|
39
|
+
# The settings associated with these fields. Accepts html options.
|
40
|
+
# @param [Proc] block
|
41
|
+
# The content inside this set of fields.
|
42
|
+
#
|
43
|
+
# @return [String] The html fields with the specified options.
|
22
44
|
#
|
45
|
+
# @example
|
23
46
|
# fields_for @user.assignment do |assignment| ... end
|
24
47
|
# fields_for :assignment do |assigment| ... end
|
25
48
|
#
|
49
|
+
# @api public
|
26
50
|
def fields_for(object, settings={}, &block)
|
27
51
|
instance = builder_instance(object, settings)
|
28
52
|
fields_html = capture_html(instance, &block)
|
@@ -33,10 +57,19 @@ module Padrino
|
|
33
57
|
##
|
34
58
|
# Constructs a form without object based on options
|
35
59
|
#
|
36
|
-
#
|
60
|
+
# @param [String] url
|
61
|
+
# The url this form will submit to.
|
62
|
+
# @param [Hash] options
|
63
|
+
# The html options associated with this form.
|
64
|
+
# @param [Proc] block
|
65
|
+
# The fields and content inside this form.
|
66
|
+
#
|
67
|
+
# @return [String] The html form with the specified options and input fields.
|
37
68
|
#
|
38
|
-
#
|
69
|
+
# @example
|
70
|
+
# form_tag '/register', :class => "registration_form" do ... end
|
39
71
|
#
|
72
|
+
# @api public
|
40
73
|
def form_tag(url, options={}, &block)
|
41
74
|
desired_method = options[:method]
|
42
75
|
data_method = options.delete(:method) if options[:method].to_s !~ /get|post/i
|
@@ -55,11 +88,16 @@ module Padrino
|
|
55
88
|
# Only 'get' and 'post' are allowed within browsers;
|
56
89
|
# 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
|
57
90
|
#
|
58
|
-
#
|
91
|
+
# @param [String] desired_method
|
92
|
+
# The method this hidden field represents (i.e put or delete))
|
59
93
|
#
|
94
|
+
# @return [String] The hidden field representing the +desired_method+ for the form.
|
95
|
+
#
|
96
|
+
# @example
|
60
97
|
# # Generate: <input name="_method" value="delete" />
|
61
98
|
# hidden_form_method_field('delete')
|
62
99
|
#
|
100
|
+
# @api semipublic
|
63
101
|
def hidden_form_method_field(desired_method)
|
64
102
|
return '' if desired_method.blank? || desired_method.to_s =~ /get|post/i
|
65
103
|
hidden_field_tag(:_method, :value => desired_method)
|
@@ -68,10 +106,21 @@ module Padrino
|
|
68
106
|
##
|
69
107
|
# Constructs a field_set to group fields with given options
|
70
108
|
#
|
71
|
-
#
|
109
|
+
# @overload field_set_tag(legend=nil, options={}, &block)
|
110
|
+
# @param [String] legend The legend caption for the fieldset
|
111
|
+
# @param [Hash] options The html options for the fieldset.
|
112
|
+
# @param [Proc] block The content inside the fieldset.
|
113
|
+
# @overload field_set_tag(options={}, &block)
|
114
|
+
# @param [Hash] options The html options for the fieldset.
|
115
|
+
# @param [Proc] block The content inside the fieldset.
|
116
|
+
#
|
117
|
+
# @return [String] The html for the fieldset tag based on given +options+.
|
72
118
|
#
|
73
|
-
#
|
119
|
+
# @example
|
120
|
+
# field_set_tag(:class => "office-set") { }
|
121
|
+
# field_set_tag("Office", :class => 'office-set') { }
|
74
122
|
#
|
123
|
+
# @api public
|
75
124
|
def field_set_tag(*args, &block)
|
76
125
|
options = args.extract_options!
|
77
126
|
legend_text = args[0].is_a?(String) ? args.first : nil
|
@@ -83,30 +132,41 @@ module Padrino
|
|
83
132
|
##
|
84
133
|
# Constructs list html for the errors for a given symbol
|
85
134
|
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# or
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
135
|
+
# @overload error_messages_for(*objects, options = {})
|
136
|
+
# @param [Array<Object>] object Splat of objects to display errors for.
|
137
|
+
# @param [Hash] options Error message display options.
|
138
|
+
# @option options [String] :header_tag ("h2")
|
139
|
+
# Used for the header of the error div
|
140
|
+
# @option options [String] :id ("errorExplanation")
|
141
|
+
# The id of the error div.
|
142
|
+
# @option options [String] :class ("errorExplanation")
|
143
|
+
# The class of the error div.
|
144
|
+
# @option options [Array<Object>] :object
|
145
|
+
# The object (or array of objects) for which to display errors,
|
146
|
+
# if you need to escape the instance variable convention.
|
147
|
+
# @option options [String] :object_name
|
148
|
+
# The object name to use in the header, or any text that you prefer.
|
149
|
+
# If +:object_name+ is not set, the name of the first object will be used.
|
150
|
+
# @option options [String] :header_message ("X errors prohibited this object from being saved")
|
151
|
+
# The message in the header of the error div. Pass +nil+ or an empty string
|
152
|
+
# to avoid the header message altogether.
|
153
|
+
# @option options [String] :message ("There were problems with the following fields:")
|
154
|
+
# The explanation message after the header message and before
|
155
|
+
# the error list. Pass +nil+ or an empty string to avoid the explanation message
|
156
|
+
# altogether.
|
157
|
+
#
|
158
|
+
# @return [String] The html section with all errors for the specified +objects+
|
159
|
+
#
|
160
|
+
# @example
|
104
161
|
# error_messages_for :user
|
105
162
|
#
|
163
|
+
# @api public
|
106
164
|
def error_messages_for(*objects)
|
107
165
|
options = objects.extract_options!.symbolize_keys
|
108
|
-
objects = objects.map {|object_name|
|
109
|
-
|
166
|
+
objects = objects.map { |object_name|
|
167
|
+
object_name.is_a?(Symbol) ? instance_variable_get("@#{object_name}") : object_name
|
168
|
+
}.compact
|
169
|
+
count = objects.inject(0) { |sum, object| sum + object.errors.size }
|
110
170
|
|
111
171
|
unless count.zero?
|
112
172
|
html = {}
|
@@ -153,14 +213,20 @@ module Padrino
|
|
153
213
|
##
|
154
214
|
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
|
155
215
|
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
216
|
+
# @param [Object] object
|
217
|
+
# The object to display the error for.
|
218
|
+
# @param [Symbol] field
|
219
|
+
# The field on the +object+ to display the error for.
|
220
|
+
# @param [Hash] options
|
221
|
+
# The options to control the error display.
|
222
|
+
# @option options [String] :tag ("div")
|
223
|
+
# The tag that encloses the error.
|
224
|
+
# @option options [String] :prepend ("")
|
225
|
+
# The text to prepend before the field error.
|
226
|
+
# @option options [String] :append ("")
|
227
|
+
# The text to append after the field error.
|
228
|
+
#
|
229
|
+
# @example
|
164
230
|
# # => <span class="error">can't be blank</div>
|
165
231
|
# error_message_on :post, :title
|
166
232
|
# error_message_on @post, :title
|
@@ -171,6 +237,9 @@ module Padrino
|
|
171
237
|
# # => <div class="error">This title can't be blank (or it won't work)</div>
|
172
238
|
# error_message_on :post, :title, :prepend => "This title", :append => "(or it won't work)"
|
173
239
|
#
|
240
|
+
# @return [String] The html display of an error for a particular +object+ and +field+.
|
241
|
+
#
|
242
|
+
# @api public
|
174
243
|
def error_message_on(object, field, options={})
|
175
244
|
object = object.is_a?(Symbol) ? instance_variable_get("@#{object}") : object
|
176
245
|
error = object.errors[field] rescue nil
|
@@ -188,11 +257,22 @@ module Padrino
|
|
188
257
|
##
|
189
258
|
# Constructs a label tag from the given options
|
190
259
|
#
|
191
|
-
#
|
260
|
+
# @param [String] name
|
261
|
+
# The name of the field to label.
|
262
|
+
# @param [Hash] options
|
263
|
+
# The html options for this label.
|
264
|
+
# @option options :caption
|
265
|
+
# The caption for this label.
|
266
|
+
# @param [Proc] block
|
267
|
+
# The content to be inserted into the label.
|
268
|
+
#
|
269
|
+
# @return [String] The html for this label with the given +options+.
|
192
270
|
#
|
271
|
+
# @example
|
193
272
|
# label_tag :username, :class => 'long-label'
|
194
273
|
# label_tag :username, :class => 'long-label' do ... end
|
195
274
|
#
|
275
|
+
# @api public
|
196
276
|
def label_tag(name, options={}, &block)
|
197
277
|
options.reverse_merge!(:caption => "#{name.to_s.humanize}: ", :for => name)
|
198
278
|
caption_text = options.delete(:caption)
|
@@ -206,36 +286,48 @@ module Padrino
|
|
206
286
|
end
|
207
287
|
|
208
288
|
##
|
209
|
-
# Constructs a
|
289
|
+
# Constructs a text field input from the given options
|
210
290
|
#
|
211
|
-
#
|
291
|
+
# @macro [new] input_field_doc
|
292
|
+
# @param [String] name
|
293
|
+
# The name of the input field.
|
294
|
+
# @param [Hash] options
|
295
|
+
# The html options for the input field.
|
212
296
|
#
|
213
|
-
#
|
297
|
+
# @return [String] The html input field based on the +options+ specified
|
214
298
|
#
|
215
|
-
|
299
|
+
# @example
|
300
|
+
# text_field_tag :username, :class => 'long'
|
301
|
+
#
|
302
|
+
# @api public
|
303
|
+
def text_field_tag(name, options={})
|
216
304
|
options.reverse_merge!(:name => name)
|
217
|
-
input_tag(:
|
305
|
+
input_tag(:text, options)
|
218
306
|
end
|
219
307
|
|
220
308
|
##
|
221
|
-
# Constructs a
|
309
|
+
# Constructs a hidden field input from the given options
|
222
310
|
#
|
223
|
-
#
|
311
|
+
# @macro input_field_doc
|
224
312
|
#
|
225
|
-
#
|
313
|
+
# @example
|
314
|
+
# hidden_field_tag :session_key, :value => "__secret__"
|
226
315
|
#
|
227
|
-
|
316
|
+
# @api public
|
317
|
+
def hidden_field_tag(name, options={})
|
228
318
|
options.reverse_merge!(:name => name)
|
229
|
-
input_tag(:
|
319
|
+
input_tag(:hidden, options)
|
230
320
|
end
|
231
321
|
|
232
322
|
##
|
233
323
|
# Constructs a text area input from the given options
|
234
324
|
#
|
235
|
-
#
|
325
|
+
# @macro input_field_doc
|
236
326
|
#
|
327
|
+
# @example
|
237
328
|
# text_area_tag :username, :class => 'long', :value => "Demo?"
|
238
329
|
#
|
330
|
+
# @api public
|
239
331
|
def text_area_tag(name, options={})
|
240
332
|
options.reverse_merge!(:name => name, :rows => "", :cols => "")
|
241
333
|
content_tag(:textarea, options.delete(:value).to_s, options)
|
@@ -244,20 +336,63 @@ module Padrino
|
|
244
336
|
##
|
245
337
|
# Constructs a password field input from the given options
|
246
338
|
#
|
247
|
-
#
|
339
|
+
# @macro input_field_doc
|
248
340
|
#
|
341
|
+
# @example
|
249
342
|
# password_field_tag :password, :class => 'long'
|
250
343
|
#
|
344
|
+
# @api public
|
251
345
|
def password_field_tag(name, options={})
|
252
346
|
options.reverse_merge!(:name => name)
|
253
347
|
input_tag(:password, options)
|
254
348
|
end
|
255
349
|
|
256
350
|
##
|
257
|
-
# Constructs a
|
351
|
+
# Constructs a check_box from the given options
|
258
352
|
#
|
259
|
-
#
|
353
|
+
# @macro input_field_doc
|
354
|
+
#
|
355
|
+
# @example
|
356
|
+
# check_box_tag :remember_me, :value => 'Yes'
|
260
357
|
#
|
358
|
+
# @api public
|
359
|
+
def check_box_tag(name, options={})
|
360
|
+
options.reverse_merge!(:name => name, :value => '1')
|
361
|
+
input_tag(:checkbox, options)
|
362
|
+
end
|
363
|
+
|
364
|
+
##
|
365
|
+
# Constructs a radio_button from the given options
|
366
|
+
#
|
367
|
+
# @macro input_field_doc
|
368
|
+
#
|
369
|
+
# @example
|
370
|
+
# radio_button_tag :remember_me, :value => 'true'
|
371
|
+
#
|
372
|
+
# @api public
|
373
|
+
def radio_button_tag(name, options={})
|
374
|
+
options.reverse_merge!(:name => name)
|
375
|
+
input_tag(:radio, options)
|
376
|
+
end
|
377
|
+
|
378
|
+
##
|
379
|
+
# Constructs a file field input from the given options
|
380
|
+
#
|
381
|
+
# @macro input_field_doc
|
382
|
+
#
|
383
|
+
# @example
|
384
|
+
# file_field_tag :photo, :class => 'long'
|
385
|
+
#
|
386
|
+
# @api public
|
387
|
+
def file_field_tag(name, options={})
|
388
|
+
options.reverse_merge!(:name => name)
|
389
|
+
input_tag(:file, options)
|
390
|
+
end
|
391
|
+
|
392
|
+
##
|
393
|
+
# Constructs a select from the given options
|
394
|
+
#
|
395
|
+
# @example
|
261
396
|
# options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
|
262
397
|
# options = ['option', 'red', 'yellow' ]
|
263
398
|
# select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1')
|
@@ -273,6 +408,28 @@ module Padrino
|
|
273
408
|
# grouped_options = {'Friends' => ['Yoda',['Obiwan',1]],'Enemies' => ['Palpatine',['Darth Vader',3]]}
|
274
409
|
# select_tag(:color, :grouped_options => [['warm',['red','yellow']],['cool',['blue', 'purple']]])
|
275
410
|
#
|
411
|
+
# @param [String] name
|
412
|
+
# The name of the input field.
|
413
|
+
# @param [Hash] options
|
414
|
+
# The html options for the input field.
|
415
|
+
# @option options [Array<String, Array>] :options
|
416
|
+
# Explicit options to display in the select. Can be strings or string tuples.
|
417
|
+
# @option options [Array<Array>] :grouped_options
|
418
|
+
# List of options for each group in the select. See examples for details.
|
419
|
+
# @option options [Array<Object>] :collection
|
420
|
+
# Collection of objects used as options in the select.
|
421
|
+
# @option options [Array<Symbol>] :fields
|
422
|
+
# The attributes used as "label" and "value" for each +collection+ object.
|
423
|
+
# @option options [String] :selected (nil)
|
424
|
+
# The option value initially selected.
|
425
|
+
# @option options [Boolean] :include_blank (false)
|
426
|
+
# Include a blank option in the select.
|
427
|
+
# @option options [Boolean] :multiple (false)
|
428
|
+
# Allow multiple options to be selected at once.
|
429
|
+
#
|
430
|
+
# @return [String] The html input field based on the +options+ specified
|
431
|
+
#
|
432
|
+
# @api public
|
276
433
|
def select_tag(name, options={})
|
277
434
|
options.reverse_merge!(:name => name)
|
278
435
|
collection, fields = options.delete(:collection), options.delete(:fields)
|
@@ -289,133 +446,122 @@ module Padrino
|
|
289
446
|
end
|
290
447
|
|
291
448
|
##
|
292
|
-
# Constructs a
|
293
|
-
#
|
294
|
-
# ==== Examples
|
295
|
-
#
|
296
|
-
# check_box_tag :remember_me, :value => 'Yes'
|
297
|
-
#
|
298
|
-
def check_box_tag(name, options={})
|
299
|
-
options.reverse_merge!(:name => name, :value => '1')
|
300
|
-
input_tag(:checkbox, options)
|
301
|
-
end
|
302
|
-
|
303
|
-
##
|
304
|
-
# Constructs a radio_button from the given options
|
305
|
-
#
|
306
|
-
# ==== Examples
|
307
|
-
#
|
308
|
-
# radio_button_tag :remember_me, :value => 'true'
|
449
|
+
# Constructs a button input from the given options
|
309
450
|
#
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
##
|
316
|
-
# Constructs a file field input from the given options
|
451
|
+
# @param [String] caption
|
452
|
+
# The caption for the button.
|
453
|
+
# @param [Hash] options
|
454
|
+
# The html options for the input field.
|
317
455
|
#
|
318
|
-
#
|
456
|
+
# @return [String] The html button based on the +options+ specified.
|
319
457
|
#
|
320
|
-
#
|
458
|
+
# @example
|
459
|
+
# button_tag "Cancel", :class => 'clear'
|
321
460
|
#
|
322
|
-
|
323
|
-
|
324
|
-
|
461
|
+
# @api public
|
462
|
+
def button_tag(caption, options = {})
|
463
|
+
options.reverse_merge!(:value => caption)
|
464
|
+
input_tag(:button, options)
|
325
465
|
end
|
326
466
|
|
327
467
|
##
|
328
468
|
# Constructs a submit button from the given options
|
329
469
|
#
|
330
|
-
#
|
470
|
+
# @param [String] caption
|
471
|
+
# The caption for the submit button.
|
472
|
+
# @param [Hash] options
|
473
|
+
# The html options for the input field.
|
331
474
|
#
|
475
|
+
# @return [String] The html submit button based on the +options+ specified.
|
476
|
+
#
|
477
|
+
# @example
|
332
478
|
# submit_tag "Create", :class => 'success'
|
333
479
|
#
|
480
|
+
# @api public
|
334
481
|
def submit_tag(caption="Submit", options={})
|
335
482
|
options.reverse_merge!(:value => caption)
|
336
483
|
input_tag(:submit, options)
|
337
484
|
end
|
338
485
|
|
339
|
-
##
|
340
|
-
# Constructs a button input from the given options
|
341
|
-
#
|
342
|
-
# ==== Examples
|
343
|
-
#
|
344
|
-
# button_tag "Cancel", :class => 'clear'
|
345
|
-
#
|
346
|
-
def button_tag(caption, options = {})
|
347
|
-
options.reverse_merge!(:value => caption)
|
348
|
-
input_tag(:button, options)
|
349
|
-
end
|
350
|
-
|
351
486
|
# Constructs a submit button from the given options
|
352
487
|
#
|
353
|
-
#
|
488
|
+
# @param [String] source
|
489
|
+
# The source image path for the button.
|
490
|
+
# @param [Hash] options
|
491
|
+
# The html options for the input field.
|
492
|
+
#
|
493
|
+
# @return [String] The html image button based on the +options+ specified.
|
354
494
|
#
|
495
|
+
# @example
|
355
496
|
# submit_tag "Create", :class => 'success'
|
356
497
|
#
|
498
|
+
# @api public
|
357
499
|
def image_submit_tag(source, options={})
|
358
500
|
options.reverse_merge!(:src => image_path(source))
|
359
501
|
input_tag(:image, options)
|
360
502
|
end
|
361
503
|
|
362
|
-
|
363
|
-
# Returns an array of option items for a select field based on the given collection
|
364
|
-
# fields is an array containing the fields to display from each item in the collection
|
365
|
-
#
|
366
|
-
def options_from_collection(collection, fields)
|
367
|
-
collection.map { |item| [ item.send(fields.first), item.send(fields.last) ] }
|
368
|
-
end
|
504
|
+
protected
|
369
505
|
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
value ||= caption
|
377
|
-
content_tag(:option, caption, :value => value, :selected => option_is_selected?(value, caption, selected_value))
|
506
|
+
##
|
507
|
+
# Returns an array of option items for a select field based on the given collection
|
508
|
+
# fields is an array containing the fields to display from each item in the collection
|
509
|
+
#
|
510
|
+
def options_from_collection(collection, fields)
|
511
|
+
collection.map { |item| [ item.send(fields.first), item.send(fields.last) ] }
|
378
512
|
end
|
379
|
-
end
|
380
513
|
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
end
|
514
|
+
#
|
515
|
+
# Returns the options tags for a select based on the given option items
|
516
|
+
#
|
517
|
+
def options_for_select(option_items, selected_value=nil)
|
518
|
+
return '' if option_items.blank?
|
519
|
+
option_items.map do |caption, value|
|
520
|
+
value ||= caption
|
521
|
+
content_tag(:option, caption, :value => value, :selected => option_is_selected?(value, caption, selected_value))
|
390
522
|
end
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
523
|
+
end
|
524
|
+
|
525
|
+
#
|
526
|
+
# Returns the optgroups with options tags for a select based on the given :grouped_options items
|
527
|
+
#
|
528
|
+
def grouped_options_for_select(collection, selected=nil, prompt=false)
|
529
|
+
if collection.is_a?(Hash)
|
530
|
+
collection.map do |key, value|
|
531
|
+
content_tag :optgroup, :label => key do
|
532
|
+
options_for_select(value, selected)
|
533
|
+
end
|
534
|
+
end
|
535
|
+
elsif collection.is_a?(Array)
|
536
|
+
collection.map do |optgroup|
|
537
|
+
content_tag :optgroup, :label => optgroup.first do
|
538
|
+
options_for_select(optgroup.last, selected)
|
539
|
+
end
|
395
540
|
end
|
396
541
|
end
|
397
542
|
end
|
398
|
-
end
|
399
543
|
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
544
|
+
#
|
545
|
+
# Returns the blank option serving as a prompt if passed
|
546
|
+
#
|
547
|
+
def blank_option(prompt)
|
548
|
+
return unless prompt
|
549
|
+
case prompt
|
550
|
+
when String then content_tag(:option, prompt, :value => '')
|
551
|
+
when Array then content_tag(:option, prompt.first, :value => prompt.last)
|
552
|
+
else content_tag(:option, '', :value => '')
|
553
|
+
end
|
409
554
|
end
|
410
|
-
end
|
411
555
|
|
412
556
|
private
|
413
557
|
##
|
414
558
|
# Returns the FormBuilder class to use based on all available setting sources
|
415
559
|
# If explicitly defined, returns that, otherwise returns defaults.
|
416
560
|
#
|
561
|
+
# @example
|
417
562
|
# configured_form_builder_class(nil) => StandardFormBuilder
|
418
563
|
#
|
564
|
+
# @api private
|
419
565
|
def configured_form_builder_class(explicit_builder=nil)
|
420
566
|
default_builder = self.respond_to?(:settings) && self.settings.default_builder
|
421
567
|
configured_builder = explicit_builder || default_builder || 'StandardFormBuilder'
|
@@ -426,8 +572,10 @@ module Padrino
|
|
426
572
|
##
|
427
573
|
# Returns an initialized builder instance for the given object and settings
|
428
574
|
#
|
575
|
+
# @example
|
429
576
|
# builder_instance(@account, :nested => { ... }) => <FormBuilder>
|
430
577
|
#
|
578
|
+
# @api private
|
431
579
|
def builder_instance(object, settings={})
|
432
580
|
builder_class = configured_form_builder_class(settings.delete(:builder))
|
433
581
|
builder_class.new(self, object, settings)
|
@@ -436,6 +584,11 @@ module Padrino
|
|
436
584
|
##
|
437
585
|
# Returns whether the option should be selected or not
|
438
586
|
#
|
587
|
+
# @example
|
588
|
+
# option_is_selected?("red", "Red", ["red", "blue"]) => true
|
589
|
+
# option_is_selected?("red", "Red", ["green", "blue"]) => false
|
590
|
+
#
|
591
|
+
# @api private
|
439
592
|
def option_is_selected?(value, caption, selected_values)
|
440
593
|
Array(selected_values).any? do |selected|
|
441
594
|
[value.to_s, caption.to_s].include?(selected.to_s)
|