actionview 4.2.11.1 → 6.0.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 (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +201 -192
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +9 -8
  5. data/lib/action_view/base.rb +144 -37
  6. data/lib/action_view/buffers.rb +18 -1
  7. data/lib/action_view/cache_expiry.rb +53 -0
  8. data/lib/action_view/context.rb +8 -12
  9. data/lib/action_view/dependency_tracker.rb +54 -20
  10. data/lib/action_view/digestor.rb +88 -85
  11. data/lib/action_view/flows.rb +11 -12
  12. data/lib/action_view/gem_version.rb +6 -4
  13. data/lib/action_view/helpers/active_model_helper.rb +16 -11
  14. data/lib/action_view/helpers/asset_tag_helper.rb +241 -82
  15. data/lib/action_view/helpers/asset_url_helper.rb +171 -67
  16. data/lib/action_view/helpers/atom_feed_helper.rb +19 -17
  17. data/lib/action_view/helpers/cache_helper.rb +112 -42
  18. data/lib/action_view/helpers/capture_helper.rb +20 -13
  19. data/lib/action_view/helpers/controller_helper.rb +15 -4
  20. data/lib/action_view/helpers/csp_helper.rb +26 -0
  21. data/lib/action_view/helpers/csrf_helper.rb +8 -6
  22. data/lib/action_view/helpers/date_helper.rb +230 -129
  23. data/lib/action_view/helpers/debug_helper.rb +7 -6
  24. data/lib/action_view/helpers/form_helper.rb +755 -129
  25. data/lib/action_view/helpers/form_options_helper.rb +130 -75
  26. data/lib/action_view/helpers/form_tag_helper.rb +116 -71
  27. data/lib/action_view/helpers/javascript_helper.rb +30 -14
  28. data/lib/action_view/helpers/number_helper.rb +84 -59
  29. data/lib/action_view/helpers/output_safety_helper.rb +36 -4
  30. data/lib/action_view/helpers/rendering_helper.rb +11 -8
  31. data/lib/action_view/helpers/sanitize_helper.rb +30 -31
  32. data/lib/action_view/helpers/tag_helper.rb +201 -75
  33. data/lib/action_view/helpers/tags/base.rb +138 -98
  34. data/lib/action_view/helpers/tags/check_box.rb +20 -19
  35. data/lib/action_view/helpers/tags/checkable.rb +4 -2
  36. data/lib/action_view/helpers/tags/collection_check_boxes.rb +12 -34
  37. data/lib/action_view/helpers/tags/collection_helpers.rb +69 -36
  38. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +6 -12
  39. data/lib/action_view/helpers/tags/collection_select.rb +4 -2
  40. data/lib/action_view/helpers/tags/color_field.rb +4 -3
  41. data/lib/action_view/helpers/tags/date_field.rb +2 -1
  42. data/lib/action_view/helpers/tags/date_select.rb +37 -36
  43. data/lib/action_view/helpers/tags/datetime_field.rb +4 -3
  44. data/lib/action_view/helpers/tags/datetime_local_field.rb +2 -1
  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 -2
  51. data/lib/action_view/helpers/tags/month_field.rb +2 -1
  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 -6
  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 -10
  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 -8
  62. data/lib/action_view/helpers/tags/time_field.rb +2 -1
  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 +15 -16
  66. data/lib/action_view/helpers/tags/url_field.rb +2 -0
  67. data/lib/action_view/helpers/tags/week_field.rb +2 -1
  68. data/lib/action_view/helpers/tags.rb +3 -1
  69. data/lib/action_view/helpers/text_helper.rb +56 -38
  70. data/lib/action_view/helpers/translation_helper.rb +91 -47
  71. data/lib/action_view/helpers/url_helper.rb +160 -105
  72. data/lib/action_view/helpers.rb +5 -3
  73. data/lib/action_view/layouts.rb +65 -61
  74. data/lib/action_view/log_subscriber.rb +61 -10
  75. data/lib/action_view/lookup_context.rb +147 -89
  76. data/lib/action_view/model_naming.rb +3 -1
  77. data/lib/action_view/path_set.rb +28 -23
  78. data/lib/action_view/railtie.rb +62 -6
  79. data/lib/action_view/record_identifier.rb +53 -26
  80. data/lib/action_view/renderer/abstract_renderer.rb +71 -13
  81. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +103 -0
  82. data/lib/action_view/renderer/partial_renderer.rb +239 -225
  83. data/lib/action_view/renderer/renderer.rb +22 -8
  84. data/lib/action_view/renderer/streaming_template_renderer.rb +54 -54
  85. data/lib/action_view/renderer/template_renderer.rb +79 -73
  86. data/lib/action_view/rendering.rb +68 -44
  87. data/lib/action_view/routing_url_for.rb +33 -22
  88. data/lib/action_view/tasks/cache_digests.rake +25 -0
  89. data/lib/action_view/template/error.rb +44 -29
  90. data/lib/action_view/template/handlers/builder.rb +12 -13
  91. data/lib/action_view/template/handlers/erb/erubi.rb +87 -0
  92. data/lib/action_view/template/handlers/erb.rb +24 -86
  93. data/lib/action_view/template/handlers/html.rb +11 -0
  94. data/lib/action_view/template/handlers/raw.rb +4 -4
  95. data/lib/action_view/template/handlers.rb +38 -8
  96. data/lib/action_view/template/html.rb +19 -10
  97. data/lib/action_view/template/inline.rb +22 -0
  98. data/lib/action_view/template/raw_file.rb +28 -0
  99. data/lib/action_view/template/resolver.rb +217 -193
  100. data/lib/action_view/template/sources/file.rb +17 -0
  101. data/lib/action_view/template/sources.rb +13 -0
  102. data/lib/action_view/template/text.rb +11 -10
  103. data/lib/action_view/template/types.rb +18 -18
  104. data/lib/action_view/template.rb +146 -90
  105. data/lib/action_view/test_case.rb +52 -32
  106. data/lib/action_view/testing/resolvers.rb +46 -34
  107. data/lib/action_view/unbound_template.rb +31 -0
  108. data/lib/action_view/version.rb +3 -1
  109. data/lib/action_view/view_paths.rb +48 -31
  110. data/lib/action_view.rb +11 -8
  111. data/lib/assets/compiled/rails-ujs.js +746 -0
  112. metadata +38 -29
  113. data/lib/action_view/helpers/record_tag_helper.rb +0 -108
  114. data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionView
2
4
  # = Action View Cache Helper
3
- module Helpers
5
+ module Helpers #:nodoc:
4
6
  module CacheHelper
5
7
  # This helper exposes a method for caching fragments of a view
6
8
  # rather than an entire action or page. This technique is useful
@@ -8,10 +10,9 @@ module ActionView
8
10
  # fragments, and so on. This method takes a block that contains
9
11
  # the content you wish to cache.
10
12
  #
11
- # The best way to use this is by doing key-based cache expiration
12
- # on top of a cache store like Memcached that'll automatically
13
- # kick out old entries. For more on key-based expiration, see:
14
- # http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works
13
+ # The best way to use this is by doing recyclable key-based cache expiration
14
+ # on top of a cache store like Memcached or Redis that'll automatically
15
+ # kick out old entries.
15
16
  #
16
17
  # When using this method, you list the cache dependency as the name of the cache, like so:
17
18
  #
@@ -23,10 +24,14 @@ module ActionView
23
24
  # This approach will assume that when a new topic is added, you'll touch
24
25
  # the project. The cache key generated from this call will be something like:
25
26
  #
26
- # views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9
27
- # ^class ^id ^updated_at ^template tree digest
27
+ # views/template/action.html.erb:7a1156131a6928cb0026877f8b749ac9/projects/123
28
+ # ^template path ^template tree digest ^class ^id
28
29
  #
29
- # The cache is thus automatically bumped whenever the project updated_at is touched.
30
+ # This cache key is stable, but it's combined with a cache version derived from the project
31
+ # record. When the project updated_at is touched, the #cache_version changes, even
32
+ # if the key stays stable. This means that unlike a traditional key-based cache expiration
33
+ # approach, you won't be generating cache trash, unused keys, simply because the dependent
34
+ # record is updated.
30
35
  #
31
36
  # If your template cache depends on multiple sources (try to avoid this to keep things simple),
32
37
  # you can name all these dependencies as part of an array:
@@ -39,13 +44,13 @@ module ActionView
39
44
  # This will include both records as part of the cache key and updating either of them will
40
45
  # expire the cache.
41
46
  #
42
- # ==== Template digest
47
+ # ==== \Template digest
43
48
  #
44
- # The template digest that's added to the cache key is computed by taking an md5 of the
49
+ # The template digest that's added to the cache key is computed by taking an MD5 of the
45
50
  # contents of the entire template file. This ensures that your caches will automatically
46
51
  # expire when you change the template file.
47
52
  #
48
- # Note that the md5 is taken of the entire template file, not just what's within the
53
+ # Note that the MD5 is taken of the entire template file, not just what's within the
49
54
  # cache do/end call. So it's possible that changing something outside of that call will
50
55
  # still expire the cache.
51
56
  #
@@ -69,13 +74,14 @@ module ActionView
69
74
  # render 'comments/comments'
70
75
  # render('comments/comments')
71
76
  #
72
- # render "header" => render("comments/header")
77
+ # render "header" translates to render("comments/header")
73
78
  #
74
- # render(@topic) => render("topics/topic")
75
- # render(topics) => render("topics/topic")
76
- # render(message.topics) => render("topics/topic")
79
+ # render(@topic) translates to render("topics/topic")
80
+ # render(topics) translates to render("topics/topic")
81
+ # render(message.topics) translates to render("topics/topic")
77
82
  #
78
- # It's not possible to derive all render calls like that, though. Here are a few examples of things that can't be derived:
83
+ # It's not possible to derive all render calls like that, though.
84
+ # Here are a few examples of things that can't be derived:
79
85
  #
80
86
  # render group_of_attachments
81
87
  # render @project.documents.where(published: true).order('created_at')
@@ -87,7 +93,7 @@ module ActionView
87
93
  #
88
94
  # === Explicit dependencies
89
95
  #
90
- # Some times you'll have template dependencies that can't be derived at all. This is typically
96
+ # Sometimes you'll have template dependencies that can't be derived at all. This is typically
91
97
  # the case when you have template rendering that happens in helpers. Here's an example:
92
98
  #
93
99
  # <%= render_sortable_todolists @project.todolists %>
@@ -97,22 +103,70 @@ module ActionView
97
103
  # <%# Template Dependency: todolists/todolist %>
98
104
  # <%= render_sortable_todolists @project.todolists %>
99
105
  #
100
- # The pattern used to match these is /# Template Dependency: ([^ ]+)/, so it's important that you type it out just so.
106
+ # In some cases, like a single table inheritance setup, you might have
107
+ # a bunch of explicit dependencies. Instead of writing every template out,
108
+ # you can use a wildcard to match any template in a directory:
109
+ #
110
+ # <%# Template Dependency: events/* %>
111
+ # <%= render_categorizable_events @person.events %>
112
+ #
113
+ # This marks every template in the directory as a dependency. To find those
114
+ # templates, the wildcard path must be absolutely defined from <tt>app/views</tt> or paths
115
+ # otherwise added with +prepend_view_path+ or +append_view_path+.
116
+ # This way the wildcard for <tt>app/views/recordings/events</tt> would be <tt>recordings/events/*</tt> etc.
117
+ #
118
+ # The pattern used to match explicit dependencies is <tt>/# Template Dependency: (\S+)/</tt>,
119
+ # so it's important that you type it out just so.
101
120
  # You can only declare one template dependency per line.
102
121
  #
103
122
  # === External dependencies
104
123
  #
105
- # If you use a helper method, for example, inside of a cached block and you then update that helper,
106
- # you'll have to bump the cache as well. It doesn't really matter how you do it, but the md5 of the template file
124
+ # If you use a helper method, for example, inside a cached block and
125
+ # you then update that helper, you'll have to bump the cache as well.
126
+ # It doesn't really matter how you do it, but the MD5 of the template file
107
127
  # must change. One recommendation is to simply be explicit in a comment, like:
108
128
  #
109
129
  # <%# Helper Dependency Updated: May 6, 2012 at 6pm %>
110
130
  # <%= some_helper_method(person) %>
111
131
  #
112
- # Now all you'll have to do is change that timestamp when the helper method changes.
113
- def cache(name = {}, options = nil, &block)
132
+ # Now all you have to do is change that timestamp when the helper method changes.
133
+ #
134
+ # === Collection Caching
135
+ #
136
+ # When rendering a collection of objects that each use the same partial, a <tt>:cached</tt>
137
+ # option can be passed.
138
+ #
139
+ # For collections rendered such:
140
+ #
141
+ # <%= render partial: 'projects/project', collection: @projects, cached: true %>
142
+ #
143
+ # The <tt>cached: true</tt> will make Action View's rendering read several templates
144
+ # from cache at once instead of one call per template.
145
+ #
146
+ # Templates in the collection not already cached are written to cache.
147
+ #
148
+ # Works great alongside individual template fragment caching.
149
+ # For instance if the template the collection renders is cached like:
150
+ #
151
+ # # projects/_project.html.erb
152
+ # <% cache project do %>
153
+ # <%# ... %>
154
+ # <% end %>
155
+ #
156
+ # Any collection renders will find those cached templates when attempting
157
+ # to read multiple templates at once.
158
+ #
159
+ # If your collection cache depends on multiple sources (try to avoid this to keep things simple),
160
+ # you can name all these dependencies as part of a block that returns an array:
161
+ #
162
+ # <%= render partial: 'projects/project', collection: @projects, cached: -> project { [ project, current_user ] } %>
163
+ #
164
+ # This will include both records as part of the cache key and updating either of them will
165
+ # expire the cache.
166
+ def cache(name = {}, options = {}, &block)
114
167
  if controller.respond_to?(:perform_caching) && controller.perform_caching
115
- safe_concat(fragment_for(cache_fragment_name(name, options), options, &block))
168
+ name_options = options.slice(:skip_digest, :virtual_path)
169
+ safe_concat(fragment_for(cache_fragment_name(name, **name_options), options, &block))
116
170
  else
117
171
  yield
118
172
  end
@@ -126,7 +180,7 @@ module ActionView
126
180
  # <b>All the topics on this project</b>
127
181
  # <%= render project.topics %>
128
182
  # <% end %>
129
- def cache_if(condition, name = {}, options = nil, &block)
183
+ def cache_if(condition, name = {}, options = {}, &block)
130
184
  if condition
131
185
  cache(name, options, &block)
132
186
  else
@@ -142,50 +196,66 @@ module ActionView
142
196
  # <b>All the topics on this project</b>
143
197
  # <%= render project.topics %>
144
198
  # <% end %>
145
- def cache_unless(condition, name = {}, options = nil, &block)
199
+ def cache_unless(condition, name = {}, options = {}, &block)
146
200
  cache_if !condition, name, options, &block
147
201
  end
148
202
 
149
203
  # This helper returns the name of a cache key for a given fragment cache
150
- # call. By supplying skip_digest: true to cache, the digestion of cache
204
+ # call. By supplying <tt>skip_digest: true</tt> to cache, the digestion of cache
151
205
  # fragments can be manually bypassed. This is useful when cache fragments
152
206
  # cannot be manually expired unless you know the exact key which is the
153
207
  # case when using memcached.
154
- def cache_fragment_name(name = {}, options = nil)
155
- skip_digest = options && options[:skip_digest]
156
-
208
+ #
209
+ # The digest will be generated using +virtual_path:+ if it is provided.
210
+ #
211
+ def cache_fragment_name(name = {}, skip_digest: nil, virtual_path: nil, digest_path: nil)
157
212
  if skip_digest
158
213
  name
159
214
  else
160
- fragment_name_with_digest(name)
215
+ fragment_name_with_digest(name, virtual_path, digest_path)
216
+ end
217
+ end
218
+
219
+ def digest_path_from_template(template) # :nodoc:
220
+ digest = Digestor.digest(name: template.virtual_path, format: template.format, finder: lookup_context, dependencies: view_cache_dependencies)
221
+
222
+ if digest.present?
223
+ "#{template.virtual_path}:#{digest}"
224
+ else
225
+ template.virtual_path
161
226
  end
162
227
  end
163
228
 
164
229
  private
230
+ def fragment_name_with_digest(name, virtual_path, digest_path)
231
+ virtual_path ||= @virtual_path
165
232
 
166
- def fragment_name_with_digest(name) #:nodoc:
167
- if @virtual_path
168
- names = Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name)
169
- digest = Digestor.digest name: @virtual_path, finder: lookup_context, dependencies: view_cache_dependencies
233
+ if virtual_path || digest_path
234
+ name = controller.url_for(name).split("://").last if name.is_a?(Hash)
170
235
 
