actionview 4.2.11.1 → 7.0.2.4

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.

Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +229 -215
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +9 -8
  5. data/lib/action_view/base.rb +116 -43
  6. data/lib/action_view/buffers.rb +20 -3
  7. data/lib/action_view/cache_expiry.rb +66 -0
  8. data/lib/action_view/context.rb +8 -12
  9. data/lib/action_view/dependency_tracker/erb_tracker.rb +154 -0
  10. data/lib/action_view/dependency_tracker/ripper_tracker.rb +59 -0
  11. data/lib/action_view/dependency_tracker.rb +21 -122
  12. data/lib/action_view/digestor.rb +92 -85
  13. data/lib/action_view/flows.rb +15 -16
  14. data/lib/action_view/gem_version.rb +6 -4
  15. data/lib/action_view/helpers/active_model_helper.rb +17 -12
  16. data/lib/action_view/helpers/asset_tag_helper.rb +356 -101
  17. data/lib/action_view/helpers/asset_url_helper.rb +180 -74
  18. data/lib/action_view/helpers/atom_feed_helper.rb +21 -19
  19. data/lib/action_view/helpers/cache_helper.rb +156 -43
  20. data/lib/action_view/helpers/capture_helper.rb +21 -14
  21. data/lib/action_view/helpers/controller_helper.rb +16 -5
  22. data/lib/action_view/helpers/csp_helper.rb +26 -0
  23. data/lib/action_view/helpers/csrf_helper.rb +8 -6
  24. data/lib/action_view/helpers/date_helper.rb +288 -132
  25. data/lib/action_view/helpers/debug_helper.rb +9 -6
  26. data/lib/action_view/helpers/form_helper.rb +956 -173
  27. data/lib/action_view/helpers/form_options_helper.rb +178 -97
  28. data/lib/action_view/helpers/form_tag_helper.rb +220 -101
  29. data/lib/action_view/helpers/javascript_helper.rb +33 -19
  30. data/lib/action_view/helpers/number_helper.rb +88 -63
  31. data/lib/action_view/helpers/output_safety_helper.rb +38 -6
  32. data/lib/action_view/helpers/rendering_helper.rb +21 -10
  33. data/lib/action_view/helpers/sanitize_helper.rb +31 -32
  34. data/lib/action_view/helpers/tag_helper.rb +332 -71
  35. data/lib/action_view/helpers/tags/base.rb +123 -99
  36. data/lib/action_view/helpers/tags/check_box.rb +21 -20
  37. data/lib/action_view/helpers/tags/checkable.rb +4 -2
  38. data/lib/action_view/helpers/tags/collection_check_boxes.rb +12 -34
  39. data/lib/action_view/helpers/tags/collection_helpers.rb +69 -36
  40. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +6 -12
  41. data/lib/action_view/helpers/tags/collection_select.rb +5 -3
  42. data/lib/action_view/helpers/tags/color_field.rb +4 -3
  43. data/lib/action_view/helpers/tags/date_field.rb +3 -2
  44. data/lib/action_view/helpers/tags/date_select.rb +38 -37
  45. data/lib/action_view/helpers/tags/datetime_field.rb +4 -3
  46. data/lib/action_view/helpers/tags/datetime_local_field.rb +3 -2
  47. data/lib/action_view/helpers/tags/datetime_select.rb +2 -0
  48. data/lib/action_view/helpers/tags/email_field.rb +2 -0
  49. data/lib/action_view/helpers/tags/file_field.rb +18 -0
  50. data/lib/action_view/helpers/tags/grouped_collection_select.rb +4 -2
  51. data/lib/action_view/helpers/tags/hidden_field.rb +6 -0
  52. data/lib/action_view/helpers/tags/label.rb +7 -2
  53. data/lib/action_view/helpers/tags/month_field.rb +3 -2
  54. data/lib/action_view/helpers/tags/number_field.rb +2 -0
  55. data/lib/action_view/helpers/tags/password_field.rb +3 -1
  56. data/lib/action_view/helpers/tags/placeholderable.rb +3 -1
  57. data/lib/action_view/helpers/tags/radio_button.rb +7 -6
  58. data/lib/action_view/helpers/tags/range_field.rb +2 -0
  59. data/lib/action_view/helpers/tags/search_field.rb +14 -9
  60. data/lib/action_view/helpers/tags/select.rb +11 -10
  61. data/lib/action_view/helpers/tags/tel_field.rb +2 -0
  62. data/lib/action_view/helpers/tags/text_area.rb +4 -2
  63. data/lib/action_view/helpers/tags/text_field.rb +8 -8
  64. data/lib/action_view/helpers/tags/time_field.rb +12 -2
  65. data/lib/action_view/helpers/tags/time_select.rb +2 -0
  66. data/lib/action_view/helpers/tags/time_zone_select.rb +3 -1
  67. data/lib/action_view/helpers/tags/translator.rb +15 -16
  68. data/lib/action_view/helpers/tags/url_field.rb +2 -0
  69. data/lib/action_view/helpers/tags/week_field.rb +3 -2
  70. data/lib/action_view/helpers/tags/weekday_select.rb +28 -0
  71. data/lib/action_view/helpers/tags.rb +5 -2
  72. data/lib/action_view/helpers/text_helper.rb +80 -51
  73. data/lib/action_view/helpers/translation_helper.rb +120 -69
  74. data/lib/action_view/helpers/url_helper.rb +398 -171
  75. data/lib/action_view/helpers.rb +29 -27
  76. data/lib/action_view/layouts.rb +68 -63
  77. data/lib/action_view/log_subscriber.rb +77 -10
  78. data/lib/action_view/lookup_context.rb +137 -113
  79. data/lib/action_view/model_naming.rb +4 -2
  80. data/lib/action_view/path_set.rb +28 -32
  81. data/lib/action_view/railtie.rb +74 -13
  82. data/lib/action_view/record_identifier.rb +53 -26
  83. data/lib/action_view/render_parser.rb +188 -0
  84. data/lib/action_view/renderer/abstract_renderer.rb +152 -15
  85. data/lib/action_view/renderer/collection_renderer.rb +196 -0
  86. data/lib/action_view/renderer/object_renderer.rb +34 -0
  87. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +102 -0
  88. data/lib/action_view/renderer/partial_renderer.rb +51 -333
  89. data/lib/action_view/renderer/renderer.rb +68 -11
  90. data/lib/action_view/renderer/streaming_template_renderer.rb +60 -56
  91. data/lib/action_view/renderer/template_renderer.rb +87 -74
  92. data/lib/action_view/rendering.rb +73 -47
  93. data/lib/action_view/ripper_ast_parser.rb +198 -0
  94. data/lib/action_view/routing_url_for.rb +35 -24
  95. data/lib/action_view/tasks/cache_digests.rake +25 -0
  96. data/lib/action_view/template/error.rb +151 -41
  97. data/lib/action_view/template/handlers/builder.rb +12 -13
  98. data/lib/action_view/template/handlers/erb/erubi.rb +89 -0
  99. data/lib/action_view/template/handlers/erb.rb +29 -89
  100. data/lib/action_view/template/handlers/html.rb +11 -0
  101. data/lib/action_view/template/handlers/raw.rb +4 -4
  102. data/lib/action_view/template/handlers.rb +14 -10
  103. data/lib/action_view/template/html.rb +12 -13
  104. data/lib/action_view/template/inline.rb +22 -0
  105. data/lib/action_view/template/raw_file.rb +25 -0
  106. data/lib/action_view/template/renderable.rb +24 -0
  107. data/lib/action_view/template/resolver.rb +139 -300
  108. data/lib/action_view/template/sources/file.rb +17 -0
  109. data/lib/action_view/template/sources.rb +13 -0
  110. data/lib/action_view/template/text.rb +10 -12
  111. data/lib/action_view/template/types.rb +28 -26
  112. data/lib/action_view/template.rb +123 -91
  113. data/lib/action_view/template_details.rb +66 -0
  114. data/lib/action_view/template_path.rb +64 -0
  115. data/lib/action_view/test_case.rb +70 -53
  116. data/lib/action_view/testing/resolvers.rb +25 -35
  117. data/lib/action_view/unbound_template.rb +57 -0
  118. data/lib/action_view/version.rb +3 -1
  119. data/lib/action_view/view_paths.rb +73 -58
  120. data/lib/action_view.rb +16 -11
  121. data/lib/assets/compiled/rails-ujs.js +746 -0
  122. metadata +52 -32
  123. data/lib/action_view/helpers/record_tag_helper.rb +0 -108
  124. data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,11 +1,13 @@
