actionview 4.2.11.3 → 5.2.7.1

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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +115 -245
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -6
  5. data/lib/action_view/base.rb +38 -28
  6. data/lib/action_view/buffers.rb +3 -1
  7. data/lib/action_view/context.rb +3 -3
  8. data/lib/action_view/dependency_tracker.rb +54 -20
  9. data/lib/action_view/digestor.rb +94 -83
  10. data/lib/action_view/flows.rb +11 -11
  11. data/lib/action_view/gem_version.rb +5 -3
  12. data/lib/action_view/helpers/active_model_helper.rb +17 -11
  13. data/lib/action_view/helpers/asset_tag_helper.rb +244 -62
  14. data/lib/action_view/helpers/asset_url_helper.rb +170 -67
  15. data/lib/action_view/helpers/atom_feed_helper.rb +19 -17
  16. data/lib/action_view/helpers/cache_helper.rb +105 -42
  17. data/lib/action_view/helpers/capture_helper.rb +16 -13
  18. data/lib/action_view/helpers/controller_helper.rb +15 -4
  19. data/lib/action_view/helpers/csp_helper.rb +24 -0
  20. data/lib/action_view/helpers/csrf_helper.rb +7 -5
  21. data/lib/action_view/helpers/date_helper.rb +170 -112
  22. data/lib/action_view/helpers/debug_helper.rb +7 -6
  23. data/lib/action_view/helpers/form_helper.rb +521 -127
  24. data/lib/action_view/helpers/form_options_helper.rb +109 -63
  25. data/lib/action_view/helpers/form_tag_helper.rb +110 -67
  26. data/lib/action_view/helpers/javascript_helper.rb +27 -12
  27. data/lib/action_view/helpers/number_helper.rb +77 -58
  28. data/lib/action_view/helpers/output_safety_helper.rb +36 -4
  29. data/lib/action_view/helpers/record_tag_helper.rb +14 -99
  30. data/lib/action_view/helpers/rendering_helper.rb +6 -5
  31. data/lib/action_view/helpers/sanitize_helper.rb +20 -15
  32. data/lib/action_view/helpers/tag_helper.rb +229 -73
  33. data/lib/action_view/helpers/tags/base.rb +134 -97
  34. data/lib/action_view/helpers/tags/check_box.rb +20 -18
  35. data/lib/action_view/helpers/tags/checkable.rb +4 -2
  36. data/lib/action_view/helpers/tags/collection_check_boxes.rb +12 -33
  37. data/lib/action_view/helpers/tags/collection_helpers.rb +70 -36
  38. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +6 -11
  39. data/lib/action_view/helpers/tags/collection_select.rb +4 -2
  40. data/lib/action_view/helpers/tags/color_field.rb +3 -1
  41. data/lib/action_view/helpers/tags/date_field.rb +2 -0
  42. data/lib/action_view/helpers/tags/date_select.rb +38 -36
  43. data/lib/action_view/helpers/tags/datetime_field.rb +4 -2
  44. data/lib/action_view/helpers/tags/datetime_local_field.rb +2 -0
  45. data/lib/action_view/helpers/tags/datetime_select.rb +2 -0
  46. data/lib/action_view/helpers/tags/email_field.rb +2 -0
  47. data/lib/action_view/helpers/tags/file_field.rb +2 -0
  48. data/lib/action_view/helpers/tags/grouped_collection_select.rb +4 -2
  49. data/lib/action_view/helpers/tags/hidden_field.rb +2 -0
  50. data/lib/action_view/helpers/tags/label.rb +3 -1
  51. data/lib/action_view/helpers/tags/month_field.rb +2 -0
  52. data/lib/action_view/helpers/tags/number_field.rb +2 -0
  53. data/lib/action_view/helpers/tags/password_field.rb +3 -1
  54. data/lib/action_view/helpers/tags/placeholderable.rb +3 -1
  55. data/lib/action_view/helpers/tags/radio_button.rb +7 -5
  56. data/lib/action_view/helpers/tags/range_field.rb +2 -0
  57. data/lib/action_view/helpers/tags/search_field.rb +14 -9
  58. data/lib/action_view/helpers/tags/select.rb +11 -9
  59. data/lib/action_view/helpers/tags/tel_field.rb +2 -0
  60. data/lib/action_view/helpers/tags/text_area.rb +4 -2
  61. data/lib/action_view/helpers/tags/text_field.rb +8 -7
  62. data/lib/action_view/helpers/tags/time_field.rb +2 -0
  63. data/lib/action_view/helpers/tags/time_select.rb +2 -0
  64. data/lib/action_view/helpers/tags/time_zone_select.rb +3 -1
  65. data/lib/action_view/helpers/tags/translator.rb +17 -13
  66. data/lib/action_view/helpers/tags/url_field.rb +2 -0
  67. data/lib/action_view/helpers/tags/week_field.rb +2 -0
  68. data/lib/action_view/helpers/tags.rb +3 -1
  69. data/lib/action_view/helpers/text_helper.rb +55 -36
  70. data/lib/action_view/helpers/translation_helper.rb +74 -32
  71. data/lib/action_view/helpers/url_helper.rb +159 -104
  72. data/lib/action_view/helpers.rb +5 -1
  73. data/lib/action_view/layouts.rb +65 -58
  74. data/lib/action_view/log_subscriber.rb +60 -8
  75. data/lib/action_view/lookup_context.rb +80 -65
  76. data/lib/action_view/model_naming.rb +3 -1
  77. data/lib/action_view/path_set.rb +30 -19
  78. data/lib/action_view/railtie.rb +39 -6
  79. data/lib/action_view/record_identifier.rb +53 -25
  80. data/lib/action_view/renderer/abstract_renderer.rb +21 -15
  81. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +57 -0
  82. data/lib/action_view/renderer/partial_renderer.rb +218 -214
  83. data/lib/action_view/renderer/renderer.rb +8 -6
  84. data/lib/action_view/renderer/streaming_template_renderer.rb +50 -48
  85. data/lib/action_view/renderer/template_renderer.rb +67 -66
  86. data/lib/action_view/rendering.rb +19 -14
  87. data/lib/action_view/routing_url_for.rb +27 -17
  88. data/lib/action_view/tasks/cache_digests.rake +25 -0
  89. data/lib/action_view/template/error.rb +16 -16
  90. data/lib/action_view/template/handlers/builder.rb +10 -11
  91. data/lib/action_view/template/handlers/erb/erubi.rb +83 -0
  92. data/lib/action_view/template/handlers/erb.rb +9 -80
  93. data/lib/action_view/template/handlers/html.rb +11 -0
  94. data/lib/action_view/template/handlers/raw.rb +3 -3
  95. data/lib/action_view/template/handlers.rb +11 -7
  96. data/lib/action_view/template/html.rb +5 -5
  97. data/lib/action_view/template/resolver.rb +140 -115
  98. data/lib/action_view/template/text.rb +8 -9
  99. data/lib/action_view/template/types.rb +18 -18
  100. data/lib/action_view/template.rb +54 -33
  101. data/lib/action_view/test_case.rb +50 -29
  102. data/lib/action_view/testing/resolvers.rb +31 -31
  103. data/lib/action_view/version.rb +3 -1
  104. data/lib/action_view/view_paths.rb +28 -34
  105. data/lib/action_view.rb +8 -7
  106. data/lib/assets/compiled/rails-ujs.js +720 -0
  107. metadata +28 -27
  108. data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,10 +1,12 @@
