actionview 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionview might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +271 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +40 -0
- data/lib/action_view.rb +98 -0
- data/lib/action_view/base.rb +312 -0
- data/lib/action_view/buffers.rb +67 -0
- data/lib/action_view/cache_expiry.rb +54 -0
- data/lib/action_view/context.rb +32 -0
- data/lib/action_view/dependency_tracker.rb +175 -0
- data/lib/action_view/digestor.rb +126 -0
- data/lib/action_view/flows.rb +76 -0
- data/lib/action_view/gem_version.rb +17 -0
- data/lib/action_view/helpers.rb +66 -0
- data/lib/action_view/helpers/active_model_helper.rb +55 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +488 -0
- data/lib/action_view/helpers/asset_url_helper.rb +470 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +205 -0
- data/lib/action_view/helpers/cache_helper.rb +271 -0
- data/lib/action_view/helpers/capture_helper.rb +216 -0
- data/lib/action_view/helpers/controller_helper.rb +36 -0
- data/lib/action_view/helpers/csp_helper.rb +26 -0
- data/lib/action_view/helpers/csrf_helper.rb +35 -0
- data/lib/action_view/helpers/date_helper.rb +1200 -0
- data/lib/action_view/helpers/debug_helper.rb +36 -0
- data/lib/action_view/helpers/form_helper.rb +2569 -0
- data/lib/action_view/helpers/form_options_helper.rb +896 -0
- data/lib/action_view/helpers/form_tag_helper.rb +920 -0
- data/lib/action_view/helpers/javascript_helper.rb +95 -0
- data/lib/action_view/helpers/number_helper.rb +456 -0
- data/lib/action_view/helpers/output_safety_helper.rb +70 -0
- data/lib/action_view/helpers/rendering_helper.rb +101 -0
- data/lib/action_view/helpers/sanitize_helper.rb +171 -0
- data/lib/action_view/helpers/tag_helper.rb +314 -0
- data/lib/action_view/helpers/tags.rb +44 -0
- data/lib/action_view/helpers/tags/base.rb +196 -0
- data/lib/action_view/helpers/tags/check_box.rb +66 -0
- data/lib/action_view/helpers/tags/checkable.rb +18 -0
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +36 -0
- data/lib/action_view/helpers/tags/collection_helpers.rb +119 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +31 -0
- data/lib/action_view/helpers/tags/collection_select.rb +30 -0
- data/lib/action_view/helpers/tags/color_field.rb +27 -0
- data/lib/action_view/helpers/tags/date_field.rb +15 -0
- data/lib/action_view/helpers/tags/date_select.rb +74 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +32 -0
- data/lib/action_view/helpers/tags/datetime_local_field.rb +21 -0
- data/lib/action_view/helpers/tags/datetime_select.rb +10 -0
- data/lib/action_view/helpers/tags/email_field.rb +10 -0
- data/lib/action_view/helpers/tags/file_field.rb +10 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +31 -0
- data/lib/action_view/helpers/tags/hidden_field.rb +10 -0
- data/lib/action_view/helpers/tags/label.rb +81 -0
- data/lib/action_view/helpers/tags/month_field.rb +15 -0
- data/lib/action_view/helpers/tags/number_field.rb +20 -0
- data/lib/action_view/helpers/tags/password_field.rb +14 -0
- data/lib/action_view/helpers/tags/placeholderable.rb +24 -0
- data/lib/action_view/helpers/tags/radio_button.rb +33 -0
- data/lib/action_view/helpers/tags/range_field.rb +10 -0
- data/lib/action_view/helpers/tags/search_field.rb +27 -0
- data/lib/action_view/helpers/tags/select.rb +43 -0
- data/lib/action_view/helpers/tags/tel_field.rb +10 -0
- data/lib/action_view/helpers/tags/text_area.rb +24 -0
- data/lib/action_view/helpers/tags/text_field.rb +34 -0
- data/lib/action_view/helpers/tags/time_field.rb +15 -0
- data/lib/action_view/helpers/tags/time_select.rb +10 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +22 -0
- data/lib/action_view/helpers/tags/translator.rb +39 -0
- data/lib/action_view/helpers/tags/url_field.rb +10 -0
- data/lib/action_view/helpers/tags/week_field.rb +15 -0
- data/lib/action_view/helpers/text_helper.rb +486 -0
- data/lib/action_view/helpers/translation_helper.rb +145 -0
- data/lib/action_view/helpers/url_helper.rb +676 -0
- data/lib/action_view/layouts.rb +433 -0
- data/lib/action_view/locale/en.yml +56 -0
- data/lib/action_view/log_subscriber.rb +96 -0
- data/lib/action_view/lookup_context.rb +316 -0
- data/lib/action_view/model_naming.rb +14 -0
- data/lib/action_view/path_set.rb +95 -0
- data/lib/action_view/railtie.rb +105 -0
- data/lib/action_view/record_identifier.rb +112 -0
- data/lib/action_view/renderer/abstract_renderer.rb +108 -0
- data/lib/action_view/renderer/partial_renderer.rb +563 -0
- data/lib/action_view/renderer/partial_renderer/collection_caching.rb +103 -0
- data/lib/action_view/renderer/renderer.rb +68 -0
- data/lib/action_view/renderer/streaming_template_renderer.rb +105 -0
- data/lib/action_view/renderer/template_renderer.rb +108 -0
- data/lib/action_view/rendering.rb +171 -0
- data/lib/action_view/routing_url_for.rb +146 -0
- data/lib/action_view/tasks/cache_digests.rake +25 -0
- data/lib/action_view/template.rb +393 -0
- data/lib/action_view/template/error.rb +161 -0
- data/lib/action_view/template/handlers.rb +92 -0
- data/lib/action_view/template/handlers/builder.rb +25 -0
- data/lib/action_view/template/handlers/erb.rb +84 -0
- data/lib/action_view/template/handlers/erb/erubi.rb +87 -0
- data/lib/action_view/template/handlers/html.rb +11 -0
- data/lib/action_view/template/handlers/raw.rb +11 -0
- data/lib/action_view/template/html.rb +43 -0
- data/lib/action_view/template/inline.rb +22 -0
- data/lib/action_view/template/raw_file.rb +28 -0
- data/lib/action_view/template/resolver.rb +394 -0
- data/lib/action_view/template/sources.rb +13 -0
- data/lib/action_view/template/sources/file.rb +17 -0
- data/lib/action_view/template/text.rb +35 -0
- data/lib/action_view/template/types.rb +57 -0
- data/lib/action_view/test_case.rb +300 -0
- data/lib/action_view/testing/resolvers.rb +67 -0
- data/lib/action_view/unbound_template.rb +32 -0
- data/lib/action_view/version.rb +10 -0
- data/lib/action_view/view_paths.rb +129 -0
- data/lib/assets/compiled/rails-ujs.js +746 -0
- metadata +260 -0
@@ -0,0 +1,896 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cgi"
|
4
|
+
require "erb"
|
5
|
+
require "action_view/helpers/form_helper"
|
6
|
+
require "active_support/core_ext/string/output_safety"
|
7
|
+
require "active_support/core_ext/array/extract_options"
|
8
|
+
require "active_support/core_ext/array/wrap"
|
9
|
+
|
10
|
+
module ActionView
|
11
|
+
# = Action View Form Option Helpers
|
12
|
+
module Helpers #:nodoc:
|
13
|
+
# Provides a number of methods for turning different kinds of containers into a set of option tags.
|
14
|
+
#
|
15
|
+
# The <tt>collection_select</tt>, <tt>select</tt> and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter, a hash:
|
16
|
+
#
|
17
|
+
# * <tt>:include_blank</tt> - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
|
18
|
+
#
|
19
|
+
# select("post", "category", Post::CATEGORIES, { include_blank: true })
|
20
|
+
#
|
21
|
+
# could become:
|
22
|
+
#
|
23
|
+
# <select name="post[category]" id="post_category">
|
24
|
+
# <option value=""></option>
|
25
|
+
# <option value="joke">joke</option>
|
26
|
+
# <option value="poem">poem</option>
|
27
|
+
# </select>
|
28
|
+
#
|
29
|
+
# Another common case is a select tag for a <tt>belongs_to</tt>-associated object.
|
30
|
+
#
|
31
|
+
# Example with <tt>@post.person_id => 2</tt>:
|
32
|
+
#
|
33
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: 'None' })
|
34
|
+
#
|
35
|
+
# could become:
|
36
|
+
#
|
37
|
+
# <select name="post[person_id]" id="post_person_id">
|
38
|
+
# <option value="">None</option>
|
39
|
+
# <option value="1">David</option>
|
40
|
+
# <option value="2" selected="selected">Eileen</option>
|
41
|
+
# <option value="3">Rafael</option>
|
42
|
+
# </select>
|
43
|
+
#
|
44
|
+
# * <tt>:prompt</tt> - set to true or a prompt string. When the select element doesn't have a value yet, this prepends an option with a generic prompt -- "Please select" -- or the given prompt string.
|
45
|
+
#
|
46
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { prompt: 'Select Person' })
|
47
|
+
#
|
48
|
+
# could become:
|
49
|
+
#
|
50
|
+
# <select name="post[person_id]" id="post_person_id">
|
51
|
+
# <option value="">Select Person</option>
|
52
|
+
# <option value="1">David</option>
|
53
|
+
# <option value="2">Eileen</option>
|
54
|
+
# <option value="3">Rafael</option>
|
55
|
+
# </select>
|
56
|
+
#
|
57
|
+
# * <tt>:index</tt> - like the other form helpers, +select+ can accept an <tt>:index</tt> option to manually set the ID used in the resulting output. Unlike other helpers, +select+ expects this
|
58
|
+
# option to be in the +html_options+ parameter.
|
59
|
+
#
|
60
|
+
# select("album[]", "genre", %w[rap rock country], {}, { index: nil })
|
61
|
+
#
|
62
|
+
# becomes:
|
63
|
+
#
|
64
|
+
# <select name="album[][genre]" id="album__genre">
|
65
|
+
# <option value="rap">rap</option>
|
66
|
+
# <option value="rock">rock</option>
|
67
|
+
# <option value="country">country</option>
|
68
|
+
# </select>
|
69
|
+
#
|
70
|
+
# * <tt>:disabled</tt> - can be a single value or an array of values that will be disabled options in the final output.
|
71
|
+
#
|
72
|
+
# select("post", "category", Post::CATEGORIES, { disabled: 'restricted' })
|
73
|
+
#
|
74
|
+
# could become:
|
75
|
+
#
|
76
|
+
# <select name="post[category]" id="post_category">
|
77
|
+
# <option value=""></option>
|
78
|
+
# <option value="joke">joke</option>
|
79
|
+
# <option value="poem">poem</option>
|
80
|
+
# <option disabled="disabled" value="restricted">restricted</option>
|
81
|
+
# </select>
|
82
|
+
#
|
83
|
+
# When used with the <tt>collection_select</tt> helper, <tt>:disabled</tt> can also be a Proc that identifies those options that should be disabled.
|
84
|
+
#
|
85
|
+
# collection_select(:post, :category_id, Category.all, :id, :name, { disabled: -> (category) { category.archived? } })
|
86
|
+
#
|
87
|
+
# If the categories "2008 stuff" and "Christmas" return true when the method <tt>archived?</tt> is called, this would return:
|
88
|
+
# <select name="post[category_id]" id="post_category_id">
|
89
|
+
# <option value="1" disabled="disabled">2008 stuff</option>
|
90
|
+
# <option value="2" disabled="disabled">Christmas</option>
|
91
|
+
# <option value="3">Jokes</option>
|
92
|
+
# <option value="4">Poems</option>
|
93
|
+
# </select>
|
94
|
+
#
|
95
|
+
module FormOptionsHelper
|
96
|
+
# ERB::Util can mask some helpers like textilize. Make sure to include them.
|
97
|
+
include TextHelper
|
98
|
+
|
99
|
+
# Create a select tag and a series of contained option tags for the provided object and method.
|
100
|
+
# The option currently held by the object will be selected, provided that the object is available.
|
101
|
+
#
|
102
|
+
# There are two possible formats for the +choices+ parameter, corresponding to other helpers' output:
|
103
|
+
#
|
104
|
+
# * A flat collection (see +options_for_select+).
|
105
|
+
#
|
106
|
+
# * A nested collection (see +grouped_options_for_select+).
|
107
|
+
#
|
108
|
+
# For example:
|
109
|
+
#
|
110
|
+
# select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: true })
|
111
|
+
#
|
112
|
+
# would become:
|
113
|
+
#
|
114
|
+
# <select name="post[person_id]" id="post_person_id">
|
115
|
+
# <option value=""></option>
|
116
|
+
# <option value="1" selected="selected">David</option>
|
117
|
+
# <option value="2">Eileen</option>
|
118
|
+
# <option value="3">Rafael</option>
|
119
|
+
# </select>
|
120
|
+
#
|
121
|
+
# assuming the associated person has ID 1.
|
122
|
+
#
|
123
|
+
# This can be used to provide a default set of options in the standard way: before rendering the create form, a
|
124
|
+
# new model instance is assigned the default options and bound to @model_name. Usually this model is not saved
|
125
|
+
# to the database. Instead, a second model object is created when the create request is received.
|
126
|
+
# This allows the user to submit a form page more than once with the expected results of creating multiple records.
|
127
|
+
# In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
|
128
|
+
#
|
129
|
+
# By default, <tt>post.person_id</tt> is the selected option. Specify <tt>selected: value</tt> to use a different selection
|
130
|
+
# or <tt>selected: nil</tt> to leave all options unselected. Similarly, you can specify values to be disabled in the option
|
131
|
+
# tags by specifying the <tt>:disabled</tt> option. This can either be a single value or an array of values to be disabled.
|
132
|
+
#
|
133
|
+
# A block can be passed to +select+ to customize how the options tags will be rendered. This
|
134
|
+
# is useful when the options tag has complex attributes.
|
135
|
+
#
|
136
|
+
# select(report, "campaign_ids") do
|
137
|
+
# available_campaigns.each do |c|
|
138
|
+
# content_tag(:option, c.name, value: c.id, data: { tags: c.tags.to_json })
|
139
|
+
# end
|
140
|
+
# end
|
141
|
+
#
|
142
|
+
# ==== Gotcha
|
143
|
+
#
|
144
|
+
# The HTML specification says when +multiple+ parameter passed to select and all options got deselected
|
145
|
+
# web browsers do not send any value to server. Unfortunately this introduces a gotcha:
|
146
|
+
# if an +User+ model has many +roles+ and have +role_ids+ accessor, and in the form that edits roles of the user
|
147
|
+
# the user deselects all roles from +role_ids+ multiple select box, no +role_ids+ parameter is sent. So,
|
148
|
+
# any mass-assignment idiom like
|
149
|
+
#
|
150
|
+
# @user.update(params[:user])
|
151
|
+
#
|
152
|
+
# wouldn't update roles.
|
153
|
+
#
|
154
|
+
# To prevent this the helper generates an auxiliary hidden field before
|
155
|
+
# every multiple select. The hidden field has the same name as multiple select and blank value.
|
156
|
+
#
|
157
|
+
# <b>Note:</b> The client either sends only the hidden field (representing
|
158
|
+
# the deselected multiple select box), or both fields. This means that the resulting array
|
159
|
+
# always contains a blank string.
|
160
|
+
#
|
161
|
+
# In case if you don't want the helper to generate this hidden field you can specify
|
162
|
+
# <tt>include_hidden: false</tt> option.
|
163
|
+
#
|
164
|
+
def select(object, method, choices = nil, options = {}, html_options = {}, &block)
|
165
|
+
Tags::Select.new(object, method, self, choices, options, html_options, &block).render
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns <tt><select></tt> and <tt><option></tt> tags for the collection of existing return values of
|
169
|
+
# +method+ for +object+'s class. The value returned from calling +method+ on the instance +object+ will
|
170
|
+
# be selected. If calling +method+ returns +nil+, no selection is made without including <tt>:prompt</tt>
|
171
|
+
# or <tt>:include_blank</tt> in the +options+ hash.
|
172
|
+
#
|
173
|
+
# The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods to be called on each member
|
174
|
+
# of +collection+. The return values are used as the +value+ attribute and contents of each
|
175
|
+
# <tt><option></tt> tag, respectively. They can also be any object that responds to +call+, such
|
176
|
+
# as a +proc+, that will be called for each member of the +collection+ to
|
177
|
+
# retrieve the value/text.
|
178
|
+
#
|
179
|
+
# Example object structure for use with this method:
|
180
|
+
#
|
181
|
+
# class Post < ActiveRecord::Base
|
182
|
+
# belongs_to :author
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# class Author < ActiveRecord::Base
|
186
|
+
# has_many :posts
|
187
|
+
# def name_with_initial
|
188
|
+
# "#{first_name.first}. #{last_name}"
|
189
|
+
# end
|
190
|
+
# end
|
191
|
+
#
|
192
|
+
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
|
193
|
+
#
|
194
|
+
# collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true)
|
195
|
+
#
|
196
|
+
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
|
197
|
+
# <select name="post[author_id]" id="post_author_id">
|
198
|
+
# <option value="">Please select</option>
|
199
|
+
# <option value="1" selected="selected">D. Heinemeier Hansson</option>
|
200
|
+
# <option value="2">D. Thomas</option>
|
201
|
+
# <option value="3">M. Clark</option>
|
202
|
+
# </select>
|
203
|
+
def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
|
204
|
+
Tags::CollectionSelect.new(object, method, self, collection, value_method, text_method, options, html_options).render
|
205
|
+
end
|
206
|
+
|
207
|
+
# Returns <tt><select></tt>, <tt><optgroup></tt> and <tt><option></tt> tags for the collection of existing return values of
|
208
|
+
# +method+ for +object+'s class. The value returned from calling +method+ on the instance +object+ will
|
209
|
+
# be selected. If calling +method+ returns +nil+, no selection is made without including <tt>:prompt</tt>
|
210
|
+
# or <tt>:include_blank</tt> in the +options+ hash.
|
211
|
+
#
|
212
|
+
# Parameters:
|
213
|
+
# * +object+ - The instance of the class to be used for the select tag
|
214
|
+
# * +method+ - The attribute of +object+ corresponding to the select tag
|
215
|
+
# * +collection+ - An array of objects representing the <tt><optgroup></tt> tags.
|
216
|
+
# * +group_method+ - The name of a method which, when called on a member of +collection+, returns an
|
217
|
+
# array of child objects representing the <tt><option></tt> tags. It can also be any object that responds
|
218
|
+
# to +call+, such as a +proc+, that will be called for each member of the +collection+ to retrieve the
|
219
|
+
# value.
|
220
|
+
# * +group_label_method+ - The name of a method which, when called on a member of +collection+, returns a
|
221
|
+
# string to be used as the +label+ attribute for its <tt><optgroup></tt> tag. It can also be any object
|
222
|
+
# that responds to +call+, such as a +proc+, that will be called for each member of the +collection+ to
|
223
|
+
# retrieve the label.
|
224
|
+
# * +option_key_method+ - The name of a method which, when called on a child object of a member of
|
225
|
+
# +collection+, returns a value to be used as the +value+ attribute for its <tt><option></tt> tag.
|
226
|
+
# * +option_value_method+ - The name of a method which, when called on a child object of a member of
|
227
|
+
# +collection+, returns a value to be used as the contents of its <tt><option></tt> tag.
|
228
|
+
#
|
229
|
+
# Example object structure for use with this method:
|
230
|
+
#
|
231
|
+
# class Continent < ActiveRecord::Base
|
232
|
+
# has_many :countries
|
233
|
+
# # attribs: id, name
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
# class Country < ActiveRecord::Base
|
237
|
+
# belongs_to :continent
|
238
|
+
# # attribs: id, name, continent_id
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
# class City < ActiveRecord::Base
|
242
|
+
# belongs_to :country
|
243
|
+
# # attribs: id, name, country_id
|
244
|
+
# end
|
245
|
+
#
|
246
|
+
# Sample usage:
|
247
|
+
#
|
248
|
+
# grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name)
|
249
|
+
#
|
250
|
+
# Possible output:
|
251
|
+
#
|
252
|
+
# <select name="city[country_id]" id="city_country_id">
|
253
|
+
# <optgroup label="Africa">
|
254
|
+
# <option value="1">South Africa</option>
|
255
|
+
# <option value="3">Somalia</option>
|
256
|
+
# </optgroup>
|
257
|
+
# <optgroup label="Europe">
|
258
|
+
# <option value="7" selected="selected">Denmark</option>
|
259
|
+
# <option value="2">Ireland</option>
|
260
|
+
# </optgroup>
|
261
|
+
# </select>
|
262
|
+
#
|
263
|
+
def grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
|
264
|
+
Tags::GroupedCollectionSelect.new(object, method, self, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options).render
|
265
|
+
end
|
266
|
+
|
267
|
+
# Returns select and option tags for the given object and method, using
|
268
|
+
# #time_zone_options_for_select to generate the list of option tags.
|
269
|
+
#
|
270
|
+
# In addition to the <tt>:include_blank</tt> option documented above,
|
271
|
+
# this method also supports a <tt>:model</tt> option, which defaults
|
272
|
+
# to ActiveSupport::TimeZone. This may be used by users to specify a
|
273
|
+
# different time zone model object. (See +time_zone_options_for_select+
|
274
|
+
# for more information.)
|
275
|
+
#
|
276
|
+
# You can also supply an array of ActiveSupport::TimeZone objects
|
277
|
+
# as +priority_zones+ so that they will be listed above the rest of the
|
278
|
+
# (long) list. You can use ActiveSupport::TimeZone.us_zones for a list
|
279
|
+
# of US time zones, ActiveSupport::TimeZone.country_zones(country_code)
|
280
|
+
# for another country's time zones, or a Regexp to select the zones of
|
281
|
+
# your choice.
|
282
|
+
#
|
283
|
+
# Finally, this method supports a <tt>:default</tt> option, which selects
|
284
|
+
# a default ActiveSupport::TimeZone if the object's time zone is +nil+.
|
285
|
+
#
|
286
|
+
# time_zone_select("user", "time_zone", nil, include_blank: true)
|
287
|
+
#
|
288
|
+
# time_zone_select("user", "time_zone", nil, default: "Pacific Time (US & Canada)")
|
289
|
+
#
|
290
|
+
# time_zone_select("user", 'time_zone', ActiveSupport::TimeZone.us_zones, default: "Pacific Time (US & Canada)")
|
291
|
+
#
|
292
|
+
# time_zone_select("user", 'time_zone', [ ActiveSupport::TimeZone['Alaska'], ActiveSupport::TimeZone['Hawaii'] ])
|
293
|
+
#
|
294
|
+
# time_zone_select("user", 'time_zone', /Australia/)
|
295
|
+
#
|
296
|
+
# time_zone_select("user", "time_zone", ActiveSupport::TimeZone.all.sort, model: ActiveSupport::TimeZone)
|
297
|
+
def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})
|
298
|
+
Tags::TimeZoneSelect.new(object, method, self, priority_zones, options, html_options).render
|
299
|
+
end
|
300
|
+
|
301
|
+
# Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container
|
302
|
+
# where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and
|
303
|
+
# the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values
|
304
|
+
# become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. +selected+
|
305
|
+
# may also be an array of values to be selected when using a multiple select.
|
306
|
+
#
|
307
|
+
# options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
|
308
|
+
# # => <option value="$">Dollar</option>
|
309
|
+
# # => <option value="DKK">Kroner</option>
|
310
|
+
#
|
311
|
+
# options_for_select([ "VISA", "MasterCard" ], "MasterCard")
|
312
|
+
# # => <option value="VISA">VISA</option>
|
313
|
+
# # => <option selected="selected" value="MasterCard">MasterCard</option>
|
314
|
+
#
|
315
|
+
# options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")
|
316
|
+
# # => <option value="$20">Basic</option>
|
317
|
+
# # => <option value="$40" selected="selected">Plus</option>
|
318
|
+
#
|
319
|
+
# options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
|
320
|
+
# # => <option selected="selected" value="VISA">VISA</option>
|
321
|
+
# # => <option value="MasterCard">MasterCard</option>
|
322
|
+
# # => <option selected="selected" value="Discover">Discover</option>
|
323
|
+
#
|
324
|
+
# You can optionally provide HTML attributes as the last element of the array.
|
325
|
+
#
|
326
|
+
# options_for_select([ "Denmark", ["USA", { class: 'bold' }], "Sweden" ], ["USA", "Sweden"])
|
327
|
+
# # => <option value="Denmark">Denmark</option>
|
328
|
+
# # => <option value="USA" class="bold" selected="selected">USA</option>
|
329
|
+
# # => <option value="Sweden" selected="selected">Sweden</option>
|
330
|
+
#
|
331
|
+
# options_for_select([["Dollar", "$", { class: "bold" }], ["Kroner", "DKK", { onclick: "alert('HI');" }]])
|
332
|
+
# # => <option value="$" class="bold">Dollar</option>
|
333
|
+
# # => <option value="DKK" onclick="alert('HI');">Kroner</option>
|
334
|
+
#
|
335
|
+
# If you wish to specify disabled option tags, set +selected+ to be a hash, with <tt>:disabled</tt> being either a value
|
336
|
+
# or array of values to be disabled. In this case, you can use <tt>:selected</tt> to specify selected option tags.
|
337
|
+
#
|
338
|
+
# options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], disabled: "Super Platinum")
|
339
|
+
# # => <option value="Free">Free</option>
|
340
|
+
# # => <option value="Basic">Basic</option>
|
341
|
+
# # => <option value="Advanced">Advanced</option>
|
342
|
+
# # => <option value="Super Platinum" disabled="disabled">Super Platinum</option>
|
343
|
+
#
|
344
|
+
# options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], disabled: ["Advanced", "Super Platinum"])
|
345
|
+
# # => <option value="Free">Free</option>
|
346
|
+
# # => <option value="Basic">Basic</option>
|
347
|
+
# # => <option value="Advanced" disabled="disabled">Advanced</option>
|
348
|
+
# # => <option value="Super Platinum" disabled="disabled">Super Platinum</option>
|
349
|
+
#
|
350
|
+
# options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], selected: "Free", disabled: "Super Platinum")
|
351
|
+
# # => <option value="Free" selected="selected">Free</option>
|
352
|
+
# # => <option value="Basic">Basic</option>
|
353
|
+
# # => <option value="Advanced">Advanced</option>
|
354
|
+
# # => <option value="Super Platinum" disabled="disabled">Super Platinum</option>
|
355
|
+
#
|
356
|
+
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
|
357
|
+
def options_for_select(container, selected = nil)
|
358
|
+
return container if String === container
|
359
|
+
|
360
|
+
selected, disabled = extract_selected_and_disabled(selected).map do |r|
|
361
|
+
Array(r).map(&:to_s)
|
362
|
+
end
|
363
|
+
|
364
|
+
container.map do |element|
|
365
|
+
html_attributes = option_html_attributes(element)
|
366
|
+
text, value = option_text_and_value(element).map(&:to_s)
|
367
|
+
|
368
|
+
html_attributes[:selected] ||= option_value_selected?(value, selected)
|
369
|
+
html_attributes[:disabled] ||= disabled && option_value_selected?(value, disabled)
|
370
|
+
html_attributes[:value] = value
|
371
|
+
|
372
|
+
tag_builder.content_tag_string(:option, text, html_attributes)
|
373
|
+
end.join("\n").html_safe
|
374
|
+
end
|
375
|
+
|
376
|
+
# Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning
|
377
|
+
# the result of a call to the +value_method+ as the option value and the +text_method+ as the option text.
|
378
|
+
#
|
379
|
+
# options_from_collection_for_select(@people, 'id', 'name')
|
380
|
+
# # => <option value="#{person.id}">#{person.name}</option>
|
381
|
+
#
|
382
|
+
# This is more often than not used inside a #select_tag like this example:
|
383
|
+
#
|
384
|
+
# select_tag 'person', options_from_collection_for_select(@people, 'id', 'name')
|
385
|
+
#
|
386
|
+
# If +selected+ is specified as a value or array of values, the element(s) returning a match on +value_method+
|
387
|
+
# will be selected option tag(s).
|
388
|
+
#
|
389
|
+
# If +selected+ is specified as a Proc, those members of the collection that return true for the anonymous
|
390
|
+
# function are the selected values.
|
391
|
+
#
|
392
|
+
# +selected+ can also be a hash, specifying both <tt>:selected</tt> and/or <tt>:disabled</tt> values as required.
|
393
|
+
#
|
394
|
+
# Be sure to specify the same class as the +value_method+ when specifying selected or disabled options.
|
395
|
+
# Failure to do this will produce undesired results. Example:
|
396
|
+
# options_from_collection_for_select(@people, 'id', 'name', '1')
|
397
|
+
# Will not select a person with the id of 1 because 1 (an Integer) is not the same as '1' (a string)
|
398
|
+
# options_from_collection_for_select(@people, 'id', 'name', 1)
|
399
|
+
# should produce the desired results.
|
400
|
+
def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
|
401
|
+
options = collection.map do |element|
|
402
|
+
[value_for_collection(element, text_method), value_for_collection(element, value_method), option_html_attributes(element)]
|
403
|
+
end
|
404
|
+
selected, disabled = extract_selected_and_disabled(selected)
|
405
|
+
select_deselect = {
|
406
|
+
selected: extract_values_from_collection(collection, value_method, selected),
|
407
|
+
disabled: extract_values_from_collection(collection, value_method, disabled)
|
408
|
+
}
|
409
|
+
|
410
|
+
options_for_select(options, select_deselect)
|
411
|
+
end
|
412
|
+
|
413
|
+
# Returns a string of <tt><option></tt> tags, like <tt>options_from_collection_for_select</tt>, but
|
414
|
+
# groups them by <tt><optgroup></tt> tags based on the object relationships of the arguments.
|
415
|
+
#
|
416
|
+
# Parameters:
|
417
|
+
# * +collection+ - An array of objects representing the <tt><optgroup></tt> tags.
|
418
|
+
# * +group_method+ - The name of a method which, when called on a member of +collection+, returns an
|
419
|
+
# array of child objects representing the <tt><option></tt> tags.
|
420
|
+
# * +group_label_method+ - The name of a method which, when called on a member of +collection+, returns a
|
421
|
+
# string to be used as the +label+ attribute for its <tt><optgroup></tt> tag.
|
422
|
+
# * +option_key_method+ - The name of a method which, when called on a child object of a member of
|
423
|
+
# +collection+, returns a value to be used as the +value+ attribute for its <tt><option></tt> tag.
|
424
|
+
# * +option_value_method+ - The name of a method which, when called on a child object of a member of
|
425
|
+
# +collection+, returns a value to be used as the contents of its <tt><option></tt> tag.
|
426
|
+
# * +selected_key+ - A value equal to the +value+ attribute for one of the <tt><option></tt> tags,
|
427
|
+
# which will have the +selected+ attribute set. Corresponds to the return value of one of the calls
|
428
|
+
# to +option_key_method+. If +nil+, no selection is made. Can also be a hash if disabled values are
|
429
|
+
# to be specified.
|
430
|
+
#
|
431
|
+
# Example object structure for use with this method:
|
432
|
+
#
|
433
|
+
# class Continent < ActiveRecord::Base
|
434
|
+
# has_many :countries
|
435
|
+
# # attribs: id, name
|
436
|
+
# end
|
437
|
+
#
|
438
|
+
# class Country < ActiveRecord::Base
|
439
|
+
# belongs_to :continent
|
440
|
+
# # attribs: id, name, continent_id
|
441
|
+
# end
|
442
|
+
#
|
443
|
+
# Sample usage:
|
444
|
+
# option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
|
445
|
+
#
|
446
|
+
# Possible output:
|
447
|
+
# <optgroup label="Africa">
|
448
|
+
# <option value="1">Egypt</option>
|
449
|
+
# <option value="4">Rwanda</option>
|
450
|
+
# ...
|
451
|
+
# </optgroup>
|
452
|
+
# <optgroup label="Asia">
|
453
|
+
# <option value="3" selected="selected">China</option>
|
454
|
+
# <option value="12">India</option>
|
455
|
+
# <option value="5">Japan</option>
|
456
|
+
# ...
|
457
|
+
# </optgroup>
|
458
|
+
#
|
459
|
+
# <b>Note:</b> Only the <tt><optgroup></tt> and <tt><option></tt> tags are returned, so you still have to
|
460
|
+
# wrap the output in an appropriate <tt><select></tt> tag.
|
461
|
+
def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)
|
462
|
+
collection.map do |group|
|
463
|
+
option_tags = options_from_collection_for_select(
|
464
|
+
value_for_collection(group, group_method), option_key_method, option_value_method, selected_key)
|
465
|
+
|
466
|
+
content_tag("optgroup", option_tags, label: value_for_collection(group, group_label_method))
|
467
|
+
end.join.html_safe
|
468
|
+
end
|
469
|
+
|
470
|
+
# Returns a string of <tt><option></tt> tags, like <tt>options_for_select</tt>, but
|
471
|
+
# wraps them with <tt><optgroup></tt> tags:
|
472
|
+
#
|
473
|
+
# grouped_options = [
|
474
|
+
# ['North America',
|
475
|
+
# [['United States','US'],'Canada']],
|
476
|
+
# ['Europe',
|
477
|
+
# ['Denmark','Germany','France']]
|
478
|
+
# ]
|
479
|
+
# grouped_options_for_select(grouped_options)
|
480
|
+
#
|
481
|
+
# grouped_options = {
|
482
|
+
# 'North America' => [['United States','US'], 'Canada'],
|
483
|
+
# 'Europe' => ['Denmark','Germany','France']
|
484
|
+
# }
|
485
|
+
# grouped_options_for_select(grouped_options)
|
486
|
+
#
|
487
|
+
# Possible output:
|
488
|
+
# <optgroup label="North America">
|
489
|
+
# <option value="US">United States</option>
|
490
|
+
# <option value="Canada">Canada</option>
|
491
|
+
# </optgroup>
|
492
|
+
# <optgroup label="Europe">
|
493
|
+
# <option value="Denmark">Denmark</option>
|
494
|
+
# <option value="Germany">Germany</option>
|
495
|
+
# <option value="France">France</option>
|
496
|
+
# </optgroup>
|
497
|
+
#
|
498
|
+
# Parameters:
|
499
|
+
# * +grouped_options+ - Accepts a nested array or hash of strings. The first value serves as the
|
500
|
+
# <tt><optgroup></tt> label while the second value must be an array of options. The second value can be a
|
501
|
+
# nested array of text-value pairs. See <tt>options_for_select</tt> for more info.
|
502
|
+
# Ex. ["North America",[["United States","US"],["Canada","CA"]]]
|
503
|
+
# * +selected_key+ - A value equal to the +value+ attribute for one of the <tt><option></tt> tags,
|
504
|
+
# which will have the +selected+ attribute set. Note: It is possible for this value to match multiple options
|
505
|
+
# as you might have the same option in multiple groups. Each will then get <tt>selected="selected"</tt>.
|
506
|
+
#
|
507
|
+
# Options:
|
508
|
+
# * <tt>:prompt</tt> - set to true or a prompt string. When the select element doesn't have a value yet, this
|
509
|
+
# prepends an option with a generic prompt - "Please select" - or the given prompt string.
|
510
|
+
# * <tt>:divider</tt> - the divider for the options groups.
|
511
|
+
#
|
512
|
+
# grouped_options = [
|
513
|
+
# [['United States','US'], 'Canada'],
|
514
|
+
# ['Denmark','Germany','France']
|
515
|
+
# ]
|
516
|
+
# grouped_options_for_select(grouped_options, nil, divider: '---------')
|
517
|
+
#
|
518
|
+
# Possible output:
|
519
|
+
# <optgroup label="---------">
|
520
|
+
# <option value="US">United States</option>
|
521
|
+
# <option value="Canada">Canada</option>
|
522
|
+
# </optgroup>
|
523
|
+
# <optgroup label="---------">
|
524
|
+
# <option value="Denmark">Denmark</option>
|
525
|
+
# <option value="Germany">Germany</option>
|
526
|
+
# <option value="France">France</option>
|
527
|
+
# </optgroup>
|
528
|
+
#
|
529
|
+
# <b>Note:</b> Only the <tt><optgroup></tt> and <tt><option></tt> tags are returned, so you still have to
|
530
|
+
# wrap the output in an appropriate <tt><select></tt> tag.
|
531
|
+
def grouped_options_for_select(grouped_options, selected_key = nil, options = {})
|
532
|
+
prompt = options[:prompt]
|
533
|
+
divider = options[:divider]
|
534
|
+
|
535
|
+
body = "".html_safe
|
536
|
+
|
537
|
+
if prompt
|
538
|
+
body.safe_concat content_tag("option", prompt_text(prompt), value: "")
|
539
|
+
end
|
540
|
+
|
541
|
+
grouped_options.each do |container|
|
542
|
+
html_attributes = option_html_attributes(container)
|
543
|
+
|
544
|
+
if divider
|
545
|
+
label = divider
|
546
|
+
else
|
547
|
+
label, container = container
|
548
|
+
end
|
549
|
+
|
550
|
+
html_attributes = { label: label }.merge!(html_attributes)
|
551
|
+
body.safe_concat content_tag("optgroup", options_for_select(container, selected_key), html_attributes)
|
552
|
+
end
|
553
|
+
|
554
|
+
body
|
555
|
+
end
|
556
|
+
|
557
|
+
# Returns a string of option tags for pretty much any time zone in the
|
558
|
+
# world. Supply an ActiveSupport::TimeZone name as +selected+ to have it
|
559
|
+
# marked as the selected option tag. You can also supply an array of
|
560
|
+
# ActiveSupport::TimeZone objects as +priority_zones+, so that they will
|
561
|
+
# be listed above the rest of the (long) list. (You can use
|
562
|
+
# ActiveSupport::TimeZone.us_zones as a convenience for obtaining a list
|
563
|
+
# of the US time zones, or a Regexp to select the zones of your choice)
|
564
|
+
#
|
565
|
+
# The +selected+ parameter must be either +nil+, or a string that names
|
566
|
+
# an ActiveSupport::TimeZone.
|
567
|
+
#
|
568
|
+
# By default, +model+ is the ActiveSupport::TimeZone constant (which can
|
569
|
+
# be obtained in Active Record as a value object). The +model+ parameter
|
570
|
+
# must respond to +all+ and return an array of objects that represent time
|
571
|
+
# zones; each object must respond to +name+. If a Regexp is given it will
|
572
|
+
# attempt to match the zones using the <code>=~<code> operator.
|
573
|
+
#
|
574
|
+
# NOTE: Only the option tags are returned, you have to wrap this call in
|
575
|
+
# a regular HTML select tag.
|
576
|
+
def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
|
577
|
+
zone_options = "".html_safe
|
578
|
+
|
579
|
+
zones = model.all
|
580
|
+
convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }
|
581
|
+
|
582
|
+
if priority_zones
|
583
|
+
if priority_zones.is_a?(Regexp)
|
584
|
+
priority_zones = zones.select { |z| z =~ priority_zones }
|
585
|
+
end
|
586
|
+
|
587
|
+
zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected)
|
588
|
+
zone_options.safe_concat content_tag("option", "-------------", value: "", disabled: true)
|
589
|
+
zone_options.safe_concat "\n"
|
590
|
+
|
591
|
+
zones = zones - priority_zones
|
592
|
+
end
|
593
|
+
|
594
|
+
zone_options.safe_concat options_for_select(convert_zones[zones], selected)
|
595
|
+
end
|
596
|
+
|
597
|
+
# Returns radio button tags for the collection of existing return values
|
598
|
+
# of +method+ for +object+'s class. The value returned from calling
|
599
|
+
# +method+ on the instance +object+ will be selected. If calling +method+
|
600
|
+
# returns +nil+, no selection is made.
|
601
|
+
#
|
602
|
+
# The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are
|
603
|
+
# methods to be called on each member of +collection+. The return values
|
604
|
+
# are used as the +value+ attribute and contents of each radio button tag,
|
605
|
+
# respectively. They can also be any object that responds to +call+, such
|
606
|
+
# as a +proc+, that will be called for each member of the +collection+ to
|
607
|
+
# retrieve the value/text.
|
608
|
+
#
|
609
|
+
# Example object structure for use with this method:
|
610
|
+
# class Post < ActiveRecord::Base
|
611
|
+
# belongs_to :author
|
612
|
+
# end
|
613
|
+
# class Author < ActiveRecord::Base
|
614
|
+
# has_many :posts
|
615
|
+
# def name_with_initial
|
616
|
+
# "#{first_name.first}. #{last_name}"
|
617
|
+
# end
|
618
|
+
# end
|
619
|
+
#
|
620
|
+
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
|
621
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
|
622
|
+
#
|
623
|
+
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
|
624
|
+
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
|
625
|
+
# <label for="post_author_id_1">D. Heinemeier Hansson</label>
|
626
|
+
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
|
627
|
+
# <label for="post_author_id_2">D. Thomas</label>
|
628
|
+
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
|
629
|
+
# <label for="post_author_id_3">M. Clark</label>
|
630
|
+
#
|
631
|
+
# It is also possible to customize the way the elements will be shown by
|
632
|
+
# giving a block to the method:
|
633
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
634
|
+
# b.label { b.radio_button }
|
635
|
+
# end
|
636
|
+
#
|
637
|
+
# The argument passed to the block is a special kind of builder for this
|
638
|
+
# collection, which has the ability to generate the label and radio button
|
639
|
+
# for the current item in the collection, with proper text and value.
|
640
|
+
# Using it, you can change the label and radio button display order or
|
641
|
+
# even use the label as wrapper, as in the example above.
|
642
|
+
#
|
643
|
+
# The builder methods <tt>label</tt> and <tt>radio_button</tt> also accept
|
644
|
+
# extra HTML options:
|
645
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
646
|
+
# b.label(class: "radio_button") { b.radio_button(class: "radio_button") }
|
647
|
+
# end
|
648
|
+
#
|
649
|
+
# There are also three special methods available: <tt>object</tt>, <tt>text</tt> and
|
650
|
+
# <tt>value</tt>, which are the current item being rendered, its text and value methods,
|
651
|
+
# respectively. You can use them like this:
|
652
|
+
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b|
|
653
|
+
# b.label(:"data-value" => b.value) { b.radio_button + b.text }
|
654
|
+
# end
|
655
|
+
#
|
656
|
+
# ==== Gotcha
|
657
|
+
#
|
658
|
+
# The HTML specification says when nothing is selected on a collection of radio buttons
|
659
|
+
# web browsers do not send any value to server.
|
660
|
+
# Unfortunately this introduces a gotcha:
|
661
|
+
# if a +User+ model has a +category_id+ field and in the form no category is selected, no +category_id+ parameter is sent. So,
|
662
|
+
# any strong parameters idiom like:
|
663
|
+
#
|
664
|
+
# params.require(:user).permit(...)
|
665
|
+
#
|
666
|
+
# will raise an error since no <tt>{user: ...}</tt> will be present.
|
667
|
+
#
|
668
|
+
# To prevent this the helper generates an auxiliary hidden field before
|
669
|
+
# every collection of radio buttons. The hidden field has the same name as collection radio button and blank value.
|
670
|
+
#
|
671
|
+
# In case if you don't want the helper to generate this hidden field you can specify
|
672
|
+
# <tt>include_hidden: false</tt> option.
|
673
|
+
def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
674
|
+
Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
|
675
|
+
end
|
676
|
+
|
677
|
+
# Returns check box tags for the collection of existing return values of
|
678
|
+
# +method+ for +object+'s class. The value returned from calling +method+
|
679
|
+
# on the instance +object+ will be selected. If calling +method+ returns
|
680
|
+
# +nil+, no selection is made.
|
681
|
+
#
|
682
|
+
# The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are
|
683
|
+
# methods to be called on each member of +collection+. The return values
|
684
|
+
# are used as the +value+ attribute and contents of each check box tag,
|
685
|
+
# respectively. They can also be any object that responds to +call+, such
|
686
|
+
# as a +proc+, that will be called for each member of the +collection+ to
|
687
|
+
# retrieve the value/text.
|
688
|
+
#
|
689
|
+
# Example object structure for use with this method:
|
690
|
+
# class Post < ActiveRecord::Base
|
691
|
+
# has_and_belongs_to_many :authors
|
692
|
+
# end
|
693
|
+
# class Author < ActiveRecord::Base
|
694
|
+
# has_and_belongs_to_many :posts
|
695
|
+
# def name_with_initial
|
696
|
+
# "#{first_name.first}. #{last_name}"
|
697
|
+
# end
|
698
|
+
# end
|
699
|
+
#
|
700
|
+
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
|
701
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
|
702
|
+
#
|
703
|
+
# If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
|
704
|
+
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
|
705
|
+
# <label for="post_author_ids_1">D. Heinemeier Hansson</label>
|
706
|
+
# <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
|
707
|
+
# <label for="post_author_ids_2">D. Thomas</label>
|
708
|
+
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
|
709
|
+
# <label for="post_author_ids_3">M. Clark</label>
|
710
|
+
# <input name="post[author_ids][]" type="hidden" value="" />
|
711
|
+
#
|
712
|
+
# It is also possible to customize the way the elements will be shown by
|
713
|
+
# giving a block to the method:
|
714
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
715
|
+
# b.label { b.check_box }
|
716
|
+
# end
|
717
|
+
#
|
718
|
+
# The argument passed to the block is a special kind of builder for this
|
719
|
+
# collection, which has the ability to generate the label and check box
|
720
|
+
# for the current item in the collection, with proper text and value.
|
721
|
+
# Using it, you can change the label and check box display order or even
|
722
|
+
# use the label as wrapper, as in the example above.
|
723
|
+
#
|
724
|
+
# The builder methods <tt>label</tt> and <tt>check_box</tt> also accept
|
725
|
+
# extra HTML options:
|
726
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
727
|
+
# b.label(class: "check_box") { b.check_box(class: "check_box") }
|
728
|
+
# end
|
729
|
+
#
|
730
|
+
# There are also three special methods available: <tt>object</tt>, <tt>text</tt> and
|
731
|
+
# <tt>value</tt>, which are the current item being rendered, its text and value methods,
|
732
|
+
# respectively. You can use them like this:
|
733
|
+
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
|
734
|
+
# b.label(:"data-value" => b.value) { b.check_box + b.text }
|
735
|
+
# end
|
736
|
+
#
|
737
|
+
# ==== Gotcha
|
738
|
+
#
|
739
|
+
# When no selection is made for a collection of checkboxes most
|
740
|
+
# web browsers will not send any value.
|
741
|
+
#
|
742
|
+
# For example, if we have a +User+ model with +category_ids+ field and we
|
743
|
+
# have the following code in our update action:
|
744
|
+
#
|
745
|
+
# @user.update(params[:user])
|
746
|
+
#
|
747
|
+
# If no +category_ids+ are selected then we can safely assume this field
|
748
|
+
# will not be updated.
|
749
|
+
#
|
750
|
+
# This is possible thanks to a hidden field generated by the helper method
|
751
|
+
# for every collection of checkboxes.
|
752
|
+
# This hidden field is given the same field name as the checkboxes with a
|
753
|
+
# blank value.
|
754
|
+
#
|
755
|
+
# In the rare case you don't want this hidden field, you can pass the
|
756
|
+
# <tt>include_hidden: false</tt> option to the helper method.
|
757
|
+
def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
758
|
+
Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
|
759
|
+
end
|
760
|
+
|
761
|
+
private
|
762
|
+
def option_html_attributes(element)
|
763
|
+
if Array === element
|
764
|
+
element.select { |e| Hash === e }.reduce({}, :merge!)
|
765
|
+
else
|
766
|
+
{}
|
767
|
+
end
|
768
|
+
end
|
769
|
+
|
770
|
+
def option_text_and_value(option)
|
771
|
+
# Options are [text, value] pairs or strings used for both.
|
772
|
+
if !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
|
773
|
+
option = option.reject { |e| Hash === e } if Array === option
|
774
|
+
[option.first, option.last]
|
775
|
+
else
|
776
|
+
[option, option]
|
777
|
+
end
|
778
|
+
end
|
779
|
+
|
780
|
+
def option_value_selected?(value, selected)
|
781
|
+
Array(selected).include? value
|
782
|
+
end
|
783
|
+
|
784
|
+
def extract_selected_and_disabled(selected)
|
785
|
+
if selected.is_a?(Proc)
|
786
|
+
[selected, nil]
|
787
|
+
else
|
788
|
+
selected = Array.wrap(selected)
|
789
|
+
options = selected.extract_options!.symbolize_keys
|
790
|
+
selected_items = options.fetch(:selected, selected)
|
791
|
+
[selected_items, options[:disabled]]
|
792
|
+
end
|
793
|
+
end
|
794
|
+
|
795
|
+
def extract_values_from_collection(collection, value_method, selected)
|
796
|
+
if selected.is_a?(Proc)
|
797
|
+
collection.map do |element|
|
798
|
+
public_or_deprecated_send(element, value_method) if selected.call(element)
|
799
|
+
end.compact
|
800
|
+
else
|
801
|
+
selected
|
802
|
+
end
|
803
|
+
end
|
804
|
+
|
805
|
+
def value_for_collection(item, value)
|
806
|
+
value.respond_to?(:call) ? value.call(item) : public_or_deprecated_send(item, value)
|
807
|
+
end
|
808
|
+
|
809
|
+
def public_or_deprecated_send(item, value)
|
810
|
+
item.public_send(value)
|
811
|
+
rescue NoMethodError
|
812
|
+
raise unless item.respond_to?(value, true) && !item.respond_to?(value)
|
813
|
+
ActiveSupport::Deprecation.warn "Using private methods from view helpers is deprecated (calling private #{item.class}##{value})"
|
814
|
+
item.send(value)
|
815
|
+
end
|
816
|
+
|
817
|
+
def prompt_text(prompt)
|
818
|
+
prompt.kind_of?(String) ? prompt : I18n.translate("helpers.select.prompt", default: "Please select")
|
819
|
+
end
|
820
|
+
end
|
821
|
+
|
822
|
+
class FormBuilder
|
823
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:
|
824
|
+
#
|
825
|
+
# <%= form_for @post do |f| %>
|
826
|
+
# <%= f.select :person_id, Person.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
|
827
|
+
# <%= f.submit %>
|
828
|
+
# <% end %>
|
829
|
+
#
|
830
|
+
# Please refer to the documentation of the base helper for details.
|
831
|
+
def select(method, choices = nil, options = {}, html_options = {}, &block)
|
832
|
+
@template.select(@object_name, method, choices, objectify_options(options), @default_html_options.merge(html_options), &block)
|
833
|
+
end
|
834
|
+
|
835
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#collection_select for form builders:
|
836
|
+
#
|
837
|
+
# <%= form_for @post do |f| %>
|
838
|
+
# <%= f.collection_select :person_id, Author.all, :id, :name_with_initial, prompt: true %>
|
839
|
+
# <%= f.submit %>
|
840
|
+
# <% end %>
|
841
|
+
#
|
842
|
+
# Please refer to the documentation of the base helper for details.
|
843
|
+
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
|
844
|
+
@template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options))
|
845
|
+
end
|
846
|
+
|
847
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#grouped_collection_select for form builders:
|
848
|
+
#
|
849
|
+
# <%= form_for @city do |f| %>
|
850
|
+
# <%= f.grouped_collection_select :country_id, @continents, :countries, :name, :id, :name %>
|
851
|
+
# <%= f.submit %>
|
852
|
+
# <% end %>
|
853
|
+
#
|
854
|
+
# Please refer to the documentation of the base helper for details.
|
855
|
+
def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
|
856
|
+
@template.grouped_collection_select(@object_name, method, collection, group_method, group_label_method, option_key_method, option_value_method, objectify_options(options), @default_html_options.merge(html_options))
|
857
|
+
end
|
858
|
+
|
859
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#time_zone_select for form builders:
|
860
|
+
#
|
861
|
+
# <%= form_for @user do |f| %>
|
862
|
+
# <%= f.time_zone_select :time_zone, nil, include_blank: true %>
|
863
|
+
# <%= f.submit %>
|
864
|
+
# <% end %>
|
865
|
+
#
|
866
|
+
# Please refer to the documentation of the base helper for details.
|
867
|
+
def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
|
868
|
+
@template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_html_options.merge(html_options))
|
869
|
+
end
|
870
|
+
|
871
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#collection_check_boxes for form builders:
|
872
|
+
#
|
873
|
+
# <%= form_for @post do |f| %>
|
874
|
+
# <%= f.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial %>
|
875
|
+
# <%= f.submit %>
|
876
|
+
# <% end %>
|
877
|
+
#
|
878
|
+
# Please refer to the documentation of the base helper for details.
|
879
|
+
def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
880
|
+
@template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &block)
|
881
|
+
end
|
882
|
+
|
883
|
+
# Wraps ActionView::Helpers::FormOptionsHelper#collection_radio_buttons for form builders:
|
884
|
+
#
|
885
|
+
# <%= form_for @post do |f| %>
|
886
|
+
# <%= f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial %>
|
887
|
+
# <%= f.submit %>
|
888
|
+
# <% end %>
|
889
|
+
#
|
890
|
+
# Please refer to the documentation of the base helper for details.
|
891
|
+
def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
|
892
|
+
@template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &block)
|
893
|
+
end
|
894
|
+
end
|
895
|
+
end
|
896
|
+
end
|