1
- require 'action_view/helpers/javascript_helper'
2
- require 'active_support/core_ext/array/access'
3
- require 'active_support/core_ext/hash/keys'
4
- require 'active_support/core_ext/string/output_safety'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/array/access"
4
+ require "active_support/core_ext/hash/keys"
5
+ require "active_support/core_ext/string/output_safety"
6
+ require "action_view/helpers/tag_helper"
5
7
 
6
8
  module ActionView
7
9
  # = Action View URL Helpers
8
- module Helpers #:nodoc:
10
+ module Helpers # :nodoc:
9
11
  # Provides a set of methods for making links and getting URLs that
10
12
  # depend on the routing subsystem (see ActionDispatch::Routing).
11
13
  # This allows you to use the same format for links in views
@@ -27,6 +29,8 @@ module ActionView
27
29
  end
28
30
  end
29
31
 
32
+ mattr_accessor :button_to_generates_button_tag, default: false
33
+
30
34
  # Basic implementation of url_for to allow use helpers without routes existence
31
35
  def url_for(options = nil) # :nodoc:
32
36
  case options
@@ -35,21 +39,31 @@ module ActionView
35
39
  when :back
36
40
  _back_url
37
41
  else
38
- raise ArgumentError, "arguments passed to url_for can't be handled. Please require " +
42
+ raise ArgumentError, "arguments passed to url_for can't be handled. Please require " \
39
43
  "routes or provide your own implementation"
40
44
  end
41
45
  end
42
46
 
43
47
  def _back_url # :nodoc:
44
- referrer = controller.respond_to?(:request) && controller.request.env["HTTP_REFERER"]
45
- referrer || 'javascript:history.back()'
48
+ _filtered_referrer || "javascript:history.back()"
49
+ end
50
+ private :_back_url
51
+
52
+ def _filtered_referrer # :nodoc:
53
+ if controller.respond_to?(:request)
54
+ referrer = controller.request.env["HTTP_REFERER"]
55
+ if referrer && URI(referrer).scheme != "javascript"
56
+ referrer
57
+ end
58
+ end
59
+ rescue URI::InvalidURIError
46
60
  end
47
- protected :_back_url
61
+ private :_filtered_referrer
48
62
 
49
- # Creates a link tag of the given +name+ using a URL created by the set of +options+.
63
+ # Creates an anchor element of the given +name+ using a URL created by the set of +options+.
50
64
  # See the valid options in the documentation for +url_for+. It's also possible to