171
- [ *names, digest ]
236
+ digest_path ||= digest_path_from_template(@current_template)
237
+
238
+ [ digest_path, name ]
172
239
  else
173
240
  name
174
241
  end
175
242
  end
176
243
 
177
- # TODO: Create an object that has caching read/write on it
178
- def fragment_for(name = {}, options = nil, &block) #:nodoc:
179
- read_fragment_for(name, options) || write_fragment_for(name, options, &block)
244
+ def fragment_for(name = {}, options = nil, &block)
245
+ if content = read_fragment_for(name, options)
246
+ @view_renderer.cache_hits[@virtual_path] = :hit if defined?(@view_renderer)
247
+ content
248
+ else
249
+ @view_renderer.cache_hits[@virtual_path] = :miss if defined?(@view_renderer)
250
+ write_fragment_for(name, options, &block)
251
+ end
180
252
  end
181
253
 
182
- def read_fragment_for(name, options) #:nodoc:
254
+ def read_fragment_for(name, options)
183
255
  controller.read_fragment(name, options)
184
256
  end
185
257
 
186
- def write_fragment_for(name, options) #:nodoc:
187
- # VIEW TODO: Make #capture usable outside of ERB
188
- # This dance is needed because Builder can't use capture
258
+ def write_fragment_for(name, options)
189
259
  pos = output_buffer.length
