actionpack 4.0.1 → 4.2.11.1

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

Potentially problematic release.


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

Files changed (241) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +402 -1173
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +7 -7
  5. data/lib/abstract_controller/base.rb +39 -7
  6. data/lib/abstract_controller/callbacks.rb +32 -53
  7. data/lib/abstract_controller/collector.rb +11 -1
  8. data/lib/abstract_controller/helpers.rb +26 -16
  9. data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
  10. data/lib/abstract_controller/rendering.rb +57 -127
  11. data/lib/abstract_controller/url_for.rb +1 -1
  12. data/lib/abstract_controller.rb +1 -2
  13. data/lib/action_controller/base.rb +19 -10
  14. data/lib/action_controller/caching/fragments.rb +7 -1
  15. data/lib/action_controller/caching.rb +2 -12
  16. data/lib/action_controller/log_subscriber.rb +29 -20
  17. data/lib/action_controller/metal/conditional_get.rb +37 -12
  18. data/lib/action_controller/metal/data_streaming.rb +1 -1
  19. data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
  20. data/lib/action_controller/metal/exceptions.rb +1 -1
  21. data/lib/action_controller/metal/flash.rb +17 -0
  22. data/lib/action_controller/metal/force_ssl.rb +2 -2
  23. data/lib/action_controller/metal/head.rb +8 -6
  24. data/lib/action_controller/metal/helpers.rb +6 -2
  25. data/lib/action_controller/metal/http_authentication.rb +45 -23
  26. data/lib/action_controller/metal/instrumentation.rb +9 -6
  27. data/lib/action_controller/metal/live.rb +173 -20
  28. data/lib/action_controller/metal/mime_responds.rb +127 -232
  29. data/lib/action_controller/metal/params_wrapper.rb +16 -9
  30. data/lib/action_controller/metal/rack_delegation.rb +1 -1
  31. data/lib/action_controller/metal/redirecting.rb +34 -26
  32. data/lib/action_controller/metal/renderers.rb +39 -12
  33. data/lib/action_controller/metal/rendering.rb +41 -14
  34. data/lib/action_controller/metal/request_forgery_protection.rb +147 -19
  35. data/lib/action_controller/metal/streaming.rb +19 -21
  36. data/lib/action_controller/metal/strong_parameters.rb +166 -22
  37. data/lib/action_controller/metal/testing.rb +0 -1
  38. data/lib/action_controller/metal/url_for.rb +11 -12
  39. data/lib/action_controller/metal.rb +14 -8
  40. data/lib/action_controller/model_naming.rb +1 -1
  41. data/lib/action_controller/railtie.rb +5 -1
  42. data/lib/action_controller/test_case.rb +160 -94
  43. data/lib/action_controller.rb +2 -18
  44. data/lib/action_dispatch/http/cache.rb +5 -4
  45. data/lib/action_dispatch/http/filter_parameters.rb +2 -2
  46. data/lib/action_dispatch/http/filter_redirect.rb +5 -4
  47. data/lib/action_dispatch/http/headers.rb +46 -10
  48. data/lib/action_dispatch/http/mime_negotiation.rb +31 -4
  49. data/lib/action_dispatch/http/mime_type.rb +25 -26
  50. data/lib/action_dispatch/http/mime_types.rb +1 -0
  51. data/lib/action_dispatch/http/parameter_filter.rb +1 -1
  52. data/lib/action_dispatch/http/parameters.rb +25 -41
  53. data/lib/action_dispatch/http/request.rb +49 -32
  54. data/lib/action_dispatch/http/response.rb +127 -25
  55. data/lib/action_dispatch/http/upload.rb +9 -21
  56. data/lib/action_dispatch/http/url.rb +97 -70
  57. data/lib/action_dispatch/journey/formatter.rb +35 -19
  58. data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
  59. data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
  60. data/lib/action_dispatch/journey/gtg/transition_table.rb +23 -33
  61. data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
  62. data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
  63. data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
  64. data/lib/action_dispatch/journey/nodes/node.rb +4 -0
  65. data/lib/action_dispatch/journey/parser.rb +51 -59
  66. data/lib/action_dispatch/journey/parser.y +12 -10
  67. data/lib/action_dispatch/journey/path/pattern.rb +16 -19
  68. data/lib/action_dispatch/journey/route.rb +8 -19
  69. data/lib/action_dispatch/journey/router/strexp.rb +9 -6
  70. data/lib/action_dispatch/journey/router/utils.rb +54 -18
  71. data/lib/action_dispatch/journey/router.rb +53 -75
  72. data/lib/action_dispatch/journey/routes.rb +4 -0
  73. data/lib/action_dispatch/journey/scanner.rb +5 -5
  74. data/lib/action_dispatch/journey/visitors.rb +81 -60
  75. data/lib/action_dispatch/journey/visualizer/fsm.css +0 -4
  76. data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
  77. data/lib/action_dispatch/middleware/callbacks.rb +7 -7
  78. data/lib/action_dispatch/middleware/cookies.rb +119 -43
  79. data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -13
  80. data/lib/action_dispatch/middleware/exception_wrapper.rb +60 -20
  81. data/lib/action_dispatch/middleware/flash.rb +37 -24
  82. data/lib/action_dispatch/middleware/params_parser.rb +2 -2
  83. data/lib/action_dispatch/middleware/public_exceptions.rb +12 -3
  84. data/lib/action_dispatch/middleware/reloader.rb +11 -2
  85. data/lib/action_dispatch/middleware/remote_ip.rb +40 -54
  86. data/lib/action_dispatch/middleware/request_id.rb +1 -1
  87. data/lib/action_dispatch/middleware/session/cache_store.rb +3 -3
  88. data/lib/action_dispatch/middleware/session/cookie_store.rb +8 -7
  89. data/lib/action_dispatch/middleware/show_exceptions.rb +6 -2
  90. data/lib/action_dispatch/middleware/ssl.rb +10 -7
  91. data/lib/action_dispatch/middleware/static.rb +79 -23
  92. data/lib/action_dispatch/middleware/templates/rescues/{_request_and_response.erb → _request_and_response.html.erb} +0 -0
  93. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
  94. data/lib/action_dispatch/middleware/templates/rescues/_source.erb +21 -19
  95. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
  96. data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
  97. data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +1 -1
  98. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
  99. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
  100. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
  101. data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
  102. data/lib/action_dispatch/middleware/templates/rescues/{routing_error.erb → routing_error.html.erb} +3 -1
  103. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
  104. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
  105. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
  106. data/lib/action_dispatch/middleware/templates/rescues/{unknown_action.erb → unknown_action.html.erb} +1 -1
  107. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
  108. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +120 -64
  109. data/lib/action_dispatch/railtie.rb +5 -2
  110. data/lib/action_dispatch/request/session.rb +12 -0
  111. data/lib/action_dispatch/request/utils.rb +35 -0
  112. data/lib/action_dispatch/routing/endpoint.rb +10 -0
  113. data/lib/action_dispatch/routing/inspector.rb +11 -17
  114. data/lib/action_dispatch/routing/mapper.rb +519 -312
  115. data/lib/action_dispatch/routing/polymorphic_routes.rb +204 -79
  116. data/lib/action_dispatch/routing/redirection.rb +51 -26
  117. data/lib/action_dispatch/routing/route_set.rb +331 -206
  118. data/lib/action_dispatch/routing/routes_proxy.rb +5 -4
  119. data/lib/action_dispatch/routing/url_for.rb +19 -5
  120. data/lib/action_dispatch/routing.rb +9 -6
  121. data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
  122. data/lib/action_dispatch/testing/assertions/response.rb +9 -15
  123. data/lib/action_dispatch/testing/assertions/routing.rb +22 -22
  124. data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
  125. data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
  126. data/lib/action_dispatch/testing/assertions.rb +11 -7
  127. data/lib/action_dispatch/testing/integration.rb +31 -29
  128. data/lib/action_dispatch/testing/test_request.rb +1 -1
  129. data/lib/action_dispatch/testing/test_response.rb +1 -5
  130. data/lib/action_dispatch.rb +5 -8
  131. data/lib/action_pack/gem_version.rb +15 -0
  132. data/lib/action_pack/version.rb +4 -7
  133. data/lib/action_pack.rb +1 -1
  134. metadata +77 -159
  135. data/lib/abstract_controller/layouts.rb +0 -423
  136. data/lib/abstract_controller/view_paths.rb +0 -96
  137. data/lib/action_controller/deprecated/integration_test.rb +0 -5
  138. data/lib/action_controller/deprecated.rb +0 -7
  139. data/lib/action_controller/metal/responder.rb +0 -287
  140. data/lib/action_controller/record_identifier.rb +0 -31
  141. data/lib/action_controller/vendor/html-scanner.rb +0 -5
  142. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +0 -24
  143. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +0 -7
  144. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +0 -43
  145. data/lib/action_view/base.rb +0 -201
  146. data/lib/action_view/buffers.rb +0 -49
  147. data/lib/action_view/context.rb +0 -36
  148. data/lib/action_view/dependency_tracker.rb +0 -93
  149. data/lib/action_view/digestor.rb +0 -113
  150. data/lib/action_view/flows.rb +0 -76
  151. data/lib/action_view/helpers/active_model_helper.rb +0 -49
  152. data/lib/action_view/helpers/asset_tag_helper.rb +0 -320
  153. data/lib/action_view/helpers/asset_url_helper.rb +0 -355
  154. data/lib/action_view/helpers/atom_feed_helper.rb +0 -203
  155. data/lib/action_view/helpers/cache_helper.rb +0 -196
  156. data/lib/action_view/helpers/capture_helper.rb +0 -216
  157. data/lib/action_view/helpers/controller_helper.rb +0 -25
  158. data/lib/action_view/helpers/csrf_helper.rb +0 -30
  159. data/lib/action_view/helpers/date_helper.rb +0 -1083
  160. data/lib/action_view/helpers/debug_helper.rb +0 -39
  161. data/lib/action_view/helpers/form_helper.rb +0 -1880
  162. data/lib/action_view/helpers/form_options_helper.rb +0 -838
  163. data/lib/action_view/helpers/form_tag_helper.rb +0 -785
  164. data/lib/action_view/helpers/javascript_helper.rb +0 -117
  165. data/lib/action_view/helpers/number_helper.rb +0 -441
  166. data/lib/action_view/helpers/output_safety_helper.rb +0 -38
  167. data/lib/action_view/helpers/record_tag_helper.rb +0 -106
  168. data/lib/action_view/helpers/rendering_helper.rb +0 -90
  169. data/lib/action_view/helpers/sanitize_helper.rb +0 -256
  170. data/lib/action_view/helpers/tag_helper.rb +0 -173
  171. data/lib/action_view/helpers/tags/base.rb +0 -148
  172. data/lib/action_view/helpers/tags/check_box.rb +0 -64
  173. data/lib/action_view/helpers/tags/checkable.rb +0 -16
  174. data/lib/action_view/helpers/tags/collection_check_boxes.rb +0 -44
  175. data/lib/action_view/helpers/tags/collection_helpers.rb +0 -84
  176. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +0 -36
  177. data/lib/action_view/helpers/tags/collection_select.rb +0 -28
  178. data/lib/action_view/helpers/tags/color_field.rb +0 -25
  179. data/lib/action_view/helpers/tags/date_field.rb +0 -13
  180. data/lib/action_view/helpers/tags/date_select.rb +0 -72
  181. data/lib/action_view/helpers/tags/datetime_field.rb +0 -22
  182. data/lib/action_view/helpers/tags/datetime_local_field.rb +0 -19
  183. data/lib/action_view/helpers/tags/datetime_select.rb +0 -8
  184. data/lib/action_view/helpers/tags/email_field.rb +0 -8
  185. data/lib/action_view/helpers/tags/file_field.rb +0 -8
  186. data/lib/action_view/helpers/tags/grouped_collection_select.rb +0 -29
  187. data/lib/action_view/helpers/tags/hidden_field.rb +0 -8
  188. data/lib/action_view/helpers/tags/label.rb +0 -66
  189. data/lib/action_view/helpers/tags/month_field.rb +0 -13
  190. data/lib/action_view/helpers/tags/number_field.rb +0 -18
  191. data/lib/action_view/helpers/tags/password_field.rb +0 -12
  192. data/lib/action_view/helpers/tags/radio_button.rb +0 -31
  193. data/lib/action_view/helpers/tags/range_field.rb +0 -8
  194. data/lib/action_view/helpers/tags/search_field.rb +0 -24
  195. data/lib/action_view/helpers/tags/select.rb +0 -40
  196. data/lib/action_view/helpers/tags/tel_field.rb +0 -8
  197. data/lib/action_view/helpers/tags/text_area.rb +0 -18
  198. data/lib/action_view/helpers/tags/text_field.rb +0 -29
  199. data/lib/action_view/helpers/tags/time_field.rb +0 -13
  200. data/lib/action_view/helpers/tags/time_select.rb +0 -8
  201. data/lib/action_view/helpers/tags/time_zone_select.rb +0 -20
  202. data/lib/action_view/helpers/tags/url_field.rb +0 -8
  203. data/lib/action_view/helpers/tags/week_field.rb +0 -13
  204. data/lib/action_view/helpers/tags.rb +0 -39
  205. data/lib/action_view/helpers/text_helper.rb +0 -443
  206. data/lib/action_view/helpers/translation_helper.rb +0 -107
  207. data/lib/action_view/helpers/url_helper.rb +0 -635
  208. data/lib/action_view/helpers.rb +0 -58
  209. data/lib/action_view/locale/en.yml +0 -56
  210. data/lib/action_view/log_subscriber.rb +0 -30
  211. data/lib/action_view/lookup_context.rb +0 -241
  212. data/lib/action_view/model_naming.rb +0 -12
  213. data/lib/action_view/path_set.rb +0 -77
  214. data/lib/action_view/railtie.rb +0 -43
  215. data/lib/action_view/record_identifier.rb +0 -84
  216. data/lib/action_view/renderer/abstract_renderer.rb +0 -47
  217. data/lib/action_view/renderer/partial_renderer.rb +0 -492
  218. data/lib/action_view/renderer/renderer.rb +0 -50
  219. data/lib/action_view/renderer/streaming_template_renderer.rb +0 -103
  220. data/lib/action_view/renderer/template_renderer.rb +0 -96
  221. data/lib/action_view/routing_url_for.rb +0 -107
  222. data/lib/action_view/tasks/dependencies.rake +0 -17
  223. data/lib/action_view/template/error.rb +0 -138
  224. data/lib/action_view/template/handlers/builder.rb +0 -26
  225. data/lib/action_view/template/handlers/erb.rb +0 -146
  226. data/lib/action_view/template/handlers/raw.rb +0 -11
  227. data/lib/action_view/template/handlers.rb +0 -53
  228. data/lib/action_view/template/resolver.rb +0 -326
  229. data/lib/action_view/template/text.rb +0 -34
  230. data/lib/action_view/template/types.rb +0 -57
  231. data/lib/action_view/template.rb +0 -339
  232. data/lib/action_view/test_case.rb +0 -270
  233. data/lib/action_view/testing/resolvers.rb +0 -50
  234. data/lib/action_view/vendor/html-scanner/html/document.rb +0 -68
  235. data/lib/action_view/vendor/html-scanner/html/node.rb +0 -532
  236. data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +0 -188
  237. data/lib/action_view/vendor/html-scanner/html/selector.rb +0 -830
  238. data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +0 -107
  239. data/lib/action_view/vendor/html-scanner/html/version.rb +0 -11
  240. data/lib/action_view/vendor/html-scanner.rb +0 -20
  241. data/lib/action_view.rb +0 -93
