actionview 4.1.0.beta1

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 (106) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +274 -0
  3. data/MIT-LICENSE +21 -0
  4. data/README.rdoc +34 -0
  5. data/lib/action_view.rb +97 -0
  6. data/lib/action_view/base.rb +205 -0
  7. data/lib/action_view/buffers.rb +49 -0
  8. data/lib/action_view/context.rb +36 -0
  9. data/lib/action_view/dependency_tracker.rb +93 -0
  10. data/lib/action_view/digestor.rb +116 -0
  11. data/lib/action_view/flows.rb +76 -0
  12. data/lib/action_view/helpers.rb +64 -0
  13. data/lib/action_view/helpers/active_model_helper.rb +49 -0
  14. data/lib/action_view/helpers/asset_tag_helper.rb +322 -0
  15. data/lib/action_view/helpers/asset_url_helper.rb +355 -0
  16. data/lib/action_view/helpers/atom_feed_helper.rb +203 -0
  17. data/lib/action_view/helpers/cache_helper.rb +200 -0
  18. data/lib/action_view/helpers/capture_helper.rb +216 -0
  19. data/lib/action_view/helpers/controller_helper.rb +25 -0
  20. data/lib/action_view/helpers/csrf_helper.rb +30 -0
  21. data/lib/action_view/helpers/date_helper.rb +1075 -0
  22. data/lib/action_view/helpers/debug_helper.rb +39 -0
  23. data/lib/action_view/helpers/form_helper.rb +1876 -0
  24. data/lib/action_view/helpers/form_options_helper.rb +843 -0
  25. data/lib/action_view/helpers/form_tag_helper.rb +746 -0
  26. data/lib/action_view/helpers/javascript_helper.rb +75 -0
  27. data/lib/action_view/helpers/number_helper.rb +425 -0
  28. data/lib/action_view/helpers/output_safety_helper.rb +38 -0
  29. data/lib/action_view/helpers/record_tag_helper.rb +108 -0
  30. data/lib/action_view/helpers/rendering_helper.rb +90 -0
  31. data/lib/action_view/helpers/sanitize_helper.rb +256 -0
  32. data/lib/action_view/helpers/tag_helper.rb +176 -0
  33. data/lib/action_view/helpers/tags.rb +41 -0
  34. data/lib/action_view/helpers/tags/base.rb +148 -0
  35. data/lib/action_view/helpers/tags/check_box.rb +64 -0
  36. data/lib/action_view/helpers/tags/checkable.rb +16 -0
  37. data/lib/action_view/helpers/tags/collection_check_boxes.rb +44 -0
  38. data/lib/action_view/helpers/tags/collection_helpers.rb +85 -0
  39. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +36 -0
  40. data/lib/action_view/helpers/tags/collection_select.rb +28 -0
  41. data/lib/action_view/helpers/tags/color_field.rb +25 -0
  42. data/lib/action_view/helpers/tags/date_field.rb +13 -0
  43. data/lib/action_view/helpers/tags/date_select.rb +72 -0
  44. data/lib/action_view/helpers/tags/datetime_field.rb +22 -0
  45. data/lib/action_view/helpers/tags/datetime_local_field.rb +19 -0
  46. data/lib/action_view/helpers/tags/datetime_select.rb +8 -0
  47. data/lib/action_view/helpers/tags/email_field.rb +8 -0
  48. data/lib/action_view/helpers/tags/file_field.rb +8 -0
  49. data/lib/action_view/helpers/tags/grouped_collection_select.rb +29 -0
  50. data/lib/action_view/helpers/tags/hidden_field.rb +8 -0
  51. data/lib/action_view/helpers/tags/label.rb +65 -0
  52. data/lib/action_view/helpers/tags/month_field.rb +13 -0
  53. data/lib/action_view/helpers/tags/number_field.rb +18 -0
  54. data/lib/action_view/helpers/tags/password_field.rb +12 -0
  55. data/lib/action_view/helpers/tags/radio_button.rb +31 -0
  56. data/lib/action_view/helpers/tags/range_field.rb +8 -0
  57. data/lib/action_view/helpers/tags/search_field.rb +24 -0
  58. data/lib/action_view/helpers/tags/select.rb +41 -0
  59. data/lib/action_view/helpers/tags/tel_field.rb +8 -0
  60. data/lib/action_view/helpers/tags/text_area.rb +18 -0
  61. data/lib/action_view/helpers/tags/text_field.rb +29 -0
  62. data/lib/action_view/helpers/tags/time_field.rb +13 -0
  63. data/lib/action_view/helpers/tags/time_select.rb +8 -0
  64. data/lib/action_view/helpers/tags/time_zone_select.rb +20 -0
  65. data/lib/action_view/helpers/tags/url_field.rb +8 -0
  66. data/lib/action_view/helpers/tags/week_field.rb +13 -0
  67. data/lib/action_view/helpers/text_helper.rb +447 -0
  68. data/lib/action_view/helpers/translation_helper.rb +111 -0
  69. data/lib/action_view/helpers/url_helper.rb +625 -0
  70. data/lib/action_view/layouts.rb +426 -0
  71. data/lib/action_view/locale/en.yml +56 -0
  72. data/lib/action_view/log_subscriber.rb +44 -0
  73. data/lib/action_view/lookup_context.rb +249 -0
  74. data/lib/action_view/model_naming.rb +12 -0
  75. data/lib/action_view/path_set.rb +77 -0
  76. data/lib/action_view/railtie.rb +49 -0
  77. data/lib/action_view/record_identifier.rb +84 -0
  78. data/lib/action_view/renderer/abstract_renderer.rb +47 -0
  79. data/lib/action_view/renderer/partial_renderer.rb +492 -0
  80. data/lib/action_view/renderer/renderer.rb +50 -0
  81. data/lib/action_view/renderer/streaming_template_renderer.rb +103 -0
  82. data/lib/action_view/renderer/template_renderer.rb +96 -0
  83. data/lib/action_view/rendering.rb +145 -0
  84. data/lib/action_view/routing_url_for.rb +109 -0
  85. data/lib/action_view/tasks/dependencies.rake +17 -0
  86. data/lib/action_view/template.rb +340 -0
  87. data/lib/action_view/template/error.rb +141 -0
  88. data/lib/action_view/template/handlers.rb +53 -0
  89. data/lib/action_view/template/handlers/builder.rb +26 -0
  90. data/lib/action_view/template/handlers/erb.rb +145 -0
  91. data/lib/action_view/template/handlers/raw.rb +11 -0
  92. data/lib/action_view/template/resolver.rb +329 -0
  93. data/lib/action_view/template/text.rb +34 -0
  94. data/lib/action_view/template/types.rb +57 -0
  95. data/lib/action_view/test_case.rb +272 -0
  96. data/lib/action_view/testing/resolvers.rb +50 -0
  97. data/lib/action_view/vendor/html-scanner.rb +20 -0
  98. data/lib/action_view/vendor/html-scanner/html/document.rb +68 -0
  99. data/lib/action_view/vendor/html-scanner/html/node.rb +532 -0
  100. data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +188 -0
  101. data/lib/action_view/vendor/html-scanner/html/selector.rb +830 -0
  102. data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +107 -0
  103. data/lib/action_view/vendor/html-scanner/html/version.rb +11 -0
  104. data/lib/action_view/version.rb +11 -0
  105. data/lib/action_view/view_paths.rb +96 -0
  106. metadata +218 -0