190
260
  yield
191
261
  output_safe = output_buffer.html_safe?
@@ -1,16 +1,18 @@
1
- require 'active_support/core_ext/string/output_safety'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/output_safety"
2
4
 
3
5
  module ActionView
4
6
  # = Action View Capture Helper
5
- module Helpers
7
+ module Helpers #:nodoc:
6
8
  # CaptureHelper exposes methods to let you extract generated markup which
7
9
  # can be used in other parts of a template or layout file.
8
10
  #
9
11
  # It provides a method to capture blocks into variables through capture and
10
- # a way to capture a block of markup for use in a layout through content_for.
12
+ # a way to capture a block of markup for use in a layout through {content_for}[rdoc-ref:ActionView::Helpers::CaptureHelper#content_for].
11
13
  module CaptureHelper
12
- # The capture method allows you to extract part of a template into a
13
- # variable. You can then use this variable anywhere in your templates or layout.
14
+ # The capture method extracts part of a template as a String object.
15
+ # You can then use this object anywhere in your templates, layout, or helpers.
14
16
  #
15
17
  # The capture method can be used in ERB templates...
16
18
  #
@@ -31,17 +33,22 @@ module ActionView
31
33
  # <head><title><%= @greeting %></title></head>
32
34
  # <body>
33
35
  # <b><%= @greeting %></b>