51
- # pass a String instead of an options hash, which generates a link tag that uses the
52
- # value of the String as the href for the link. Using a <tt>:back</tt> Symbol instead
65
+ # pass a \String instead of an options hash, which generates an anchor element that uses the
66
+ # value of the \String as the href for the link. Using a <tt>:back</tt> \Symbol instead
53
67
  # of an options hash will generate a link to the referrer (a JavaScript back link
54
68
  # will be used in place of a referrer if none exists). If +nil+ is passed as the name
55
69
  # the value of the link itself will become the name.
@@ -71,6 +85,8 @@ module ActionView
71
85
  # # name
72
86
  # end
73
87
  #
88
+ # link_to(active_record_model)
89
+ #
74
90
  # ==== Options
75
91
  # * <tt>:data</tt> - This option can be used to add custom data attributes.
76
92
  # * <tt>method: symbol of HTTP verb</tt> - This modifier will dynamically
@@ -89,18 +105,8 @@ module ActionView
89
105
  # completion of the Ajax request and performing JavaScript operations once
90
106
  # they're complete
91
107
  #
92
- # ==== Data attributes
93
- #
94
- # * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
95
- # driver to prompt with the question specified (in this case, the
96
- # resulting text would be <tt>question?</tt>. If the user accepts, the
97
- # link is processed normally, otherwise no action is taken.
98
- # * <tt>:disable_with</tt> - Value of this parameter will be
99
- # used as the value for a disabled version of the submit
100
- # button when the form is submitted. This feature is provided
101
- # by the unobtrusive JavaScript driver.
102
- #
103
108
  # ==== Examples
109
+ #
104
110
  # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
105
111
  # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
106
112
  # your application on resources and use
@@ -128,6 +134,17 @@ module ActionView
128
134
  # link_to "Profiles", controller: "profiles"
129
135
  # # => <a href="/profiles">Profiles</a>
130
136
  #
137
+ # When name is +nil+ the href is presented instead
138
+ #
139
+ # link_to nil, "http://example.com"
140
+ # # => <a href="http://www.example.com">http://www.example.com</a>
141
+ #
142
+ # More concise yet, when +name+ is an Active Record model that defines a
143
+ # +to_s+ method returning a default value or a model instance attribute
144
+ #
145
+ # link_to @profile
146
+ # # => <a href="http://www.example.com/profiles/1">Eileen</a>
147
+ #
131
148
  # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
132
149
  #
133
150
  # <%= link_to(@profile) do %>
@@ -161,27 +178,44 @@ module ActionView
161
178
  # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
162
179
  #
163
180
  # link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
164
- # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
181
+ # # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
165
182
  #
166
183
  # The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
167
184
  #
168
185
  # link_to("Destroy", "http://www.example.com", method: :delete)
169
186
  # # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
170
187
  #
171
- # You can also use custom data attributes using the <tt>:data</tt> option:
188
+ # Also you can set any link attributes such as <tt>target</tt>, <tt>rel</tt>, <tt>type</tt>:
189
+ #
190
+ # link_to "External link", "http://www.rubyonrails.org/", target: "_blank", rel: "nofollow"
191
+ # # => <a href="http://www.rubyonrails.org/" target="_blank" rel="nofollow">External link</a>
192
+ #
193
+ # ==== Deprecated: Rails UJS attributes
194
+ #
195
+ # Prior to Rails 7, Rails shipped with a JavaScript library called @rails/ujs on by default. Following Rails 7,
196
+ # this library is no longer on by default. This library integrated with the following options:
197
+ #
198
+ # * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
199
+ # driver to prompt with the question specified (in this case, the
200
+ # resulting text would be <tt>question?</tt>). If the user accepts, the
201
+ # link is processed normally, otherwise no action is taken.
202
+ # * <tt>:disable_with</tt> - Value of this parameter will be used as the
203
+ # name for a disabled version of the link. This feature is provided by
204
+ # the unobtrusive JavaScript driver.
172
205
  #
173
206
  # link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: "Are you sure?" }
174
207
  # # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?">Visit Other Site</a>
208
+ #
175
209
  def link_to(name = nil, options = nil, html_options = nil, &block)
176
210
  html_options, options, name = options, name, block if block_given?
177
211
  options ||= {}
178
212
 
179
213
  html_options = convert_options_to_data_attributes(options, html_options)
180
214
 
181
- url = url_for(options)
182
- html_options['href'] ||= url
215
+ url = url_target(name, options)
216
+ html_options["href"] ||= url
183
217
 
184
- content_tag(:a, name || url, html_options, &block)
218
+ content_tag("a", name || url, html_options, &block)
185
219
  end
186
220
 
187
221
  # Generates a form containing a single button that submits to the URL created
@@ -191,21 +225,33 @@ module ActionView
191
225
  # using the +link_to+ method with the <tt>:method</tt> modifier as described in
192
226
  # the +link_to+ documentation.
193
227
  #
194
- # By default, the generated form element has a class name of <tt>button_to</tt>
195
- # to allow styling of the form itself and its children. This can be changed
196
- # using the <tt>:form_class</tt> modifier within +html_options+. You can control
197
- # the form submission and input element behavior using +html_options+.
198
- # This method accepts the <tt>:method</tt> modifier described in the +link_to+ documentation.
199
- # If no <tt>:method</tt> modifier is given, it will default to performing a POST operation.
200
- # You can also disable the button by passing <tt>disabled: true</tt> in +html_options+.
201
- # If you are using RESTful routes, you can pass the <tt>:method</tt>
202
- # to change the HTTP verb used to submit the form.
228
+ # You can control the form and button behavior with +html_options+. Most
229
+ # values in +html_options+ are passed through to the button element. For
230
+ # example, passing a +:class+ option within +html_options+ will set the
231
+ # class attribute of the button element.
232
+ #
233
+ # The class attribute of the form element can be set by passing a
234
+ # +:form_class+ option within +html_options+. It defaults to
235
+ # <tt>"button_to"</tt> to allow styling of the form and its children.
236
+ #
237
+ # The form submits a POST request by default. You can specify a different
238
+ # HTTP verb via the +:method+ option within +html_options+.
203
239
  #