1
- require 'zlib'
1
+ # frozen_string_literal: true
2
+
3
+ require "zlib"
2
4
 
3
5
  module ActionView
4
6
  # = Action View Asset URL Helpers
5
- module Helpers
7
+ module Helpers #:nodoc:
6
8
  # This module provides methods for generating asset paths and
7
- # urls.
9
+ # URLs.
8
10
  #
9
11
  # image_path("rails.png")
10
12
  # # => "/assets/rails.png"
@@ -27,30 +29,37 @@ module ActionView
27
29
  # Helpers take that into account:
28
30
  #
29
31
  # image_tag("rails.png")
30
- # # => <img alt="Rails" src="http://assets.example.com/assets/rails.png" />
32
+ # # => <img src="http://assets.example.com/assets/rails.png" />
31
33
  # stylesheet_link_tag("application")
32
34
  # # => <link href="http://assets.example.com/assets/application.css" media="screen" rel="stylesheet" />
33
35
  #
34
- # Browsers typically open at most two simultaneous connections to a single
35
- # host, which means your assets often have to wait for other assets to finish
36
- # downloading. You can alleviate this by using a <tt>%d</tt> wildcard in the
37
- # +asset_host+. For example, "assets%d.example.com". If that wildcard is
38
- # present Rails distributes asset requests among the corresponding four hosts
39
- # "assets0.example.com", ..., "assets3.example.com". With this trick browsers
40
- # will open eight simultaneous connections rather than two.
36
+ # Browsers open a limited number of simultaneous connections to a single
37
+ # host. The exact number varies by browser and version. This limit may cause
38
+ # some asset downloads to wait for previous assets to finish before they can
39
+ # begin. You can use the <tt>%d</tt> wildcard in the +asset_host+ to
40
+ # distribute the requests over four hosts. For example,
41
+ # <tt>assets%d.example.com</tt> will spread the asset requests over
42
+ # "assets0.example.com", ..., "assets3.example.com".
41
43
  #