@@ -0,0 +1,111 @@
1
+ require 'action_view/helpers/tag_helper'
2
+ require 'i18n/exceptions'
3
+
4
+ module ActionView
5
+ # = Action View Translation Helpers
6
+ module Helpers
7
+ module TranslationHelper
8
+ # Delegates to <tt>I18n#translate</tt> but also performs three additional functions.
9
+ #
10
+ # First, it will ensure that any thrown +MissingTranslation+ messages will be turned
11
+ # into inline spans that:
12
+ #
13
+ # * have a "translation-missing" class set,
14
+ # * contain the missing key as a title attribute and
15
+ # * a titleized version of the last key segment as a text.
16
+ #
17
+ # E.g. the value returned for a missing translation key :"blog.post.title" will be
18
+ # <span class="translation_missing" title="translation missing: en.blog.post.title">Title</span>.
19
+ # This way your views will display rather reasonable strings but it will still
20
+ # be easy to spot missing translations.
21
+ #
22
+ # Second, it'll scope the key by the current partial if the key starts
23
+ # with a period. So if you call <tt>translate(".foo")</tt> from the
24
+ # <tt>people/index.html.erb</tt> template, you'll actually be calling
25
+ # <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
26
+ # to translate many keys within the same partials and gives you a simple framework
27
+ # for scoping them consistently. If you don't prepend the key with a period,
28
+ # nothing is converted.
29
+ #
30
+ # Third, it'll mark the translation as safe HTML if the key has the suffix
31
+ # "_html" or the last element of the key is the word "html". For example,
32
+ # calling translate("footer_html") or translate("footer.html") will return
33
+ # a safe HTML string that won't be escaped by other HTML helper methods. This
34
+ # naming convention helps to identify translations that include HTML tags so that
35
+ # you know what kind of output to expect when you call translate in a template.
36
+ def translate(key, options = {})
37
+ options[:default] = wrap_translate_defaults(options[:default]) if options[:default]
38
+
39
+ # If the user has specified rescue_format then pass it all through, otherwise use
40
+ # raise and do the work ourselves
41
+ if options.key?(:raise) || options.key?(:rescue_format)
42
+ raise_error = options[:raise] || options[:rescue_format]
43
+ else
44
+ raise_error = false
45
+ options[:raise] = true
46
+ end
47
+
48
+ if html_safe_translation_key?(key)
49
+ html_safe_options = options.dup
50
+ options.except(*I18n::RESERVED_KEYS).each do |name, value|
51
+ unless name == :count && value.is_a?(Numeric)
52
+ html_safe_options[name] = ERB::Util.html_escape(value.to_s)
53
+ end
54
+ end
55
+ translation = I18n.translate(scope_key_by_partial(key), html_safe_options)
56
+
57
+ translation.respond_to?(:html_safe) ? translation.html_safe : translation
58
+ else
59
+ I18n.translate(scope_key_by_partial(key), options)
60
+ end
61
+ rescue I18n::MissingTranslationData => e
62
+ raise e if raise_error
63
+
64
+ keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
65
+ content_tag('span', keys.last.to_s.titleize, :class => 'translation_missing', :title => "translation missing: #{keys.join('.')}")
66
+ end
67
+ alias :t :translate
68
+
69
+ # Delegates to <tt>I18n.localize</tt> with no additional functionality.
70
+ #
71
+ # See http://rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/Base:localize
72
+ # for more information.
73
+ def localize(*args)
74
+ I18n.localize(*args)
75
+ end
76
+ alias :l :localize
77
+
78
+ private
79
+ def scope_key_by_partial(key)
80
+ if key.to_s.first == "."
81
+ if @virtual_path
82
+ @virtual_path.gsub(%r{/_?}, ".") + key.to_s
83
+ else
84
+ raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
85
+ end
86
+ else
87
+ key
88
+ end
89
+ end
90
+
91
+ def html_safe_translation_key?(key)
92
+ key.to_s =~ /(\b|_|\.)html$/
93
+ end
94
+
95
+ def wrap_translate_defaults(defaults)
96
+ new_defaults = []
97
+ defaults = Array(defaults)
98
+ while key = defaults.shift
99
+ if key.is_a?(Symbol)
100
+ new_defaults << lambda { |_, options| translate key, options.merge(:default => defaults) }
101
+ break
102
+ else
103
+ new_defaults << key
104
+ end
105
+ end
106
+
107
+ new_defaults
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,625 @@
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
+ BUTTON_TAG_METHOD_VERBS = %w{patch put delete}
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 (in this case, the
96
+ # resulting text would be <tt>question?</tt>. If the user accepts, the
97
+ # link is processed normally, otherwise no action is taken.
98
+ # * <tt>:disable_with</tt> - Value of this parameter will be
99
+ # used as the value for a disabled version of the submit
100
+ # button when the form is submitted. This feature is provided
101
+ # by the unobtrusive JavaScript driver.
102
+ #
103
+ # ==== Examples
104
+ # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
105
+ # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
106
+ # your application on resources and use
107
+ #
108
+ # link_to "Profile", profile_path(@profile)
109
+ # # => <a href="/profiles/1">Profile</a>
110
+ #
111
+ # or the even pithier
112
+ #
113
+ # link_to "Profile", @profile
114
+ # # => <a href="/profiles/1">Profile</a>
115
+ #
116
+ # in place of the older more verbose, non-resource-oriented
117
+ #
118
+ # link_to "Profile", controller: "profiles", action: "show", id: @profile
119
+ # # => <a href="/profiles/show/1">Profile</a>
120
+ #
121
+ # Similarly,
122
+ #
123
+ # link_to "Profiles", profiles_path
124
+ # # => <a href="/profiles">Profiles</a>
125
+ #
126
+ # is better than
127
+ #
128
+ # link_to "Profiles", controller: "profiles"
129
+ # # => <a href="/profiles">Profiles</a>
130
+ #
131
+ # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
132
+ #
133
+ # <%= link_to(@profile) do %>
134
+ # <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
135
+ # <% end %>
136
+ # # => <a href="/profiles/1">
137
+ # <strong>David</strong> -- <span>Check it out!</span>
138
+ # </a>
139
+ #
140
+ # Classes and ids for CSS are easy to produce:
141
+ #
142
+ # link_to "Articles", articles_path, id: "news", class: "article"
143
+ # # => <a href="/articles" class="article" id="news">Articles</a>
144
+ #
145
+ # Be careful when using the older argument style, as an extra literal hash is needed:
146
+ #
147
+ # link_to "Articles", { controller: "articles" }, id: "news", class: "article"
148
+ # # => <a href="/articles" class="article" id="news">Articles</a>
149
+ #
150
+ # Leaving the hash off gives the wrong link:
151
+ #
152
+ # link_to "WRONG!", controller: "articles", id: "news", class: "article"
153
+ # # => <a href="/articles/index/news?class=article">WRONG!</a>
154
+ #
155
+ # +link_to+ can also produce links with anchors or query strings:
156
+ #
157
+ # link_to "Comment wall", profile_path(@profile, anchor: "wall")
158
+ # # => <a href="/profiles/1#wall">Comment wall</a>
159
+ #
160
+ # link_to "Ruby on Rails search", controller: "searches", query: "ruby on rails"
161
+ # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
162
+ #
163
+ # link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
164
+ # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
165
+ #
166
+ # The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
167
+ #
168
+ # link_to("Destroy", "http://www.example.com", method: :delete)
169
+ # # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
170
+ #
171
+ # You can also use custom data attributes using the <tt>:data</tt> option:
172
+ #
173
+ # link_to "Visit Other Site", "http://www.rubyonrails.org/", data: { confirm: "Are you sure?" }
174
+ # # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?">Visit Other Site</a>
175
+ def link_to(name = nil, options = nil, html_options = nil, &block)
176
+ html_options, options, name = options, name, block if block_given?
177
+ options ||= {}
178
+
179
+ html_options = convert_options_to_data_attributes(options, html_options)
180
+
181
+ url = url_for(options)
182
+ html_options['href'] ||= url
183
+
184
+ content_tag(:a, name || url, html_options, &block)
185
+ end
186
+
187
+ # Generates a form containing a single button that submits to the URL created
188
+ # by the set of +options+. This is the safest method to ensure links that
189
+ # cause changes to your data are not triggered by search bots or accelerators.
190
+ # If the HTML button does not work with your layout, you can also consider
191
+ # using the +link_to+ method with the <tt>:method</tt> modifier as described in
192
+ # the +link_to+ documentation.
193
+ #
194
+ # By default, the generated form element has a class name of <tt>button_to</tt>
195
+ # to allow styling of the form itself and its children. This can be changed
196
+ # using the <tt>:form_class</tt> modifier within +html_options+. You can control
197
+ # the form submission and input element behavior using +html_options+.
198
+ # This method accepts the <tt>:method</tt> modifier described in the +link_to+ documentation.
199
+ # If no <tt>:method</tt> modifier is given, it will default to performing a POST operation.
200
+ # You can also disable the button by passing <tt>disabled: true</tt> in +html_options+.
201
+ # If you are using RESTful routes, you can pass the <tt>:method</tt>
202
+ # to change the HTTP verb used to submit the form.
203
+ #
204
+ # ==== Options
205
+ # The +options+ hash accepts the same options as +url_for+.
206
+ #
207
+ # There are a few special +html_options+:
208
+ # * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
209
+ # <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
210
+ # * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
211
+ # * <tt>:data</tt> - This option can be used to add custom data attributes.
212
+ # * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
213
+ # submit behavior. By default this behavior is an ajax submit.
214
+ # * <tt>:form</tt> - This hash will be form attributes
215
+ # * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
216
+ # be placed
217
+ # * <tt>:params</tt> - Hash of parameters to be rendered as hidden fields within the form.
218
+ #
219
+ # ==== Data attributes
220
+ #
221
+ # * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
222
+ # prompt with the question specified. If the user accepts, the link is
223
+ # processed normally, otherwise no action is taken.
224
+ # * <tt>:disable_with</tt> - Value of this parameter will be
225
+ # used as the value for a disabled version of the submit
226
+ # button when the form is submitted. This feature is provided
227
+ # by the unobtrusive JavaScript driver.
228
+ #
229
+ # ==== Examples
230
+ # <%= button_to "New", action: "new" %>
231
+ # # => "<form method="post" action="/controller/new" class="button_to">
232
+ # # <div><input value="New" type="submit" /></div>
233
+ # # </form>"
234
+ #
235
+ # <%= button_to [:make_happy, @user] do %>
236
+ # Make happy <strong><%= @user.name %></strong>
237
+ # <% end %>
238
+ # # => "<form method="post" action="/users/1/make_happy" class="button_to">
239
+ # # <div>
240
+ # # <button type="submit">
241
+ # # Make happy <strong><%= @user.name %></strong>
242
+ # # </button>
243
+ # # </div>
244
+ # # </form>"
245
+ #
246
+ # <%= button_to "New", { action: "new" }, form_class: "new-thing" %>
247
+ # # => "<form method="post" action="/controller/new" class="new-thing">
248
+ # # <div><input value="New" type="submit" /></div>
249
+ # # </form>"
250
+ #
251
+ #
252
+ # <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %>
253
+ # # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
254
+ # # <div>
255
+ # # <input value="Create" type="submit" />
256
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
257
+ # # </div>
258
+ # # </form>"
259
+ #
260
+ #
261
+ # <%= button_to "Delete Image", { action: "delete", id: @image.id },
262
+ # method: :delete, data: { confirm: "Are you sure?" } %>
263
+ # # => "<form method="post" action="/images/delete/1" class="button_to">
264
+ # # <div>
265
+ # # <input type="hidden" name="_method" value="delete" />
266
+ # # <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
267
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
268
+ # # </div>
269
+ # # </form>"
270
+ #
271
+ #
272
+ # <%= button_to('Destroy', 'http://www.example.com',
273
+ # method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
274
+ # # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
275
+ # # <div>
276
+ # # <input name='_method' value='delete' type='hidden' />
277
+ # # <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
278
+ # # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
279
+ # # </div>
280
+ # # </form>"
281
+ # #
282
+ def button_to(name = nil, options = nil, html_options = nil, &block)
283
+ html_options, options = options, name if block_given?
284
+ options ||= {}
285
+ html_options ||= {}
286
+
287
+ html_options = html_options.stringify_keys
288
+ convert_boolean_attributes!(html_options, %w(disabled))
289
+
290
+ url = options.is_a?(String) ? options : url_for(options)
291
+ remote = html_options.delete('remote')
292
+ params = html_options.delete('params')
293
+
294
+ method = html_options.delete('method').to_s
295
+ method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : ''.html_safe
296
+
297
+ form_method = method == 'get' ? 'get' : 'post'
298
+ form_options = html_options.delete('form') || {}
299
+ form_options[:class] ||= html_options.delete('form_class') || 'button_to'
300
+ form_options.merge!(method: form_method, action: url)
301
+ form_options.merge!("data-remote" => "true") if remote
302
+
303
+ request_token_tag = form_method == 'post' ? token_tag : ''
304
+
305
+ html_options = convert_options_to_data_attributes(options, html_options)
306
+ html_options['type'] = 'submit'
307
+
308
+ button = if block_given?
309
+ content_tag('button', html_options, &block)
310
+ else
311
+ html_options['value'] = name || url
312
+ tag('input', html_options)
313
+ end
314
+
315
+ inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
316
+ if params
317
+ params.each do |param_name, value|
318
+ inner_tags.safe_concat tag(:input, type: "hidden", name: param_name, value: value.to_param)
319
+ end
320
+ end
321
+ content_tag('form', content_tag('div', inner_tags), form_options)
322
+ end
323
+
324
+ # Creates a link tag of the given +name+ using a URL created by the set of
325
+ # +options+ unless the current request URI is the same as the links, in
326
+ # which case only the name is returned (or the given block is yielded, if
327
+ # one exists). You can give +link_to_unless_current+ a block which will
328
+ # specialize the default behavior (e.g., show a "Start Here" link rather
329
+ # than the link's text).
330
+ #
331
+ # ==== Examples
332
+ # Let's say you have a navigation menu...
333
+ #
334
+ # <ul id="navbar">
335
+ # <li><%= link_to_unless_current("Home", { action: "index" }) %></li>
336
+ # <li><%= link_to_unless_current("About Us", { action: "about" }) %></li>
337
+ # </ul>
338
+ #
339
+ # If in the "about" action, it will render...
340
+ #
341
+ # <ul id="navbar">
342
+ # <li><a href="/controller/index">Home</a></li>
343
+ # <li>About Us</li>
344
+ # </ul>
345
+ #
346
+ # ...but if in the "index" action, it will render:
347
+ #
348
+ # <ul id="navbar">
349
+ # <li>Home</li>
350
+ # <li><a href="/controller/about">About Us</a></li>
351
+ # </ul>
352
+ #
353
+ # The implicit block given to +link_to_unless_current+ is evaluated if the current
354
+ # action is the action given. So, if we had a comments page and wanted to render a
355
+ # "Go Back" link instead of a link to the comments page, we could do something like this...
356
+ #
357
+ # <%=
358
+ # link_to_unless_current("Comment", { controller: "comments", action: "new" }) do
359
+ # link_to("Go back", { controller: "posts", action: "index" })
360
+ # end
361
+ # %>
362
+ def link_to_unless_current(name, options = {}, html_options = {}, &block)
363
+ link_to_unless current_page?(options), name, options, html_options, &block
364
+ end
365
+
366
+ # Creates a link tag of the given +name+ using a URL created by the set of
367
+ # +options+ unless +condition+ is true, in which case only the name is
368
+ # returned. To specialize the default behavior (i.e., show a login link rather
369
+ # than just the plaintext link text), you can pass a block that
370
+ # accepts the name or the full argument list for +link_to_unless+.
371
+ #
372
+ # ==== Examples
373
+ # <%= link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) %>
374
+ # # If the user is logged in...
375
+ # # => <a href="/controller/reply/">Reply</a>
376
+ #
377
+ # <%=
378
+ # link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) do |name|
379
+ # link_to(name, { controller: "accounts", action: "signup" })
380
+ # end
381
+ # %>
382
+ # # If the user is logged in...
383
+ # # => <a href="/controller/reply/">Reply</a>
384
+ # # If not...
385
+ # # => <a href="/accounts/signup">Reply</a>
386
+ def link_to_unless(condition, name, options = {}, html_options = {}, &block)
387
+ if condition
388
+ if block_given?
389
+ block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
390
+ else
391
+ ERB::Util.html_escape(name)
392
+ end
393
+ else
394
+ link_to(name, options, html_options)
395
+ end
396
+ end
397
+
398
+ # Creates a link tag of the given +name+ using a URL created by the set of
399
+ # +options+ if +condition+ is true, otherwise only the name is
400
+ # returned. To specialize the default behavior, you can pass a block that
401
+ # accepts the name or the full argument list for +link_to_unless+ (see the examples
402
+ # in +link_to_unless+).
403
+ #
404
+ # ==== Examples
405
+ # <%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
406
+ # # If the user isn't logged in...
407
+ # # => <a href="/sessions/new/">Login</a>
408
+ #
409
+ # <%=
410
+ # link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) do
411
+ # link_to(@current_user.login, { controller: "accounts", action: "show", id: @current_user })
412
+ # end
413
+ # %>
414
+ # # If the user isn't logged in...
415
+ # # => <a href="/sessions/new/">Login</a>
416
+ # # If they are logged in...
417
+ # # => <a href="/accounts/show/3">my_username</a>
418
+ def link_to_if(condition, name, options = {}, html_options = {}, &block)
419
+ link_to_unless !condition, name, options, html_options, &block
420
+ end
421
+
422
+ # Creates a mailto link tag to the specified +email_address+, which is
423
+ # also used as the name of the link unless +name+ is specified. Additional
424
+ # HTML attributes for the link can be passed in +html_options+.
425
+ #
426
+ # +mail_to+ has several methods for customizing the email itself by
427
+ # passing special keys to +html_options+.
428
+ #
429
+ # ==== Options
430
+ # * <tt>:subject</tt> - Preset the subject line of the email.
431
+ # * <tt>:body</tt> - Preset the body of the email.
432
+ # * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
433
+ # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
434
+ #
435
+ # ==== Obfuscation
436
+ # Prior to Rails 4.0, +mail_to+ provided options for encoding the address
437
+ # in order to hinder email harvesters. To take advantage of these options,
438
+ # install the +actionview-encoded_mail_to+ gem.
439
+ #
440
+ # ==== Examples
441
+ # mail_to "me@domain.com"
442
+ # # => <a href="mailto:me@domain.com">me@domain.com</a>
443
+ #
444
+ # mail_to "me@domain.com", "My email"
445
+ # # => <a href="mailto:me@domain.com">My email</a>
446
+ #
447
+ # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
448
+ # subject: "This is an example email"
449
+ # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
450
+ #
451
+ # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
452
+ #
453
+ # <%= mail_to "me@domain.com" do %>
454
+ # <strong>Email me:</strong> <span>me@domain.com</span>
455
+ # <% end %>
456
+ # # => <a href="mailto:me@domain.com">
457
+ # <strong>Email me:</strong> <span>me@domain.com</span>
458
+ # </a>
459
+ def mail_to(email_address, name = nil, html_options = {}, &block)
460
+ email_address = ERB::Util.html_escape(email_address)
461
+
462
+ html_options, name = name, nil if block_given?
463
+ html_options = (html_options || {}).stringify_keys
464
+
465
+ extras = %w{ cc bcc body subject }.map! { |item|
466
+ option = html_options.delete(item) || next
467
+ "#{item}=#{Rack::Utils.escape_path(option)}"
468
+ }.compact
469
+ extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))
470
+
471
+ html_options["href"] = "mailto:#{email_address}#{extras}".html_safe
472
+
473
+ content_tag(:a, name || email_address.html_safe, html_options, &block)
474
+ end
475
+
476
+ # True if the current request URI was generated by the given +options+.
477
+ #
478
+ # ==== Examples
479
+ # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc</tt> action.
480
+ #
481
+ # current_page?(action: 'process')
482
+ # # => false
483
+ #
484
+ # current_page?(controller: 'shop', action: 'checkout')
485
+ # # => true
486
+ #
487
+ # current_page?(controller: 'shop', action: 'checkout', order: 'asc')
488
+ # # => false
489
+ #
490
+ # current_page?(action: 'checkout')
491
+ # # => true
492
+ #
493
+ # current_page?(controller: 'library', action: 'checkout')
494
+ # # => false
495
+ #
496
+ # current_page?('http://www.example.com/shop/checkout')
497
+ # # => true
498
+ #
499
+ # current_page?('/shop/checkout')
500
+ # # => true
501
+ #
502
+ # Let's say we're in the <tt>http://www.example.com/shop/checkout?order=desc&page=1</tt> action.
503
+ #
504
+ # current_page?(action: 'process')
505
+ # # => false
506
+ #
507
+ # current_page?(controller: 'shop', action: 'checkout')
508
+ # # => true
509
+ #
510
+ # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '1')
511
+ # # => true
512
+ #
513
+ # current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '2')
514
+ # # => false
515
+ #
516
+ # current_page?(controller: 'shop', action: 'checkout', order: 'desc')
517
+ # # => false
518
+ #
519
+ # current_page?(action: 'checkout')
520
+ # # => true
521
+ #
522
+ # current_page?(controller: 'library', action: 'checkout')
523
+ # # => false
524
+ #
525
+ # Let's say we're in the <tt>http://www.example.com/products</tt> action with method POST in case of invalid product.
526
+ #
527
+ # current_page?(controller: 'product', action: 'index')
528
+ # # => false
529
+ #
530
+ def current_page?(options)
531
+ unless request
532
+ raise "You cannot use helpers that need to determine the current " \
533
+ "page unless your view context provides a Request object " \
534
+ "in a #request method"
535
+ end
536
+
537
+ return false unless request.get? || request.head?
538
+
539
+ url_string = URI.parser.unescape(url_for(options)).force_encoding(Encoding::BINARY)
540
+
541
+ # We ignore any extra parameters in the request_uri if the
542
+ # submitted url doesn't have any either. This lets the function
543
+ # work with things like ?order=asc
544
+ request_uri = url_string.index("?") ? request.fullpath : request.path
545
+ request_uri = URI.parser.unescape(request_uri).force_encoding(Encoding::BINARY)
546
+
547
+ if url_string =~ /^\w+:\/\//
548
+ url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
549
+ else
550
+ url_string == request_uri
551
+ end
552
+ end
553
+
554
+ private
555
+ def convert_options_to_data_attributes(options, html_options)
556
+ if html_options
557
+ html_options = html_options.stringify_keys
558
+ html_options['data-remote'] = 'true' if link_to_remote_options?(options) || link_to_remote_options?(html_options)
559
+
560
+ method = html_options.delete('method')
561
+
562
+ add_method_to_attributes!(html_options, method) if method
563
+
564
+ html_options
565
+ else
566
+ link_to_remote_options?(options) ? {'data-remote' => 'true'} : {}
567
+ end
568
+ end
569
+
570
+ def link_to_remote_options?(options)
571
+ if options.is_a?(Hash)
572
+ options.delete('remote') || options.delete(:remote)
573
+ end
574
+ end
575
+
576
+ def add_method_to_attributes!(html_options, method)
577
+ if method && method.to_s.downcase != "get" && html_options["rel"] !~ /nofollow/
578
+ html_options["rel"] = "#{html_options["rel"]} nofollow".lstrip
579
+ end
580
+ html_options["data-method"] = method
581
+ end
582
+
583
+ # Processes the +html_options+ hash, converting the boolean
584
+ # attributes from true/false form into the form required by
585
+ # HTML/XHTML. (An attribute is considered to be boolean if
586
+ # its name is listed in the given +bool_attrs+ array.)
587
+ #
588
+ # More specifically, for each boolean attribute in +html_options+
589
+ # given as:
590
+ #
591
+ # "attr" => bool_value
592
+ #
593
+ # if the associated +bool_value+ evaluates to true, it is
594
+ # replaced with the attribute's name; otherwise the attribute is
595
+ # removed from the +html_options+ hash. (See the XHTML 1.0 spec,
596
+ # section 4.5 "Attribute Minimization" for more:
597
+ # http://www.w3.org/TR/xhtml1/#h-4.5)
598
+ #
599
+ # Returns the updated +html_options+ hash, which is also modified
600
+ # in place.
601
+ #
602
+ # Example:
603
+ #
604
+ # convert_boolean_attributes!( html_options,
605
+ # %w( checked disabled readonly ) )
606
+ def convert_boolean_attributes!(html_options, bool_attrs)
607
+ bool_attrs.each { |x| html_options[x] = x if html_options.delete(x) }
608
+ html_options
609
+ end
610
+
611
+ def token_tag(token=nil)
612
+ if token != false && protect_against_forgery?
613
+ token ||= form_authenticity_token
614
+ tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token)
615
+ else
616
+ ''
617
+ end
618
+ end
619
+
620
+ def method_tag(method)
621
+ tag('input', type: 'hidden', name: '_method', value: method.to_s)
622
+ end
623
+ end
624
+ end
625
+ end