204
240
  # ==== Options
205
- # The +options+ hash accepts the same options as +url_for+.
241
+ # The +options+ hash accepts the same options as +url_for+. To generate a
242
+ # <tt><form></tt> element without an <tt>[action]</tt> attribute, pass
243
+ # <tt>false</tt>:
244
+ #
245
+ # <%= button_to "New", false %>
246
+ # # => "<form method="post" class="button_to">
247
+ # # <button type="submit">New</button>
248
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
249
+ # # </form>"
250
+ #
251
+ # Most values in +html_options+ are passed through to the button element,
252
+ # but there are a few special options:
206
253
  #
207
- # There are a few special +html_options+:
208
- # * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
254
+ # * <tt>:method</tt> - \Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
209
255
  # <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
210
256
  # * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
211
257
  # * <tt>:data</tt> - This option can be used to add custom data attributes.
@@ -214,27 +260,26 @@ module ActionView
214
260
  # * <tt>:form</tt> - This hash will be form attributes
215
261
  # * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
216
262
  # be placed
217
- # * <tt>:params</tt> - Hash of parameters to be rendered as hidden fields within the form.
218
- #
219
- # ==== Data attributes
220
- #
221
- # * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
222
- # prompt with the question specified. If the user accepts, the link is
223
- # processed normally, otherwise no action is taken.
224
- # * <tt>:disable_with</tt> - Value of this parameter will be
225
- # used as the value for a disabled version of the submit
226
- # button when the form is submitted. This feature is provided
227
- # by the unobtrusive JavaScript driver.
263
+ # * <tt>:params</tt> - \Hash of parameters to be rendered as hidden fields within the form.
228
264
  #
229
265
  # ==== Examples
230
266
  # <%= button_to "New", action: "new" %>
231
267
  # # => "<form method="post" action="/controller/new" class="button_to">
232
- # # <input value="New" type="submit" />
268
+ # # <button type="submit">New</button>
269
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6" autocomplete="off"/>
233
270
  # # </form>"
234
271
  #
235
- # <%= button_to "New", new_articles_path %>
272
+ # <%= button_to "New", new_article_path %>
236
273
  # # => "<form method="post" action="/articles/new" class="button_to">
237
- # # <input value="New" type="submit" />
274
+ # # <button type="submit">New</button>
275
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6" autocomplete="off"/>
276
+ # # </form>"
277
+ #
278
+ # <%= button_to "New", new_article_path, params: { time: Time.now } %>
279
+ # # => "<form method="post" action="/articles/new" class="button_to">
280
+ # # <button type="submit">New</button>
281
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
282
+ # # <input type="hidden" name="time" value="2021-04-08 14:06:09 -0500" autocomplete="off">
238
283
  # # </form>"
239
284
  #
240
285
  # <%= button_to [:make_happy, @user] do %>
@@ -244,78 +289,87 @@ module ActionView
244
289
  # # <button type="submit">
245
290
  # # Make happy <strong><%= @user.name %></strong>
246
291
  # # </button>
292
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6" autocomplete="off"/>
247
293
  # # </form>"
248
294
  #
249
295
  # <%= button_to "New", { action: "new" }, form_class: "new-thing" %>
250
296
  # # => "<form method="post" action="/controller/new" class="new-thing">
251
- # # <input value="New" type="submit" />
297
+ # # <button type="submit">New</button>
298
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6" autocomplete="off"/>
252
299
  # # </form>"
253
300
  #
254
- #
255
301
  # <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %>
256
302
  # # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
257
- # # <input value="Create" type="submit" />
258
- # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
303
+ # # <button type="submit">Create</button>
304
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6" autocomplete="off"/>
259
305
  # # </form>"
260
306
  #
307
+ # ==== Deprecated: Rails UJS attributes
261
308
  #
262
- # <%= button_to "Delete Image", { action: "delete", id: @image.id },
263
- # method: :delete, data: { confirm: "Are you sure?" } %>
264
- # # => "<form method="post" action="/images/delete/1" class="button_to">
265
- # # <input type="hidden" name="_method" value="delete" />
266
- # # <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
267
- # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
268
- # # </form>"
269
- #
309
+ # Prior to Rails 7, Rails shipped with a JavaScript library called @rails/ujs on by default. Following Rails 7,
310
+ # this library is no longer on by default. This library integrated with the following options:
270
311
  #
271
- # <%= button_to('Destroy', 'http://www.example.com',
272
- # method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
273
- # # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
274
- # # <input name='_method' value='delete' type='hidden' />
275
- # # <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
276
- # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
277
- # # </form>"
278
- # #
312
+ # * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
313
+ # driver to prompt with the question specified (in this case, the
314
+ # resulting text would be <tt>question?</tt>). If the user accepts, the
315
+ # button is processed normally, otherwise no action is taken.
316
+ # * <tt>:disable_with</tt> - Value of this parameter will be
317
+ # used as the value for a disabled version of the submit
318
+ # button when the form is submitted. This feature is provided
319
+ # by the unobtrusive JavaScript driver.
279
320
  def button_to(name = nil, options = nil, html_options = nil, &block)