42
44
  # image_tag("rails.png")
43
- # # => <img alt="Rails" src="http://assets0.example.com/assets/rails.png" />
45
+ # # => <img src="http://assets0.example.com/assets/rails.png" />
44
46
  # stylesheet_link_tag("application")
45
47
  # # => <link href="http://assets2.example.com/assets/application.css" media="screen" rel="stylesheet" />
46
48
  #
47
- # To do this, you can either setup four actual hosts, or you can use wildcard
48
- # DNS to CNAME the wildcard to a single asset host. You can read more about
49
- # setting up your DNS CNAME records from your ISP.
49
+ # This may improve the asset loading performance of your application.
50
+ # It is also possible the combination of additional connection overhead
51
+ # (DNS, SSL) and the overall browser connection limits may result in this
52
+ # solution being slower. You should be sure to measure your actual
53
+ # performance across targeted browsers both before and after this change.
54
+ #
55
+ # To implement the corresponding hosts you can either setup four actual
56
+ # hosts or use wildcard DNS to CNAME the wildcard to a single asset host.
57
+ # You can read more about setting up your DNS CNAME records from your ISP.
50
58
  #
51
59
  # Note: This is purely a browser performance optimization and is not meant
52
- # for server load balancing. See http://www.die.net/musings/page_load_time/
53
- # for background.
60
+ # for server load balancing. See https://www.die.net/musings/page_load_time/
61
+ # for background and https://www.browserscope.org/?category=network for
62
+ # connection limit data.
54
63
  #
55
64
  # Alternatively, you can exert more control over the asset host by setting
56
65
  # +asset_host+ to a proc like this:
@@ -59,7 +68,7 @@ module ActionView
59
68
  # "http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com"
60
69
  # }
61
70
  # image_tag("rails.png")
62
- # # => <img alt="Rails" src="http://assets1.example.com/assets/rails.png" />
71
+ # # => <img src="http://assets1.example.com/assets/rails.png" />
63
72
  # stylesheet_link_tag("application")
64
73
  # # => <link href="http://assets2.example.com/assets/application.css" media="screen" rel="stylesheet" />
65
74
  #
@@ -78,7 +87,7 @@ module ActionView
78
87
  # end
79
88
  # }
80
89
  # image_tag("rails.png")
81
- # # => <img alt="Rails" src="http://assets.example.com/assets/rails.png" />
90
+ # # => <img src="http://assets.example.com/assets/rails.png" />
82
91
  # stylesheet_link_tag("application")
83
92
  # # => <link href="http://stylesheets.example.com/assets/application.css" media="screen" rel="stylesheet" />
84
93
  #
@@ -88,9 +97,9 @@ module ActionView
88
97
  # still sending assets for plain HTTP requests from asset hosts. If you don't
89
98
  # have SSL certificates for each of the asset hosts this technique allows you
90
99
  # to avoid warnings in the client about mixed media.
91
- # Note that the request parameter might not be supplied, e.g. when the assets
92
- # are precompiled via a Rake task. Make sure to use a Proc instead of a lambda,
93
- # since a Proc allows missing parameters and sets them to nil.
100
+ # Note that the +request+ parameter might not be supplied, e.g. when the assets
101
+ # are precompiled via a Rake task. Make sure to use a +Proc+ instead of a lambda,
102
+ # since a +Proc+ allows missing parameters and sets them to +nil+.
94
103
  #