34
- # </body></html>
36
+ # </body>
37
+ # </html>
38
+ #
39
+ # The return of capture is the string generated by the block. For Example:
40
+ #
41
+ # @greeting # => "Welcome to my shiny new web page! The date and time is 2018-09-06 11:09:16 -0500"
35
42
  #
36
43
  def capture(*args)
37
44
  value = nil
38
45
  buffer = with_output_buffer { value = yield(*args) }
39
- if string = buffer.presence || value and string.is_a?(String)
46
+ if (string = buffer.presence || value) && string.is_a?(String)
40
47
  ERB::Util.html_escape string
41
48
  end
42
49
  end
43
50
 
44
- # Calling content_for stores a block of markup in an identifier for later use.
51
+ # Calling <tt>content_for</tt> stores a block of markup in an identifier for later use.
45
52
  # In order to access this stored content in other templates, helper modules
46
53
  # or the layout, you would pass the identifier as an argument to <tt>content_for</tt>.
47
54
  #
@@ -107,14 +114,14 @@ module ActionView
107
114
  # That will place +script+ tags for your default set of JavaScript files on the page;
108
115
  # this technique is useful if you'll only be using these scripts in a few views.
109
116
  #
110
- # Note that content_for concatenates (default) the blocks it is given for a particular
117
+ # Note that <tt>content_for</tt> concatenates (default) the blocks it is given for a particular
111
118
  # identifier in order. For example:
112
119
  #
113
120
  # <% content_for :navigation do %>
114
121
  # <li><%= link_to 'Home', action: 'index' %></li>
115
122
  # <% end %>
116
123
  #
117
- # And in other place:
124
+ # And in another place:
118
125
  #
119
126
  # <% content_for :navigation do %>
120
127
  # <li><%= link_to 'Login', action: 'login' %></li>
@@ -124,7 +131,7 @@ module ActionView
124
131
  #
125
132
  # <ul><%= content_for :navigation %></ul>
126
133
  #
127
- # If the flush parameter is true content_for replaces the blocks it is given. For example:
134
+ # If the flush parameter is +true+ <tt>content_for</tt> replaces the blocks it is given. For example:
128
135
  #
129
136
  # <% content_for :navigation do %>
130
137
  # <li><%= link_to 'Home', action: 'index' %></li>
@@ -144,7 +151,7 @@ module ActionView
144
151
  #
145
152
  # <% content_for :script, javascript_include_tag(:defaults) %>
146
153
  #
147
- # WARNING: content_for is ignored in caches. So you shouldn't use it for elements that will be fragment cached.
154
+ # WARNING: <tt>content_for</tt> is ignored in caches. So you shouldn't use it for elements that will be fragment cached.
148
155
  def content_for(name, content = nil, options = {}, &block)