280
321
  html_options, options = options, name if block_given?
281
- options ||= {}
282
322
  html_options ||= {}
283
-
284
323
  html_options = html_options.stringify_keys
285
- convert_boolean_attributes!(html_options, %w(disabled))
286
324
 
287
- url = options.is_a?(String) ? options : url_for(options)
288
- remote = html_options.delete('remote')
289
- params = html_options.delete('params')
325
+ url =
326
+ case options
327
+ when FalseClass then nil
328
+ else url_for(options)
329
+ end
330
+
331
+ remote = html_options.delete("remote")
332
+ params = html_options.delete("params")
333
+
334
+ authenticity_token = html_options.delete("authenticity_token")
290
335
 
291
- method = html_options.delete('method').to_s
292
- method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : ''.html_safe
336
+ method = (html_options.delete("method").presence || method_for_options(options)).to_s
337
+ method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : "".html_safe
293
338
 
294
- form_method = method == 'get' ? 'get' : 'post'
295
- form_options = html_options.delete('form') || {}
296
- form_options[:class] ||= html_options.delete('form_class') || 'button_to'
297
- form_options.merge!(method: form_method, action: url)
298
- form_options.merge!("data-remote" => "true") if remote
339
+ form_method = method == "get" ? "get" : "post"
340
+ form_options = html_options.delete("form") || {}
341
+ form_options[:class] ||= html_options.delete("form_class") || "button_to"
342
+ form_options[:method] = form_method
343
+ form_options[:action] = url
344
+ form_options[:'data-remote'] = true if remote
299
345
 
300
- request_token_tag = form_method == 'post' ? token_tag : ''
346
+ request_token_tag = if form_method == "post"
347
+ request_method = method.empty? ? "post" : method
348
+ token_tag(authenticity_token, form_options: { action: url, method: request_method })
349
+ else
350
+ ""
351
+ end
301
352
 
302
353
  html_options = convert_options_to_data_attributes(options, html_options)
303
- html_options['type'] = 'submit'
354
+ html_options["type"] = "submit"
304
355
 
305
356
  button = if block_given?
306
- content_tag('button', html_options, &block)
357
+ content_tag("button", html_options, &block)
358
+ elsif button_to_generates_button_tag
359
+ content_tag("button", name || url, html_options, &block)
307
360
  else
308
- html_options['value'] = name || url
309
- tag('input', html_options)
361
+ html_options["value"] = name || url
362
+ tag("input", html_options)
310
363
  end
311
364
 
312
365
  inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
313
366
  if params
314
- params.each do |param_name, value|
315
- inner_tags.safe_concat tag(:input, type: "hidden", name: param_name, value: value.to_param)
367
+ to_form_params(params).each do |param|
368
+ inner_tags.safe_concat tag(:input, type: "hidden", name: param[:name], value: param[:value],
369
+ autocomplete: "off")
316
370
  end
317
371
  end
318
- content_tag('form', inner_tags, form_options)
372
+ content_tag("form", inner_tags, form_options)
319
373
  end
320
374
 
321
375
  # Creates a link tag of the given +name+ using a URL created by the set of
@@ -387,8 +441,7 @@ module ActionView
387
441
  # Creates a link tag of the given +name+ using a URL created by the set of
388
442
  # +options+ if +condition+ is true, otherwise only the name is
389
443
  # returned. To specialize the default behavior, you can pass a block that
390
- # accepts the name or the full argument list for +link_to_unless+ (see the examples
391
- # in +link_to_unless+).
444
+ # accepts the name or the full argument list for +link_to_if+.
392
445
  #
393
446
  # ==== Examples
394
447
  # <%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
@@ -428,6 +481,7 @@ module ActionView
428
481
  # * <tt>:body</tt> - Preset the body of the email.
429
482
  # * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
430
483
  # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
484
+ # * <tt>:reply_to</tt> - Preset the Reply-To field of the email.
431
485
  #
432
486
  # ==== Obfuscation
433
487
  # Prior to Rails 4.0, +mail_to+ provided options for encoding the address
@@ -441,9 +495,9 @@ module ActionView
441
495
  # mail_to "me@domain.com", "My email"
442
496
  # # => <a href="mailto:me@domain.com">My email</a>
443
497
  #
444
- # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
498
+ # mail_to "me@domain.com", cc: "ccaddress@domain.com",
445
499
  # subject: "This is an example email"
446
- # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
500
+ # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">me@domain.com</a>
447
501
  #
448
502
  # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
449
503
  #
@@ -454,76 +508,67 @@ module ActionView
454
508
  # <strong>Email me:</strong> <span>me@domain.com</span>
455
509
  # </a>
456
510
  def mail_to(email_address, name = nil, html_options = {}, &block)
457
- html_options, name = name, nil if block_given?
511
+ html_options, name = name, nil if name.is_a?(Hash)
458
512
  html_options = (html_options || {}).stringify_keys
459
513
 