95
104
  # config.action_controller.asset_host = Proc.new { |source, request|
96
105
  # if request && request.ssl?
@@ -110,29 +119,86 @@ module ActionView
110
119
  module AssetUrlHelper
111
120
  URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}i
112
121
 
113
- # Computes the path to asset in public directory. If :type
114
- # options is set, a file extension will be appended and scoped
115
- # to the corresponding public directory.
122
+ # This is the entry point for all assets.
123
+ # When using the asset pipeline (i.e. sprockets and sprockets-rails), the
124
+ # behavior is "enhanced". You can bypass the asset pipeline by passing in
125
+ # <tt>skip_pipeline: true</tt> to the options.
116
126
  #
117
127
  # All other asset *_path helpers delegate through this method.
118
128
  #
119
- # asset_path "application.js" # => /assets/application.js
120
- # asset_path "application", type: :javascript # => /assets/application.js
121
- # asset_path "application", type: :stylesheet # => /assets/application.css
122
- # asset_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
129
+ # === With the asset pipeline
130
+ #
131
+ # All options passed to +asset_path+ will be passed to +compute_asset_path+
132
+ # which is implemented by sprockets-rails.
133
+ #
134
+ # asset_path("application.js") # => "/assets/application-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.js"
135
+ #
136
+ # === Without the asset pipeline (<tt>skip_pipeline: true</tt>)
137
+ #
138
+ # Accepts a <tt>type</tt> option that can specify the asset's extension. No error
139
+ # checking is done to verify the source passed into +asset_path+ is valid
140
+ # and that the file exists on disk.
141
+ #
142
+ # asset_path("application.js", skip_pipeline: true) # => "application.js"
143
+ # asset_path("filedoesnotexist.png", skip_pipeline: true) # => "filedoesnotexist.png"
144
+ # asset_path("application", type: :javascript, skip_pipeline: true) # => "/javascripts/application.js"
145
+ # asset_path("application", type: :stylesheet, skip_pipeline: true) # => "/stylesheets/application.css"
146
+ #
147
+ # === Options applying to all assets
148
+ #
149
+ # Below lists scenarios that apply to +asset_path+ whether or not you're
150
+ # using the asset pipeline.
151
+ #
152
+ # - All fully qualified URLs are returned immediately. This bypasses the
153
+ # asset pipeline and all other behavior described.
154
+ #
155
+ # asset_path("http://www.example.com/js/xmlhr.js") # => "http://www.example.com/js/xmlhr.js"
156
+ #
157
+ # - All assets that begin with a forward slash are assumed to be full
158
+ # URLs and will not be expanded. This will bypass the asset pipeline.
159
+ #
160
+ # asset_path("/foo.png") # => "/foo.png"
161
+ #
162
+ # - All blank strings will be returned immediately. This bypasses the
163
+ # asset pipeline and all other behavior described.
164
+ #
165
+ # asset_path("") # => ""
166
+ #
167
+ # - If <tt>config.relative_url_root</tt> is specified, all assets will have that
168
+ # root prepended.
169
+ #
170
+ # Rails.application.config.relative_url_root = "bar"
171
+ # asset_path("foo.js", skip_pipeline: true) # => "bar/foo.js"
172
+ #
173
+ # - A different asset host can be specified via <tt>config.action_controller.asset_host</tt>
174
+ # this is commonly used in conjunction with a CDN.
175
+ #
176
+ # Rails.application.config.action_controller.asset_host = "assets.example.com"
177
+ # asset_path("foo.js", skip_pipeline: true) # => "http://assets.example.com/foo.js"
178
+ #
179
+ # - An extension name can be specified manually with <tt>extname</tt>.
180
+ #
181
+ # asset_path("foo", skip_pipeline: true, extname: ".js") # => "/foo.js"
182
+ # asset_path("foo.css", skip_pipeline: true, extname: ".js") # => "/foo.css.js"
123
183
  def asset_path(source, options = {})
184
+ raise ArgumentError, "nil is not a valid asset source" if source.nil?
185
+
124
186
  source = source.to_s
125
- return "" unless source.present?
126
- return source if source =~ URI_REGEXP
187
+ return "" if source.blank?
188
+ return source if URI_REGEXP.match?(source)
127
189
 
128
- tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
190
+ tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, "".freeze)
129
191
 