149
156
  if content || block_given?
150
157
  if block_given?
@@ -171,7 +178,7 @@ module ActionView
171
178
  result unless content
172
179
  end
173
180
 
174
- # content_for? checks whether any content has been captured yet using `content_for`.
181
+ # <tt>content_for?</tt> checks whether any content has been captured yet using <tt>content_for</tt>.
175
182
  # Useful to render parts of your layout differently based on what is in your views.
176
183
  #
177
184
  # <%# This is the layout %>
@@ -1,25 +1,36 @@
1
- require 'active_support/core_ext/module/attr_internal'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/attr_internal"
2
4
 
3
5
  module ActionView
4
- module Helpers
6
+ module Helpers #:nodoc:
5
7
  # This module keeps all methods and behavior in ActionView
6
8
  # that simply delegates to the controller.
7
9
  module ControllerHelper #:nodoc:
8
10
  attr_internal :controller, :request
9
11
 
10
- delegate :request_forgery_protection_token, :params, :session, :cookies, :response, :headers,
11
- :flash, :action_name, :controller_name, :controller_path, :to => :controller
12
+ CONTROLLER_DELEGATES = [:request_forgery_protection_token, :params,
13
+ :session, :cookies, :response, :headers, :flash, :action_name,
14
+ :controller_name, :controller_path]
15
+
16
+ delegate(*CONTROLLER_DELEGATES, to: :controller)
12
17
 
13
18
  def assign_controller(controller)
14
19
  if @_controller = controller
15
20
  @_request = controller.request if controller.respond_to?(:request)
16
21
  @_config = controller.config.inheritable_copy if controller.respond_to?(:config)
22
+ @_default_form_builder = controller.default_form_builder if controller.respond_to?(:default_form_builder)
17
23
  end
18
24
  end
19
25
 
20
26
  def logger
21
27
  controller.logger if controller.respond_to?(:logger)
22
28
  end
29
+
30
+ def respond_to?(method_name, include_private = false)
31
+ return controller.respond_to?(method_name) if CONTROLLER_DELEGATES.include?(method_name.to_sym)
32
+ super
33
+ end
23
34
  end
24
35
  end
25
36
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ # = Action View CSP Helper
5
+ module Helpers #:nodoc:
6
+ module CspHelper
7
+ # Returns a meta tag "csp-nonce" with the per-session nonce value
8
+ # for allowing inline <script> tags.
9
+ #
10
+ # <head>
11
+ # <%= csp_meta_tag %>
12
+ # </head>
13
+ #
14
+ # This is used by the Rails UJS helper to create dynamically
15
+ # loaded inline <script> elements.
16
+ #
17
+ def csp_meta_tag(**options)
18
+ if content_security_policy?
19
+ options[:name] = "csp-nonce"
20
+ options[:content] = content_security_policy_nonce
21
+ tag("meta", options)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionView
2
4
  # = Action View CSRF Helper
3
- module Helpers
5
+ module Helpers #:nodoc:
4
6
  module CsrfHelper
5
7
  # Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
6
8
  # request forgery protection parameter and token, respectively.
@@ -14,14 +16,14 @@ module ActionView
14
16
  #
15
17
  # You don't need to use these tags for regular forms as they generate their own hidden fields.
16
18
  #
17
- # For AJAX requests other than GETs, extract the "csrf-token" from the meta-tag and send as the
18
- # "X-CSRF-Token" HTTP header. If you are using jQuery with jquery-rails this happens automatically.
19
+ # For AJAX requests other than GETs, extract the "csrf-token" from the meta-tag and send as the
20
+ # "X-CSRF-Token" HTTP header. If you are using rails-ujs this happens automatically.
19
21
  #
20
22
  def csrf_meta_tags
21
- if protect_against_forgery?
23
+ if defined?(protect_against_forgery?) && protect_against_forgery?
22
24
  [
23
- tag('meta', :name => 'csrf-param', :content => request_forgery_protection_token),
24
- tag('meta', :name => 'csrf-token', :content => form_authenticity_token)
25
+ tag("meta", name: "csrf-param", content: request_forgery_protection_token),
26
+ tag("meta", name: "csrf-token", content: form_authenticity_token)
25
27
  ].join("\n").html_safe
26
28
  end
27
29
  end