460
- extras = %w{ cc bcc body subject }.map! { |item|
461
- option = html_options.delete(item) || next
462
- "#{item}=#{Rack::Utils.escape_path(option)}"
514
+ extras = %w{ cc bcc body subject reply_to }.map! { |item|
515
+ option = html_options.delete(item).presence || next
516
+ "#{item.dasherize}=#{ERB::Util.url_encode(option)}"
463
517
  }.compact
464
- extras = extras.empty? ? '' : '?' + extras.join('&')
518
+ extras = extras.empty? ? "" : "?" + extras.join("&")
465
519
 
466
- encoded_email_address = ERB::Util.url_encode(email_address ? email_address.to_str : '').gsub("%40", "@")
520
+ encoded_email_address = ERB::Util.url_encode(email_address).gsub("%40", "@")
467
521
  html_options["href"] = "mailto:#{encoded_email_address}#{extras}"
468
522
 
469
- content_tag(:a, name || email_address, html_options, &block)
523
+ content_tag("a", name || email_address, html_options, &block)
470
524
  end
471
525
 
472
526
  # True if the current request URI was generated by the given +options+.
473
527
  #
474
528
  # ==== Examples
475
- # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc</tt> action.
529
+ # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc&page=1</tt> action.
476
530
  #
477
531
  # current_page?(action: 'process')
478
532
  # # => false
479
533
  #
480
- # current_page?(controller: 'shop', action: 'checkout')
481
- # # => true
482
- #
483
- # current_page?(controller: 'shop', action: 'checkout', order: 'asc')
484
- # # => false
485
- #
486
534
  # current_page?(action: 'checkout')
487
535
  # # => true
488
536
  #
489
537
  # current_page?(controller: 'library', action: 'checkout')
490
538
  # # => false
491
539
  #
492
- # current_page?('http://www.example.com/shop/checkout')
493
- # # => true
494
- #
495
- # current_page?('/shop/checkout')
540
+ # current_page?(controller: 'shop', action: 'checkout')
496
541
  # # => true
497
542
  #
498
- # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc&page=1</tt> action.
499
- #
500
- # current_page?(action: 'process')
543
+ # current_page?(controller: 'shop', action: 'checkout', order: 'asc')
501
544
  # # => false
502
545
  #
503
- # current_page?(controller: 'shop', action: 'checkout')
504
- # # => true
505
- #
506
546
  # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '1')
507
547
  # # => true
508
548
  #
509
549
  # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '2')
510
550
  # # => false
511
551
  #
512
- # current_page?(controller: 'shop', action: 'checkout', order: 'desc')
552
+ # current_page?('http://www.example.com/shop/checkout')
553
+ # # => true
554
+ #
555
+ # current_page?('http://www.example.com/shop/checkout', check_parameters: true)
513
556
  # # => false
514
557
  #
515
- # current_page?(action: 'checkout')
558
+ # current_page?('/shop/checkout')
516
559
  # # => true
517
560
  #
518
- # current_page?(controller: 'library', action: 'checkout')
519
- # # => false
561
+ # current_page?('http://www.example.com/shop/checkout?order=desc&page=1')
562
+ # # => true
520
563
  #
521
564
  # Let's say we're in the <tt>http://www.example.com/products</tt> action with method POST in case of invalid product.
522
565
  #
523
566
  # current_page?(controller: 'product', action: 'index')
524
567
  # # => false
525
568
  #
526
- def current_page?(options)
569
+ # We can also pass in the symbol arguments instead of strings.
570
+ #
571
+ def current_page?(options = nil, check_parameters: false, **options_as_kwargs)
527
572
  unless request
528
573
  raise "You cannot use helpers that need to determine the current " \
529
574
  "page unless your view context provides a Request object " \
@@ -532,89 +577,271 @@ module ActionView
532
577
 
533
578
  return false unless request.get? || request.head?
534
579
 
535
- url_string = URI.parser.unescape(url_for(options)).force_encoding(Encoding::BINARY)
580
+ options ||= options_as_kwargs
581
+ check_parameters ||= options.is_a?(Hash) && options.delete(:check_parameters)
582
+ url_string = URI::DEFAULT_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)
536
583
 
537
584
  # We ignore any extra parameters in the request_uri if the
538
- # submitted url doesn't have any either. This lets the function
585
+ # submitted URL doesn't have any either. This lets the function
539
586
  # work with things like ?order=asc
540
- request_uri = url_string.index("?") ? request.fullpath : request.path
541
- request_uri = URI.parser.unescape(request_uri).force_encoding(Encoding::BINARY)
587
+ # the behaviour can be disabled with check_parameters: true
588
+ request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
589
+ request_uri = URI::DEFAULT_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)
542
590
 