130
192
  if extname = compute_asset_extname(source, options)
131
193
  source = "#{source}#{extname}"
132
194
  end
133
195
 
134
196
  if source[0] != ?/
135
- source = compute_asset_path(source, options)
197
+ if options[:skip_pipeline]
198
+ source = public_compute_asset_path(source, options)
199
+ else
200
+ source = compute_asset_path(source, options)
201
+ end
136
202
  end
137
203
 
138
204
  relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
@@ -159,31 +225,35 @@ module ActionView
159
225
  # asset_url "application.js", host: "http://cdn.example.com" # => http://cdn.example.com/assets/application.js
160
226
  #
161
227
  def asset_url(source, options = {})
162
- path_to_asset(source, options.merge(:protocol => :request))
228
+ path_to_asset(source, options.merge(protocol: :request))
163
229
  end
164
230
  alias_method :url_to_asset, :asset_url # aliased to avoid conflicts with an asset_url named route
165
231
 
166
232
  ASSET_EXTENSIONS = {
167
- javascript: '.js',
168
- stylesheet: '.css'
233
+ javascript: ".js",
234
+ stylesheet: ".css"
169
235
  }
170
236
 
171
- # Compute extname to append to asset path. Returns nil if
237
+ # Compute extname to append to asset path. Returns +nil+ if
172
238
  # nothing should be added.
173
239
  def compute_asset_extname(source, options = {})
174
240
  return if options[:extname] == false
175
241
  extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
176
- extname if extname && File.extname(source) != extname
242
+ if extname && File.extname(source) != extname
243
+ extname
244
+ else
245
+ nil
246
+ end
177
247
  end
178
248
 
179
249
  # Maps asset types to public directory.
180
250
  ASSET_PUBLIC_DIRECTORIES = {
181
- audio: '/audios',
182
- font: '/fonts',
183
- image: '/images',
184
- javascript: '/javascripts',
185
- stylesheet: '/stylesheets',
186
- video: '/videos'
251
+ audio: "/audios",
252
+ font: "/fonts",
253
+ image: "/images",
254
+ javascript: "/javascripts",
255
+ stylesheet: "/stylesheets",
256
+ video: "/videos"
187
257
  }
188
258
 
189
259
  # Computes asset path to public directory. Plugins and
@@ -193,6 +263,7 @@ module ActionView
193
263
  dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
194
264
  File.join(dir, source)
195
265
  end
266
+ alias :public_compute_asset_path :compute_asset_path
196
267
 
197
268
  # Pick an asset host for this source. Returns +nil+ if no host is set,
198
269
  # the host if no wildcard is set, the host interpolated with the
@@ -204,19 +275,21 @@ module ActionView
204
275
  host = options[:host]
205
276
  host ||= config.asset_host if defined? config.asset_host
206
277
 
207
- if host.respond_to?(:call)
208
- arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
209
- args = [source]
210
- args << request if request && (arity > 1 || arity < 0)
211
- host = host.call(*args)
212
- elsif host =~ /%d/
213
- host = host % (Zlib.crc32(source) % 4)
278
+ if host
279
+ if host.respond_to?(:call)
280
+ arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
281
+ args = [source]
282
+ args << request if request && (arity > 1 || arity < 0)
283
+ host = host.call(*args)
284
+ elsif host.include?("%d")
285
+ host = host % (Zlib.crc32(source) % 4)
286
+ end
214
287
  end
215
288
 
216
289
  host ||= request.base_url if request && options[:protocol] == :request
217
290
  return unless host
218
291
 
219
- if host =~ URI_REGEXP
292
+ if URI_REGEXP.match?(host)
220
293
  host
221
294
  else
222
295
  protocol = options[:protocol] || config.default_asset_host_protocol || (request ? :request : :relative)
@@ -242,14 +315,19 @@ module ActionView
242
315
  # javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
243
316
  # javascript_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
244
317
  def javascript_path(source, options = {})
245
- path_to_asset(source, {type: :javascript}.merge!(options))
318
+ path_to_asset(source, { type: :javascript }.merge!(options))
246
319
  end
247
320
  alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
248
321
 
249
322
  # Computes the full URL to a JavaScript asset in the public javascripts directory.
250
323
  # This will use +javascript_path+ internally, so most of their behaviors will be the same.