@@ -1,635 +0,0 @@
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'
5
-
6
- module ActionView
7
- # = Action View URL Helpers
8
- module Helpers #:nodoc:
9
- # Provides a set of methods for making links and getting URLs that
10
- # depend on the routing subsystem (see ActionDispatch::Routing).
11
- # This allows you to use the same format for links in views
12
- # and controllers.
13
- module UrlHelper
14
- # This helper may be included in any class that includes the
15
- # URL helpers of a routes (routes.url_helpers). Some methods
16
- # provided here will only work in the context of a request
17
- # (link_to_unless_current, for instance), which must be provided
18
- # as a method called #request on the context.
19
-
20
- extend ActiveSupport::Concern
21
-
22
- include TagHelper
23
-
24
- module ClassMethods
25
- def _url_for_modules
26
- ActionView::RoutingUrlFor
27
- end
28
- end
29
-
30
- # Basic implementation of url_for to allow use helpers without routes existence
31
- def url_for(options = nil) # :nodoc:
32
- case options
33
- when String
34
- options
35
- when :back
36
- _back_url
37
- else
38
- raise ArgumentError, "arguments passed to url_for can't be handled. Please require " +
39
- "routes or provide your own implementation"
40
- end
41
- end
42
-
43
- def _back_url # :nodoc:
44
- referrer = controller.respond_to?(:request) && controller.request.env["HTTP_REFERER"]
45
- referrer || 'javascript:history.back()'
46
- end
47
- protected :_back_url
48
-
49
- # Creates a link tag of the given +name+ using a URL created by the set of +options+.
50
- # 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
53
- # of an options hash will generate a link to the referrer (a JavaScript back link
54
- # will be used in place of a referrer if none exists). If +nil+ is passed as the name
55
- # the value of the link itself will become the name.
56
- #
57
- # ==== Signatures
58
- #
59
- # link_to(body, url, html_options = {})
60
- # # url is a String; you can use URL helpers like
61
- # # posts_path
62
- #
63
- # link_to(body, url_options = {}, html_options = {})
64
- # # url_options, except :method, is passed to url_for
65
- #
66
- # link_to(options = {}, html_options = {}) do
67
- # # name
68
- # end
69
- #
70
- # link_to(url, html_options = {}) do
71
- # # name
72
- # end
73
- #
74
- # ==== Options
75
- # * <tt>:data</tt> - This option can be used to add custom data attributes.
76
- # * <tt>method: symbol of HTTP verb</tt> - This modifier will dynamically
77
- # create an HTML form and immediately submit the form for processing using
78
- # the HTTP verb specified. Useful for having links perform a POST operation
79
- # in dangerous actions like deleting a record (which search bots can follow
80
- # while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>.
81
- # Note that if the user has JavaScript disabled, the request will fall back
82
- # to using GET. If <tt>href: '#'</tt> is used and the user has JavaScript
83
- # disabled clicking the link will have no effect. If you are relying on the
84
- # POST behavior, you should check for it in your controller's action by using
85
- # the request object's methods for <tt>post?</tt>, <tt>delete?</tt>, <tt>:patch</tt>, or <tt>put?</tt>.
86
- # * <tt>remote: true</tt> - This will allow the unobtrusive JavaScript
87
- # driver to make an Ajax request to the URL in question instead of following
88
- # the link. The drivers each provide mechanisms for listening for the
89
- # completion of the Ajax request and performing JavaScript operations once
90
- # they're complete
91
- #
92
- # ==== Data attributes
93
- #
94
- # * <tt>confirm: 'question?'</tt> - This will allow the unobtrusive JavaScript
95
- # driver to prompt with the question specified. If the user accepts, the link is
96
- # processed normally, otherwise no action is taken.
97
- # * <tt>:disable_with</tt> - Value of this parameter will be
98
- # used as the value for a disabled version of the submit
99
- # button when the form is submitted. This feature is provided
100
- # by the unobtrusive JavaScript driver.
101
- #
102
- # ==== Examples
103
- # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
104
- # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
105
- # your application on resources and use
106
- #
107
- # link_to "Profile", profile_path(@profile)
108
- # # => <a href="/profiles/1">Profile</a>
109
- #
110
- # or the even pithier
111
- #
112
- # link_to "Profile", @profile
113
- # # => <a href="/profiles/1">Profile</a>
114
- #
115
- # in place of the older more verbose, non-resource-oriented
116
- #
117
- # link_to "Profile", controller: "profiles", action: "show", id: @profile
118
- # # => <a href="/profiles/show/1">Profile</a>
119
- #
120
- # Similarly,
121
- #
122
- # link_to "Profiles", profiles_path
123
- # # => <a href="/profiles">Profiles</a>
124
- #
125
- # is better than
126
- #
127
- # link_to "Profiles", controller: "profiles"
128
- # # => <a href="/profiles">Profiles</a>
129
- #
130
- # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
131
- #
132
- # <%= link_to(@profile) do %>
133
- # <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
134
- # <% end %>
135
- # # => <a href="/profiles/1">
136
- # <strong>David</strong> -- <span>Check it out!</span>
137
- # </a>
138
- #
139
- # Classes and ids for CSS are easy to produce:
140
- #
141
- # link_to "Articles", articles_path, id: "news", class: "article"
142
- # # => <a href="/articles" class="article" id="news">Articles</a>
143
- #
144
- # Be careful when using the older argument style, as an extra literal hash is needed:
145
- #
146
- # link_to "Articles", { controller: "articles" }, id: "news", class: "article"
147
- # # => <a href="/articles" class="article" id="news">Articles</a>
148
- #
149
- # Leaving the hash off gives the wrong link:
150
- #
151
- # link_to "WRONG!", controller: "articles", id: "news", class: "article"
152
- # # => <a href="/articles/index/news?class=article">WRONG!</a>
153
- #
154
- # +link_to+ can also produce links with anchors or query strings:
155
- #
156
- # link_to "Comment wall", profile_path(@profile, anchor: "wall")
157
- # # => <a href="/profiles/1#wall">Comment wall</a>
158
- #
159
- # link_to "Ruby on Rails search", controller: "searches", query: "ruby on rails"
160
- # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
161
- #
162
- # link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
163
- # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
164
- #
165
- # The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
166
- #
167
- # link_to("Destroy", "http://www.example.com", method: :delete)
168
- # # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
169
- #
170
- # You can also use custom data attributes using the <tt>:data</tt> option:
171
- #
172
- # link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: "Are you sure?" }
173
- # # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?">Visit Other Site</a>
174
- def link_to(name = nil, options = nil, html_options = nil, &block)
175
- html_options, options, name = options, name, block if block_given?
176
- options ||= {}
177
-
178
- html_options = convert_options_to_data_attributes(options, html_options)
179
-
180
- url = url_for(options)
181
- html_options['href'] ||= url
182
-
183
- content_tag(:a, name || url, html_options, &block)
184
- end
185
-
186
- # Generates a form containing a single button that submits to the URL created
187
- # by the set of +options+. This is the safest method to ensure links that
188
- # cause changes to your data are not triggered by search bots or accelerators.
189
- # If the HTML button does not work with your layout, you can also consider
190
- # using the +link_to+ method with the <tt>:method</tt> modifier as described in
191
- # the +link_to+ documentation.
192
- #
193
- # By default, the generated form element has a class name of <tt>button_to</tt>
194
- # to allow styling of the form itself and its children. This can be changed
195
- # using the <tt>:form_class</tt> modifier within +html_options+. You can control
196
- # the form submission and input element behavior using +html_options+.
197
- # This method accepts the <tt>:method</tt> modifier described in the +link_to+ documentation.
198
- # If no <tt>:method</tt> modifier is given, it will default to performing a POST operation.
199
- # You can also disable the button by passing <tt>disabled: true</tt> in +html_options+.
200
- # If you are using RESTful routes, you can pass the <tt>:method</tt>
201
- # to change the HTTP verb used to submit the form.
202
- #
203
- # ==== Options
204
- # The +options+ hash accepts the same options as +url_for+.
205
- #
206
- # There are a few special +html_options+:
207
- # * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
208
- # <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
209
- # * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
210
- # * <tt>:data</tt> - This option can be used to add custom data attributes.
211
- # * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
212
- # submit behavior. By default this behavior is an ajax submit.
213
- # * <tt>:form</tt> - This hash will be form attributes
214
- # * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
215
- # be placed
216
- #
217
- # ==== Data attributes
218
- #
219
- # * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
220
- # prompt with the question specified. If the user accepts, the link is
221
- # processed normally, otherwise no action is taken.
222
- # * <tt>:disable_with</tt> - Value of this parameter will be
223
- # used as the value for a disabled version of the submit
224
- # button when the form is submitted. This feature is provided
225
- # by the unobtrusive JavaScript driver.
226
- #
227
- # ==== Examples
228
- # <%= button_to "New", action: "new" %>
229
- # # => "<form method="post" action="/controller/new" class="button_to">
230
- # # <div><input value="New" type="submit" /></div>
231
- # # </form>"
232
- #
233
- # <%= button_to [:make_happy, @user] do %>
234
- # Make happy <strong><%= @user.name %></strong>
235
- # <% end %>
236
- # # => "<form method="post" action="/users/1/make_happy" class="button_to">
237
- # # <div>
238
- # # <button type="submit">
239
- # # Make happy <strong><%= @user.name %></strong>
240
- # # </button>
241
- # # </div>
242
- # # </form>"
243
- #
244
- # <%= button_to "New", { action: "new" }, form_class: "new-thing" %>
245
- # # => "<form method="post" action="/controller/new" class="new-thing">
246
- # # <div><input value="New" type="submit" /></div>
247
- # # </form>"
248
- #
249
- #
250
- # <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %>
251
- # # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
252
- # # <div>
253
- # # <input value="Create" type="submit" />
254
- # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
255
- # # </div>
256
- # # </form>"
257
- #
258
- #
259
- # <%= button_to "Delete Image", { action: "delete", id: @image.id },
260
- # method: :delete, data: { confirm: "Are you sure?" } %>
261
- # # => "<form method="post" action="/images/delete/1" class="button_to">
262
- # # <div>
263
- # # <input type="hidden" name="_method" value="delete" />
264
- # # <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
265
- # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
266
- # # </div>
267
- # # </form>"
268
- #
269
- #
270
- # <%= button_to('Destroy', 'http://www.example.com',
271
- # method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
272
- # # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
273
- # # <div>
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
- # # </div>
278
- # # </form>"
279
- # #
280
- def button_to(name = nil, options = nil, html_options = nil, &block)
281
- html_options, options = options, name if block_given?
282
- options ||= {}
283
- html_options ||= {}
284
-
285
- html_options = html_options.stringify_keys
286
- convert_boolean_attributes!(html_options, %w(disabled))
287
-
288
- url = options.is_a?(String) ? options : url_for(options)
289
- remote = html_options.delete('remote')
290
-
291
- method = html_options.delete('method').to_s
292
- method_tag = %w{patch put delete}.include?(method) ? method_tag(method) : ''.html_safe
293
-
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
299
-
300
- request_token_tag = form_method == 'post' ? token_tag : ''
301
-
302
- html_options = convert_options_to_data_attributes(options, html_options)
303
- html_options['type'] = 'submit'
304
-
305
- button = if block_given?
306
- content_tag('button', html_options, &block)
307
- else
308
- html_options['value'] = name || url
309
- tag('input', html_options)
310
- end
311
-
312
- inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
313
- content_tag('form', content_tag('div', inner_tags), form_options)
314
- end
315
-
316
- # Creates a link tag of the given +name+ using a URL created by the set of
317
- # +options+ unless the current request URI is the same as the links, in
318
- # which case only the name is returned (or the given block is yielded, if
319
- # one exists). You can give +link_to_unless_current+ a block which will
320
- # specialize the default behavior (e.g., show a "Start Here" link rather
321
- # than the link's text).
322
- #
323
- # ==== Examples
324
- # Let's say you have a navigation menu...
325
- #
326
- # <ul id="navbar">
327
- # <li><%= link_to_unless_current("Home", { action: "index" }) %></li>
328
- # <li><%= link_to_unless_current("About Us", { action: "about" }) %></li>
329
- # </ul>
330
- #
331
- # If in the "about" action, it will render...
332
- #
333
- # <ul id="navbar">
334
- # <li><a href="/controller/index">Home</a></li>
335
- # <li>About Us</li>
336
- # </ul>
337
- #
338
- # ...but if in the "index" action, it will render:
339
- #
340
- # <ul id="navbar">
341
- # <li>Home</li>
342
- # <li><a href="/controller/about">About Us</a></li>
343
- # </ul>
344
- #
345
- # The implicit block given to +link_to_unless_current+ is evaluated if the current
346
- # action is the action given. So, if we had a comments page and wanted to render a
347
- # "Go Back" link instead of a link to the comments page, we could do something like this...
348
- #
349
- # <%=
350
- # link_to_unless_current("Comment", { controller: "comments", action: "new" }) do
351
- # link_to("Go back", { controller: "posts", action: "index" })
352
- # end
353
- # %>
354
- def link_to_unless_current(name, options = {}, html_options = {}, &block)
355
- link_to_unless current_page?(options), name, options, html_options, &block
356
- end
357
-
358
- # Creates a link tag of the given +name+ using a URL created by the set of
359
- # +options+ unless +condition+ is true, in which case only the name is
360
- # returned. To specialize the default behavior (i.e., show a login link rather
361
- # than just the plaintext link text), you can pass a block that
362
- # accepts the name or the full argument list for +link_to_unless+.
363
- #
364
- # ==== Examples
365
- # <%= link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) %>
366
- # # If the user is logged in...
367
- # # => <a href="/controller/reply/">Reply</a>
368
- #
369
- # <%=
370
- # link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) do |name|
371
- # link_to(name, { controller: "accounts", action: "signup" })
372
- # end
373
- # %>
374
- # # If the user is logged in...
375
- # # => <a href="/controller/reply/">Reply</a>
376
- # # If not...
377
- # # => <a href="/accounts/signup">Reply</a>
378
- def link_to_unless(condition, name, options = {}, html_options = {}, &block)
379
- if condition
380
- if block_given?
381
- block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
382
- else
383
- ERB::Util.html_escape(name)
384
- end
385
- else
386
- link_to(name, options, html_options)
387
- end
388
- end
389
-
390
- # Creates a link tag of the given +name+ using a URL created by the set of
391
- # +options+ if +condition+ is true, otherwise only the name is
392
- # returned. To specialize the default behavior, you can pass a block that
393
- # accepts the name or the full argument list for +link_to_unless+ (see the examples
394
- # in +link_to_unless+).
395
- #
396
- # ==== Examples
397
- # <%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
398
- # # If the user isn't logged in...
399
- # # => <a href="/sessions/new/">Login</a>
400
- #
401
- # <%=
402
- # link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) do
403
- # link_to(@current_user.login, { controller: "accounts", action: "show", id: @current_user })
404
- # end
405
- # %>
406
- # # If the user isn't logged in...
407
- # # => <a href="/sessions/new/">Login</a>
408
- # # If they are logged in...
409
- # # => <a href="/accounts/show/3">my_username</a>
410
- def link_to_if(condition, name, options = {}, html_options = {}, &block)
411
- link_to_unless !condition, name, options, html_options, &block
412
- end
413
-
414
- # Creates a mailto link tag to the specified +email_address+, which is
415
- # also used as the name of the link unless +name+ is specified. Additional
416
- # HTML attributes for the link can be passed in +html_options+.
417
- #
418
- # +mail_to+ has several methods for customizing the email itself by
419
- # passing special keys to +html_options+.
420
- #
421
- # ==== Options
422
- # * <tt>:subject</tt> - Preset the subject line of the email.
423
- # * <tt>:body</tt> - Preset the body of the email.
424
- # * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
425
- # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
426
- #
427
- # ==== Obfuscation
428
- # Prior to Rails 4.0, +mail_to+ provided options for encoding the address
429
- # in order to hinder email harvesters. To take advantage of these options,
430
- # install the +actionview-encoded_mail_to+ gem.
431
- #
432
- # ==== Examples
433
- # mail_to "me@domain.com"
434
- # # => <a href="mailto:me@domain.com">me@domain.com</a>
435
- #
436
- # mail_to "me@domain.com", "My email"
437
- # # => <a href="mailto:me@domain.com">My email</a>
438
- #
439
- # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
440
- # subject: "This is an example email"
441
- # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
442
- #
443
- # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
444
- #
445
- # <%= mail_to "me@domain.com" do %>
446
- # <strong>Email me:</strong> <span>me@domain.com</span>
447
- # <% end %>
448
- # # => <a href="mailto:me@domain.com">
449
- # <strong>Email me:</strong> <span>me@domain.com</span>
450
- # </a>
451
- def mail_to(email_address, name = nil, html_options = {}, &block)
452
- email_address = ERB::Util.html_escape(email_address)
453
-
454
- html_options, name = name, nil if block_given?
455
- html_options = (html_options || {}).stringify_keys
456
-
457
- extras = %w{ cc bcc body subject }.map { |item|
458
- option = html_options.delete(item) || next
459
- "#{item}=#{Rack::Utils.escape_path(option)}"
460
- }.compact
461
- extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))
462
-
463
- html_options["href"] = "mailto:#{email_address}#{extras}".html_safe
464
-
465
- content_tag(:a, name || email_address.html_safe, html_options, &block)
466
- end
467
-
468
- # True if the current request URI was generated by the given +options+.
469
- #
470
- # ==== Examples
471
- # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc</tt> action.
472
- #
473
- # current_page?(action: 'process')
474
- # # => false
475
- #
476
- # current_page?(controller: 'shop', action: 'checkout')
477
- # # => true
478
- #
479
- # current_page?(controller: 'shop', action: 'checkout', order: 'asc')
480
- # # => false
481
- #
482
- # current_page?(action: 'checkout')
483
- # # => true
484
- #
485
- # current_page?(controller: 'library', action: 'checkout')
486
- # # => false
487
- #
488
- # current_page?('http://www.example.com/shop/checkout')
489
- # # => true
490
- #
491
- # current_page?('/shop/checkout')
492
- # # => true
493
- #
494
- # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc&page=1</tt> action.
495
- #
496
- # current_page?(action: 'process')
497
- # # => false
498
- #
499
- # current_page?(controller: 'shop', action: 'checkout')
500
- # # => true
501
- #
502
- # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '1')
503
- # # => true
504
- #
505
- # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '2')
506
- # # => false
507
- #
508
- # current_page?(controller: 'shop', action: 'checkout', order: 'desc')
509
- # # => false
510
- #
511
- # current_page?(action: 'checkout')
512
- # # => true
513
- #
514
- # current_page?(controller: 'library', action: 'checkout')
515
- # # => false
516
- #
517
- # Let's say we're in the <tt>http://www.example.com/products</tt> action with method POST in case of invalid product.
518
- #
519
- # current_page?(controller: 'product', action: 'index')
520
- # # => false
521
- #
522
- def current_page?(options)
523
- unless request
524
- raise "You cannot use helpers that need to determine the current " \
525
- "page unless your view context provides a Request object " \
526
- "in a #request method"
527
- end
528
-
529
- return false unless request.get? || request.head?
530
-
531
- url_string = URI.parser.unescape(url_for(options)).force_encoding(Encoding::BINARY)
532
-
533
- # We ignore any extra parameters in the request_uri if the
534
- # submitted url doesn't have any either. This lets the function
535
- # work with things like ?order=asc
536
- request_uri = url_string.index("?") ? request.fullpath : request.path
537
- request_uri = URI.parser.unescape(request_uri).force_encoding(Encoding::BINARY)
538
-
539
- if url_string =~ /^\w+:\/\//
540
- url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
541
- else
542
- url_string == request_uri
543
- end
544
- end
545
-
546
- private
547
- def convert_options_to_data_attributes(options, html_options)
548
- if html_options
549
- html_options = html_options.stringify_keys
550
- html_options['data-remote'] = 'true' if link_to_remote_options?(options) || link_to_remote_options?(html_options)
551
-
552
- disable_with = html_options.delete("disable_with")
553
- confirm = html_options.delete('confirm')
554
- method = html_options.delete('method')
555
-
556
- if confirm
557
- message = ":confirm option is deprecated and will be removed from Rails 4.1. " \
558
- "Use 'data: { confirm: \'Text\' }' instead."
559
- ActiveSupport::Deprecation.warn message
560
-
561
- html_options["data-confirm"] = confirm
562
- end
563
-
564
- add_method_to_attributes!(html_options, method) if method
565
-
566
- if disable_with
567
- message = ":disable_with option is deprecated and will be removed from Rails 4.1. " \
568
- "Use 'data: { disable_with: \'Text\' }' instead."
569
- ActiveSupport::Deprecation.warn message
570
-
571
- html_options["data-disable-with"] = disable_with
572
- end
573
-
574
- html_options
575
- else
576
- link_to_remote_options?(options) ? {'data-remote' => 'true'} : {}
577
- end
578
- end
579
-
580
- def link_to_remote_options?(options)
581
- if options.is_a?(Hash)
582
- options.delete('remote') || options.delete(:remote)
583
- end
584
- end
585
-
586
- def add_method_to_attributes!(html_options, method)
587
- if method && method.to_s.downcase != "get" && html_options["rel"] !~ /nofollow/
588
- html_options["rel"] = "#{html_options["rel"]} nofollow".lstrip
589
- end
590
- html_options["data-method"] = method
591
- end
592
-
593
- # Processes the +html_options+ hash, converting the boolean
594
- # attributes from true/false form into the form required by
595
- # HTML/XHTML. (An attribute is considered to be boolean if
596
- # its name is listed in the given +bool_attrs+ array.)
597
- #
598
- # More specifically, for each boolean attribute in +html_options+
599
- # given as:
600
- #
601
- # "attr" => bool_value
602
- #
603
- # if the associated +bool_value+ evaluates to true, it is
604
- # replaced with the attribute's name; otherwise the attribute is
605
- # removed from the +html_options+ hash. (See the XHTML 1.0 spec,
606
- # section 4.5 "Attribute Minimization" for more:
607
- # http://www.w3.org/TR/xhtml1/#h-4.5)
608
- #
609
- # Returns the updated +html_options+ hash, which is also modified
610
- # in place.
611
- #
612
- # Example:
613
- #
614
- # convert_boolean_attributes!( html_options,
615
- # %w( checked disabled readonly ) )
616
- def convert_boolean_attributes!(html_options, bool_attrs)
617
- bool_attrs.each { |x| html_options[x] = x if html_options.delete(x) }
618
- html_options
619
- end
620
-
621
- def token_tag(token=nil)
622
- if token != false && protect_against_forgery?
623
- token ||= form_authenticity_token
624
- tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token)
625
- else
626
- ''
627
- end
628
- end
629
-
630
- def method_tag(method)
631
- tag('input', type: 'hidden', name: '_method', value: method.to_s)
632
- end
633
- end
634
- end
635
- end