543
- if url_string =~ /^\w+:\/\//
544
- url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
545
- else
546
- url_string == request_uri
591
+ if %r{^\w+://}.match?(url_string)
592
+ request_uri = +"#{request.protocol}#{request.host_with_port}#{request_uri}"
593
+ end
594
+
595
+ remove_trailing_slash!(url_string)
596
+ remove_trailing_slash!(request_uri)
597
+
598
+ url_string == request_uri
599
+ end
600
+
601
+ if RUBY_VERSION.start_with?("2.7")
602
+ using Module.new {
603
+ refine UrlHelper do
604
+ alias :_current_page? :current_page?
605
+ end
606
+ }
607
+
608
+ def current_page?(*args) # :nodoc:
609
+ options = args.pop
610
+ options.is_a?(Hash) ? _current_page?(*args, **options) : _current_page?(*args, options)
547
611
  end
548
612
  end
549
613
 
614
+ # Creates an SMS anchor link tag to the specified +phone_number+. When the
615
+ # link is clicked, the default SMS messaging app is opened ready to send a
616
+ # message to the linked phone number. If the +body+ option is specified,
617
+ # the contents of the message will be preset to +body+.
618
+ #
619
+ # If +name+ is not specified, +phone_number+ will be used as the name of
620
+ # the link.
621
+ #
622
+ # A +country_code+ option is supported, which prepends a plus sign and the
623
+ # given country code to the linked phone number. For example,
624
+ # <tt>country_code: "01"</tt> will prepend <tt>+01</tt> to the linked
625
+ # phone number.
626
+ #
627
+ # Additional HTML attributes for the link can be passed via +html_options+.
628
+ #
629
+ # ==== Options
630
+ # * <tt>:country_code</tt> - Prepend the country code to the phone number.
631
+ # * <tt>:body</tt> - Preset the body of the message.
632
+ #
633
+ # ==== Examples
634
+ # sms_to "5155555785"
635
+ # # => <a href="sms:5155555785;">5155555785</a>
636
+ #
637
+ # sms_to "5155555785", country_code: "01"
638
+ # # => <a href="sms:+015155555785;">5155555785</a>
639
+ #
640
+ # sms_to "5155555785", "Text me"
641
+ # # => <a href="sms:5155555785;">Text me</a>
642
+ #
643
+ # sms_to "5155555785", body: "I have a question about your product."
644
+ # # => <a href="sms:5155555785;?body=I%20have%20a%20question%20about%20your%20product">5155555785</a>
645
+ #
646
+ # You can use a block as well if your link target is hard to fit into the name parameter. \ERB example:
647
+ #
648
+ # <%= sms_to "5155555785" do %>
649
+ # <strong>Text me:</strong>
650
+ # <% end %>
651
+ # # => <a href="sms:5155555785;">
652
+ # <strong>Text me:</strong>
653
+ # </a>
654
+ def sms_to(phone_number, name = nil, html_options = {}, &block)
655
+ html_options, name = name, nil if name.is_a?(Hash)
656
+ html_options = (html_options || {}).stringify_keys
657
+
658
+ country_code = html_options.delete("country_code").presence
659
+ country_code = country_code ? "+#{ERB::Util.url_encode(country_code)}" : ""
660
+
661
+ body = html_options.delete("body").presence
662
+ body = body ? "?&body=#{ERB::Util.url_encode(body)}" : ""
663
+
664
+ encoded_phone_number = ERB::Util.url_encode(phone_number)
665
+ html_options["href"] = "sms:#{country_code}#{encoded_phone_number};#{body}"
666
+
667
+ content_tag("a", name || phone_number, html_options, &block)
668
+ end
669
+
670
+ # Creates a TEL anchor link tag to the specified +phone_number+. When the
671
+ # link is clicked, the default app to make phone calls is opened and
672
+ # prepopulated with the phone number.
673
+ #
674
+ # If +name+ is not specified, +phone_number+ will be used as the name of
675
+ # the link.
676
+ #
677
+ # A +country_code+ option is supported, which prepends a plus sign and the
678
+ # given country code to the linked phone number. For example,
679
+ # <tt>country_code: "01"</tt> will prepend <tt>+01</tt> to the linked
680
+ # phone number.
681
+ #
682
+ # Additional HTML attributes for the link can be passed via +html_options+.
683
+ #
684
+ # ==== Options
685
+ # * <tt>:country_code</tt> - Prepends the country code to the phone number
686
+ #
687
+ # ==== Examples
688
+ # phone_to "1234567890"
689
+ # # => <a href="tel:1234567890">1234567890</a>
690
+ #
691
+ # phone_to "1234567890", "Phone me"
692
+ # # => <a href="tel:1234567890">Phone me</a>
693
+ #
694
+ # phone_to "1234567890", country_code: "01"
695
+ # # => <a href="tel:+011234567890">1234567890</a>
696
+ #
697
+ # You can use a block as well if your link target is hard to fit into the name parameter. \ERB example:
698
+ #
699
+ # <%= phone_to "1234567890" do %>
700
+ # <strong>Phone me:</strong>
701
+ # <% end %>
702
+ # # => <a href="tel:1234567890">
703
+ # <strong>Phone me:</strong>
704
+ # </a>
705
+ def phone_to(phone_number, name = nil, html_options = {}, &block)
706
+ html_options, name = name, nil if name.is_a?(Hash)
707
+ html_options = (html_options || {}).stringify_keys
708
+
709
+ country_code = html_options.delete("country_code").presence
710
+ country_code = country_code.nil? ? "" : "+#{ERB::Util.url_encode(country_code)}"
711
+
712
+ encoded_phone_number = ERB::Util.url_encode(phone_number)
713
+ html_options["href"] = "tel:#{country_code}#{encoded_phone_number}"
714
+
715
+ content_tag("a", name || phone_number, html_options, &block)
716
+ end
717
+
550
718
  private
551
719
  def convert_options_to_data_attributes(options, html_options)
552
720
  if html_options
553
721
  html_options = html_options.stringify_keys
554
- html_options['data-remote'] = 'true' if link_to_remote_options?(options) || link_to_remote_options?(html_options)
722
+ html_options["data-remote"] = "true" if link_to_remote_options?(options) || link_to_remote_options?(html_options)
555
723
 
556
- method = html_options.delete('method')
724
+ method = html_options.delete("method")
557
725
 
558
726
  add_method_to_attributes!(html_options, method) if method
559
727
 
560
728
  html_options
561
729
  else
562
- link_to_remote_options?(options) ? {'data-remote' => 'true'} : {}
730
+ link_to_remote_options?(options) ? { "data-remote" => "true" } : {}
731
+ end
732
+ end
733
+
734
+ def url_target(name, options)
735
+ if name.respond_to?(:model_name) && options.is_a?(Hash) && options.empty?
736
+ url_for(name)
737
+ else
738
+ url_for(options)
563
739
  end
564
740
  end
565
741
 
566
742
  def link_to_remote_options?(options)
567
743
  if options.is_a?(Hash)
568
- options.delete('remote') || options.delete(:remote)
744
+ options.delete("remote") || options.delete(:remote)
569
745
  end
570
746
  end
571
747
 
572
748
  def add_method_to_attributes!(html_options, method)
573
- if method && method.to_s.downcase != "get" && html_options["rel"] !~ /nofollow/
574
- html_options["rel"] = "#{html_options["rel"]} nofollow".lstrip
749
+ if method_not_get_method?(method) && !html_options["rel"]&.match?(/nofollow/)
750
+ if html_options["rel"].blank?
751
+ html_options["rel"] = "nofollow"
752
+ else
753
+ html_options["rel"] = "#{html_options["rel"]} nofollow"
754
+ end
575
755
  end
576
756
  html_options["data-method"] = method
577
757
  end
578
758
 
579
- # Processes the +html_options+ hash, converting the boolean
580
- # attributes from true/false form into the form required by
581
- # HTML/XHTML. (An attribute is considered to be boolean if
582
- # its name is listed in the given +bool_attrs+ array.)
583
- #
584
- # More specifically, for each boolean attribute in +html_options+
585
- # given as:
759
+ def method_for_options(options)
760
+ if options.is_a?(Array)
761
+ method_for_options(options.last)
762
+ elsif options.respond_to?(:persisted?)
763
+ options.persisted? ? :patch : :post
764
+ elsif options.respond_to?(:to_model)
765
+ method_for_options(options.to_model)
766
+ end
767
+ end
768
+
769
+ STRINGIFIED_COMMON_METHODS = {
770
+ get: "get",
771
+ delete: "delete",
772
+ patch: "patch",
773
+ post: "post",
774
+ put: "put",
775
+ }.freeze
776
+
777
+ def method_not_get_method?(method)
778
+ return false unless method
779
+ (STRINGIFIED_COMMON_METHODS[method] || method.to_s.downcase) != "get"
780
+ end
781
+
782
+ def token_tag(token = nil, form_options: {})
783
+ if token != false && defined?(protect_against_forgery?) && protect_against_forgery?
784
+ token =
785
+ if token == true || token.nil?
786
+ form_authenticity_token(form_options: form_options.merge(authenticity_token: token))
787
+ else
788
+ token
789
+ end
790
+ tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token, autocomplete: "off")
791
+ else
792
+ ""
793
+ end
794
+ end
795
+
796
+ def method_tag(method)
797
+ tag("input", type: "hidden", name: "_method", value: method.to_s, autocomplete: "off")
798
+ end
799
+
800
+ # Returns an array of hashes each containing :name and :value keys
801
+ # suitable for use as the names and values of form input fields:
586
802
  #