324
+ # Since +javascript_url+ is based on +asset_url+ method you can set :host options. If :host
325
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
326
+ #
327
+ # javascript_url "js/xmlhr.js", host: "http://stage.example.com" # => http://stage.example.com/assets/js/xmlhr.js
328
+ #
251
329
  def javascript_url(source, options = {})
252
- url_to_asset(source, {type: :javascript}.merge!(options))
330
+ url_to_asset(source, { type: :javascript }.merge!(options))
253
331
  end
254
332
  alias_method :url_to_javascript, :javascript_url # aliased to avoid conflicts with a javascript_url named route
255
333
 
@@ -264,14 +342,19 @@ module ActionView
264
342
  # stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style
265
343
  # stylesheet_path "http://www.example.com/css/style.css" # => http://www.example.com/css/style.css
266
344
  def stylesheet_path(source, options = {})
267
- path_to_asset(source, {type: :stylesheet}.merge!(options))
345
+ path_to_asset(source, { type: :stylesheet }.merge!(options))
268
346
  end
269
347
  alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
270
348
 
271
349
  # Computes the full URL to a stylesheet asset in the public stylesheets directory.
272
350
  # This will use +stylesheet_path+ internally, so most of their behaviors will be the same.
351
+ # Since +stylesheet_url+ is based on +asset_url+ method you can set :host options. If :host
352
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
353
+ #
354
+ # stylesheet_url "css/style.css", host: "http://stage.example.com" # => http://stage.example.com/assets/css/style.css
355
+ #
273
356
  def stylesheet_url(source, options = {})
274
- url_to_asset(source, {type: :stylesheet}.merge!(options))
357
+ url_to_asset(source, { type: :stylesheet }.merge!(options))
275
358
  end
276
359
  alias_method :url_to_stylesheet, :stylesheet_url # aliased to avoid conflicts with a stylesheet_url named route
277
360
 
@@ -289,14 +372,19 @@ module ActionView
289
372
  # The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
290
373
  # plugin authors are encouraged to do so.
291
374
  def image_path(source, options = {})
292
- path_to_asset(source, {type: :image}.merge!(options))
375
+ path_to_asset(source, { type: :image }.merge!(options))
293
376
  end
294
377
  alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
295
378
 
296
379
  # Computes the full URL to an image asset.
297
380
  # This will use +image_path+ internally, so most of their behaviors will be the same.
381
+ # Since +image_url+ is based on +asset_url+ method you can set :host options. If :host
382
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
383
+ #
384
+ # image_url "edit.png", host: "http://stage.example.com" # => http://stage.example.com/assets/edit.png
385
+ #
298
386
  def image_url(source, options = {})
299
- url_to_asset(source, {type: :image}.merge!(options))
387
+ url_to_asset(source, { type: :image }.merge!(options))
300
388
  end
301
389
  alias_method :url_to_image, :image_url # aliased to avoid conflicts with an image_url named route
302
390
 
@@ -310,16 +398,21 @@ module ActionView
310
398
  # video_path("/trailers/hd.avi") # => /trailers/hd.avi
311
399
  # video_path("http://www.example.com/vid/hd.avi") # => http://www.example.com/vid/hd.avi
312
400
  def video_path(source, options = {})
313
- path_to_asset(source, {type: :video}.merge!(options))
401
+ path_to_asset(source, { type: :video }.merge!(options))
314
402
  end
315
403
  alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
316
404
 
317
405
  # Computes the full URL to a video asset in the public videos directory.
318
406
  # This will use +video_path+ internally, so most of their behaviors will be the same.
407
+ # Since +video_url+ is based on +asset_url+ method you can set :host options. If :host
408
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
409
+ #
410
+ # video_url "hd.avi", host: "http://stage.example.com" # => http://stage.example.com/videos/hd.avi
411
+ #
319
412
  def video_url(source, options = {})
320
- url_to_asset(source, {type: :video}.merge!(options))
413
+ url_to_asset(source, { type: :video }.merge!(options))
321
414
  end
322
- alias_method :url_to_video, :video_url # aliased to avoid conflicts with an video_url named route
415
+ alias_method :url_to_video, :video_url # aliased to avoid conflicts with a video_url named route
323
416
 
324
417
  # Computes the path to an audio asset in the public audios directory.
325
418
  # Full paths from the document root will be passed through.
