actionpack 4.0.1 → 4.2.11.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

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