587
- # "attr" => bool_value
803
+ # to_form_params(name: 'David', nationality: 'Danish')
804
+ # # => [{name: 'name', value: 'David'}, {name: 'nationality', value: 'Danish'}]
588
805
  #
589
- # if the associated +bool_value+ evaluates to true, it is
590
- # replaced with the attribute's name; otherwise the attribute is
591
- # removed from the +html_options+ hash. (See the XHTML 1.0 spec,
592
- # section 4.5 "Attribute Minimization" for more:
593
- # http://www.w3.org/TR/xhtml1/#h-4.5)
806
+ # to_form_params(country: { name: 'Denmark' })
807
+ # # => [{name: 'country[name]', value: 'Denmark'}]
594
808
  #
595
- # Returns the updated +html_options+ hash, which is also modified
596
- # in place.
809
+ # to_form_params(countries: ['Denmark', 'Sweden']})
810
+ # # => [{name: 'countries[]', value: 'Denmark'}, {name: 'countries[]', value: 'Sweden'}]
597
811
  #
598
- # Example:
812
+ # An optional namespace can be passed to enclose key names:
599
813
  #
600
- # convert_boolean_attributes!( html_options,
601
- # %w( checked disabled readonly ) )
602
- def convert_boolean_attributes!(html_options, bool_attrs)
603
- bool_attrs.each { |x| html_options[x] = x if html_options.delete(x) }
604
- html_options
605
- end
814
+ # to_form_params({ name: 'Denmark' }, 'country')
815
+ # # => [{name: 'country[name]', value: 'Denmark'}]
816
+ def to_form_params(attribute, namespace = nil)
817
+ attribute = if attribute.respond_to?(:permitted?)
818
+ attribute.to_h
819
+ else
820
+ attribute
821
+ end
606
822
 
607
- def token_tag(token=nil)
608
- if token != false && protect_against_forgery?
609
- token ||= form_authenticity_token
610
- tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token)
823
+ params = []
824
+ case attribute
825
+ when Hash
826
+ attribute.each do |key, value|
827
+ prefix = namespace ? "#{namespace}[#{key}]" : key
828
+ params.push(*to_form_params(value, prefix))
829
+ end
830
+ when Array
831
+ array_prefix = "#{namespace}[]"
832
+ attribute.each do |value|
833
+ params.push(*to_form_params(value, array_prefix))
834
+ end
611
835
  else
612
- ''
836
+ params << { name: namespace.to_s, value: attribute.to_param }
613
837
  end
838
+
839
+ params.sort_by { |pair| pair[:name] }
614
840
  end
615
841
 
616
- def method_tag(method)
617
- tag('input', type: 'hidden', name: '_method', value: method.to_s)
842
+ def remove_trailing_slash!(url_string)
843
+ trailing_index = (url_string.index("?") || 0) - 1
844
+ url_string[trailing_index] = "" if url_string[trailing_index] == "/"
618
845
  end
619
846
  end
620
847
  end