@@ -331,14 +424,19 @@ module ActionView
331
424
  # audio_path("/sounds/horse.wav") # => /sounds/horse.wav
332
425
  # audio_path("http://www.example.com/sounds/horse.wav") # => http://www.example.com/sounds/horse.wav
333
426
  def audio_path(source, options = {})
334
- path_to_asset(source, {type: :audio}.merge!(options))
427
+ path_to_asset(source, { type: :audio }.merge!(options))
335
428
  end
336
429
  alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route
337
430
 
338
431
  # Computes the full URL to an audio asset in the public audios directory.
339
432
  # This will use +audio_path+ internally, so most of their behaviors will be the same.
433
+ # Since +audio_url+ is based on +asset_url+ method you can set :host options. If :host
434
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
435
+ #
436
+ # audio_url "horse.wav", host: "http://stage.example.com" # => http://stage.example.com/audios/horse.wav
437
+ #
340
438
  def audio_url(source, options = {})
341
- url_to_asset(source, {type: :audio}.merge!(options))
439
+ url_to_asset(source, { type: :audio }.merge!(options))
342
440
  end
343
441
  alias_method :url_to_audio, :audio_url # aliased to avoid conflicts with an audio_url named route
344
442
 
@@ -351,16 +449,21 @@ module ActionView
351
449
  # font_path("/dir/font.ttf") # => /dir/font.ttf
352
450
  # font_path("http://www.example.com/dir/font.ttf") # => http://www.example.com/dir/font.ttf
353
451
  def font_path(source, options = {})
354
- path_to_asset(source, {type: :font}.merge!(options))
452
+ path_to_asset(source, { type: :font }.merge!(options))
355
453
  end
356
- alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
454
+ alias_method :path_to_font, :font_path # aliased to avoid conflicts with a font_path named route
357
455
 
358
456
  # Computes the full URL to a font asset.
359
457
  # This will use +font_path+ internally, so most of their behaviors will be the same.
458
+ # Since +font_url+ is based on +asset_url+ method you can set :host options. If :host
459
+ # options is set, it overwrites global +config.action_controller.asset_host+ setting.
460
+ #
461
+ # font_url "font.ttf", host: "http://stage.example.com" # => http://stage.example.com/fonts/font.ttf
462
+ #
360
463
  def font_url(source, options = {})
361
- url_to_asset(source, {type: :font}.merge!(options))
464
+ url_to_asset(source, { type: :font }.merge!(options))
362
465
  end
363
- alias_method :url_to_font, :font_url # aliased to avoid conflicts with an font_url named route
466
+ alias_method :url_to_font, :font_url # aliased to avoid conflicts with a font_url named route
364
467
  end
365
468
  end
366
469
  end
@@ -1,8 +1,10 @@
1
- require 'set'
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
2
4
 
3
5
  module ActionView
4
6
  # = Action View Atom Feed Helpers
5
- module Helpers
7
+ module Helpers #:nodoc:
6
8
  module AtomFeedHelper
7
9
  # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other
8
10
  # template languages).
@@ -16,7 +18,7 @@ module ActionView
16
18
  # end
17
19
  #
18
20
  # app/controllers/posts_controller.rb:
19
- # class PostsController < ApplicationController::Base
21
+ # class PostsController < ApplicationController
20
22
  # # GET /posts.html
21
23
  # # GET /posts.atom
22
24
  # def index
@@ -51,7 +53,7 @@ module ActionView
51
53
  # * <tt>:language</tt>: Defaults to "en-US".
52
54
  # * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
53
55
  # * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
54
- # * <tt>:id</tt>: The id for this feed. Defaults to "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}"
56
+ # * <tt>:id</tt>: The id for this feed. Defaults to "tag:localhost,2005:/posts", in this case.
55
57
  # * <tt>:schema_date</tt>: The date at which the tag scheme for the feed was first used. A good default is the year you
56
58
  # created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified,
57
59
  # 2005 is used (as an "I don't care" value).
@@ -103,7 +105,7 @@ module ActionView
103
105
  xml = options.delete(:xml) || eval("xml", block.binding)
104
106
  xml.instruct!
105
107
  if options[:instruct]
106
- options[:instruct].each do |target,attrs|
108
+ options[:instruct].each do |target, attrs|
107
109
  if attrs.respond_to?(:keys)
108
110
  xml.instruct!(target, attrs)
109
111
  elsif attrs.respond_to?(:each)
@@ -112,13 +114,13 @@ module ActionView
112
114
  end
113
115
  end
114
116
 
115
- feed_opts = {"xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom'}
116
- feed_opts.merge!(options).reject!{|k,v| !k.to_s.match(/^xml/)}
117
+ feed_opts = { "xml:lang" => options[:language] || "en-US", "xmlns" => "http://www.w3.org/2005/Atom" }
118
+ feed_opts.merge!(options).reject! { |k, v| !k.to_s.match(/^xml/) }
117
119
 
118
120
  xml.feed(feed_opts) do
119
121
  xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}")
120
- xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
121
- xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
122
+ xml.link(rel: "alternate", type: "text/html", href: options[:root_url] || (request.protocol + request.host_with_port))
123
+ xml.link(rel: "self", type: "application/atom+xml", href: options[:url] || request.url)
122
124
 
123
125
  yield AtomFeedBuilder.new(xml, self, options)
124
126
  end
@@ -132,13 +134,13 @@ module ActionView
132
134
  end
133
135
 
134
136
  private
135
- # Delegate to xml builder, first wrapping the element in a xhtml
137
+ # Delegate to xml builder, first wrapping the element in an xhtml
136
138
  # namespaced div element if the method and arguments indicate
137
139
  # that an xhtml_block? is desired.
138
140
  def method_missing(method, *arguments, &block)
139
141
  if xhtml_block?(method, arguments)
140
142
  @xml.__send__(method, *arguments) do
141
- @xml.div(:xmlns => 'http://www.w3.org/1999/xhtml') do |xhtml|
143
+ @xml.div(xmlns: "http://www.w3.org/1999/xhtml") do |xhtml|
142
144
  block.call(xhtml)
143
145
  end
144
146
  end
@@ -153,7 +155,7 @@ module ActionView
153
155
  def xhtml_block?(method, arguments)
154
156
  if XHTML_TAG_NAMES.include?(method.to_s)
155
157
  last = arguments.last
156
- last.is_a?(Hash) && last[:type].to_s == 'xhtml'
158
+ last.is_a?(Hash) && last[:type].to_s == "xhtml"
157
159
  end
158
160
  end
159
161
  end
@@ -163,7 +165,7 @@ module ActionView
163
165
  @xml, @view, @feed_options = xml, view, feed_options
164
166
  end
165
167
 
166
- # Accepts a Date or Time object and inserts it in the proper format. If nil is passed, current time in UTC is used.
168
+ # Accepts a Date or Time object and inserts it in the proper format. If +nil+ is passed, current time in UTC is used.
167
169
  def updated(date_or_time = nil)
168
170
  @xml.updated((date_or_time || Time.now.utc).xmlschema)
169
171
  end
@@ -174,7 +176,7 @@ module ActionView
174
176
  #
175
177
  # * <tt>:published</tt>: Time first published. Defaults to the created_at attribute on the record if one such exists.
176
178
  # * <tt>:updated</tt>: Time of update. Defaults to the updated_at attribute on the record if one such exists.
177
- # * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record.
179
+ # * <tt>:url</tt>: The URL for this entry or +false+ or +nil+ for not having a link tag. Defaults to the +polymorphic_url+ for the record.
178
180
  # * <tt>:id</tt>: The ID for this entry. Defaults to "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}"
179
181
  # * <tt>:type</tt>: The TYPE for this entry. Defaults to "text/html".
180
182
  def entry(record, options = {})
@@ -189,15 +191,15 @@ module ActionView
189
191
  @xml.updated((options[:updated] || record.updated_at).xmlschema)
190
192
  end
191
193
 
192
- type = options.fetch(:type, 'text/html')
194
+ type = options.fetch(:type, "text/html")
193
195
 
194
- @xml.link(:rel => 'alternate', :type => type, :href => options[:url] || @view.polymorphic_url(record))
196
+ url = options.fetch(:url) { @view.polymorphic_url(record) }
197
+ @xml.link(rel: "alternate", type: type, href: url) if url
195
198
 
196
199
  yield AtomBuilder.new(@xml)
197
200
  end
198
201
  end
199
202
  end
200
-
201
203
  end
202
204
  end
203
205
  end