halorgium-actionpack 3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG +5179 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +409 -0
  4. data/lib/abstract_controller.rb +16 -0
  5. data/lib/abstract_controller/base.rb +158 -0
  6. data/lib/abstract_controller/callbacks.rb +113 -0
  7. data/lib/abstract_controller/exceptions.rb +12 -0
  8. data/lib/abstract_controller/helpers.rb +151 -0
  9. data/lib/abstract_controller/layouts.rb +250 -0
  10. data/lib/abstract_controller/localized_cache.rb +49 -0
  11. data/lib/abstract_controller/logger.rb +61 -0
  12. data/lib/abstract_controller/rendering_controller.rb +188 -0
  13. data/lib/action_controller.rb +72 -0
  14. data/lib/action_controller/base.rb +168 -0
  15. data/lib/action_controller/caching.rb +80 -0
  16. data/lib/action_controller/caching/actions.rb +163 -0
  17. data/lib/action_controller/caching/fragments.rb +116 -0
  18. data/lib/action_controller/caching/pages.rb +154 -0
  19. data/lib/action_controller/caching/sweeping.rb +97 -0
  20. data/lib/action_controller/deprecated.rb +4 -0
  21. data/lib/action_controller/deprecated/integration_test.rb +2 -0
  22. data/lib/action_controller/deprecated/performance_test.rb +1 -0
  23. data/lib/action_controller/dispatch/dispatcher.rb +57 -0
  24. data/lib/action_controller/metal.rb +129 -0
  25. data/lib/action_controller/metal/benchmarking.rb +73 -0
  26. data/lib/action_controller/metal/compatibility.rb +145 -0
  27. data/lib/action_controller/metal/conditional_get.rb +86 -0
  28. data/lib/action_controller/metal/configuration.rb +28 -0
  29. data/lib/action_controller/metal/cookies.rb +105 -0
  30. data/lib/action_controller/metal/exceptions.rb +55 -0
  31. data/lib/action_controller/metal/filter_parameter_logging.rb +77 -0
  32. data/lib/action_controller/metal/flash.rb +162 -0
  33. data/lib/action_controller/metal/head.rb +27 -0
  34. data/lib/action_controller/metal/helpers.rb +115 -0
  35. data/lib/action_controller/metal/hide_actions.rb +47 -0
  36. data/lib/action_controller/metal/http_authentication.rb +312 -0
  37. data/lib/action_controller/metal/layouts.rb +171 -0
  38. data/lib/action_controller/metal/mime_responds.rb +317 -0
  39. data/lib/action_controller/metal/rack_convenience.rb +27 -0
  40. data/lib/action_controller/metal/redirector.rb +22 -0
  41. data/lib/action_controller/metal/render_options.rb +103 -0
  42. data/lib/action_controller/metal/rendering_controller.rb +57 -0
  43. data/lib/action_controller/metal/request_forgery_protection.rb +108 -0
  44. data/lib/action_controller/metal/rescuable.rb +13 -0
  45. data/lib/action_controller/metal/responder.rb +200 -0
  46. data/lib/action_controller/metal/session.rb +15 -0
  47. data/lib/action_controller/metal/session_management.rb +45 -0
  48. data/lib/action_controller/metal/streaming.rb +188 -0
  49. data/lib/action_controller/metal/testing.rb +39 -0
  50. data/lib/action_controller/metal/url_for.rb +41 -0
  51. data/lib/action_controller/metal/verification.rb +130 -0
  52. data/lib/action_controller/middleware.rb +38 -0
  53. data/lib/action_controller/notifications.rb +10 -0
  54. data/lib/action_controller/polymorphic_routes.rb +183 -0
  55. data/lib/action_controller/record_identifier.rb +91 -0
  56. data/lib/action_controller/testing/process.rb +111 -0
  57. data/lib/action_controller/testing/test_case.rb +345 -0
  58. data/lib/action_controller/translation.rb +13 -0
  59. data/lib/action_controller/url_rewriter.rb +204 -0
  60. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  61. data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
  62. data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
  63. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +176 -0
  64. data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
  65. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
  66. data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
  67. data/lib/action_dispatch.rb +70 -0
  68. data/lib/action_dispatch/http/headers.rb +33 -0
  69. data/lib/action_dispatch/http/mime_type.rb +231 -0
  70. data/lib/action_dispatch/http/mime_types.rb +23 -0
  71. data/lib/action_dispatch/http/request.rb +539 -0
  72. data/lib/action_dispatch/http/response.rb +290 -0
  73. data/lib/action_dispatch/http/status_codes.rb +42 -0
  74. data/lib/action_dispatch/http/utils.rb +20 -0
  75. data/lib/action_dispatch/middleware/callbacks.rb +50 -0
  76. data/lib/action_dispatch/middleware/params_parser.rb +79 -0
  77. data/lib/action_dispatch/middleware/rescue.rb +26 -0
  78. data/lib/action_dispatch/middleware/session/abstract_store.rb +208 -0
  79. data/lib/action_dispatch/middleware/session/cookie_store.rb +235 -0
  80. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +47 -0
  81. data/lib/action_dispatch/middleware/show_exceptions.rb +143 -0
  82. data/lib/action_dispatch/middleware/stack.rb +116 -0
  83. data/lib/action_dispatch/middleware/static.rb +44 -0
  84. data/lib/action_dispatch/middleware/string_coercion.rb +29 -0
  85. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +24 -0
  86. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +26 -0
  87. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +10 -0
  88. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +29 -0
  89. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +2 -0
  90. data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +10 -0
  91. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +21 -0
  92. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +2 -0
  93. data/lib/action_dispatch/routing.rb +381 -0
  94. data/lib/action_dispatch/routing/deprecated_mapper.rb +878 -0
  95. data/lib/action_dispatch/routing/mapper.rb +327 -0
  96. data/lib/action_dispatch/routing/route.rb +49 -0
  97. data/lib/action_dispatch/routing/route_set.rb +497 -0
  98. data/lib/action_dispatch/testing/assertions.rb +8 -0
  99. data/lib/action_dispatch/testing/assertions/dom.rb +35 -0
  100. data/lib/action_dispatch/testing/assertions/model.rb +19 -0
  101. data/lib/action_dispatch/testing/assertions/response.rb +145 -0
  102. data/lib/action_dispatch/testing/assertions/routing.rb +144 -0
  103. data/lib/action_dispatch/testing/assertions/selector.rb +639 -0
  104. data/lib/action_dispatch/testing/assertions/tag.rb +123 -0
  105. data/lib/action_dispatch/testing/integration.rb +504 -0
  106. data/lib/action_dispatch/testing/performance_test.rb +15 -0
  107. data/lib/action_dispatch/testing/test_request.rb +83 -0
  108. data/lib/action_dispatch/testing/test_response.rb +131 -0
  109. data/lib/action_pack.rb +24 -0
  110. data/lib/action_pack/version.rb +9 -0
  111. data/lib/action_view.rb +58 -0
  112. data/lib/action_view/base.rb +308 -0
  113. data/lib/action_view/context.rb +44 -0
  114. data/lib/action_view/erb/util.rb +48 -0
  115. data/lib/action_view/helpers.rb +62 -0
  116. data/lib/action_view/helpers/active_model_helper.rb +306 -0
  117. data/lib/action_view/helpers/ajax_helper.rb +68 -0
  118. data/lib/action_view/helpers/asset_tag_helper.rb +830 -0
  119. data/lib/action_view/helpers/atom_feed_helper.rb +198 -0
  120. data/lib/action_view/helpers/cache_helper.rb +39 -0
  121. data/lib/action_view/helpers/capture_helper.rb +168 -0
  122. data/lib/action_view/helpers/date_helper.rb +988 -0
  123. data/lib/action_view/helpers/debug_helper.rb +38 -0
  124. data/lib/action_view/helpers/form_helper.rb +1102 -0
  125. data/lib/action_view/helpers/form_options_helper.rb +600 -0
  126. data/lib/action_view/helpers/form_tag_helper.rb +495 -0
  127. data/lib/action_view/helpers/javascript_helper.rb +208 -0
  128. data/lib/action_view/helpers/number_helper.rb +311 -0
  129. data/lib/action_view/helpers/prototype_helper.rb +1309 -0
  130. data/lib/action_view/helpers/raw_output_helper.rb +9 -0
  131. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  132. data/lib/action_view/helpers/record_tag_helper.rb +58 -0
  133. data/lib/action_view/helpers/sanitize_helper.rb +259 -0
  134. data/lib/action_view/helpers/scriptaculous_helper.rb +226 -0
  135. data/lib/action_view/helpers/tag_helper.rb +151 -0
  136. data/lib/action_view/helpers/text_helper.rb +594 -0
  137. data/lib/action_view/helpers/translation_helper.rb +39 -0
  138. data/lib/action_view/helpers/url_helper.rb +639 -0
  139. data/lib/action_view/locale/en.yml +117 -0
  140. data/lib/action_view/paths.rb +80 -0
  141. data/lib/action_view/render/partials.rb +342 -0
  142. data/lib/action_view/render/rendering.rb +134 -0
  143. data/lib/action_view/safe_buffer.rb +28 -0
  144. data/lib/action_view/template/error.rb +101 -0
  145. data/lib/action_view/template/handler.rb +36 -0
  146. data/lib/action_view/template/handlers.rb +52 -0
  147. data/lib/action_view/template/handlers/builder.rb +17 -0
  148. data/lib/action_view/template/handlers/erb.rb +53 -0
  149. data/lib/action_view/template/handlers/rjs.rb +18 -0
  150. data/lib/action_view/template/resolver.rb +165 -0
  151. data/lib/action_view/template/template.rb +131 -0
  152. data/lib/action_view/template/text.rb +38 -0
  153. data/lib/action_view/test_case.rb +163 -0
  154. metadata +236 -0
@@ -0,0 +1,39 @@
1
+ require 'action_view/helpers/tag_helper'
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module TranslationHelper
6
+ # Delegates to I18n#translate but also performs two additional functions. First, it'll catch MissingTranslationData exceptions
7
+ # and turn them into inline spans that contains the missing key, such that you can see in a view what is missing where.
8
+ #
9
+ # Second, it'll scope the key by the current partial if the key starts with a period. So if you call translate(".foo") from the
10
+ # people/index.html.erb template, you'll actually be calling I18n.translate("people.index.foo"). This makes it less repetitive
11
+ # to translate many keys within the same partials and gives you a simple framework for scoping them consistently. If you don't
12
+ # prepend the key with a period, nothing is converted.
13
+ def translate(key, options = {})
14
+ options[:raise] = true
15
+ I18n.translate(scope_key_by_partial(key), options)
16
+ rescue I18n::MissingTranslationData => e
17
+ keys = I18n.send(:normalize_translation_keys, e.locale, e.key, e.options[:scope])
18
+ content_tag('span', keys.join(', '), :class => 'translation_missing')
19
+ end
20
+ alias :t :translate
21
+
22
+ # Delegates to I18n.localize with no additional functionality.
23
+ def localize(*args)
24
+ I18n.localize *args
25
+ end
26
+ alias :l :localize
27
+
28
+
29
+ private
30
+ def scope_key_by_partial(key)
31
+ if key.to_s.first == "."
32
+ template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s
33
+ else
34
+ key
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,639 @@
1
+ require 'action_view/helpers/javascript_helper'
2
+ require 'active_support/core_ext/array/access'
3
+ require 'active_support/core_ext/hash/keys'
4
+
5
+ module ActionView
6
+ module Helpers #:nodoc:
7
+ # Provides a set of methods for making links and getting URLs that
8
+ # depend on the routing subsystem (see ActionController::Routing).
9
+ # This allows you to use the same format for links in views
10
+ # and controllers.
11
+ module UrlHelper
12
+ include JavaScriptHelper
13
+
14
+ # Returns the URL for the set of +options+ provided. This takes the
15
+ # same options as +url_for+ in Action Controller (see the
16
+ # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
17
+ # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
18
+ # instead of the fully qualified URL like "http://example.com/controller/action".
19
+ #
20
+ # When called from a view, +url_for+ returns an HTML escaped url. If you
21
+ # need an unescaped url, pass <tt>:escape => false</tt> in the +options+.
22
+ #
23
+ # ==== Options
24
+ # * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
25
+ # * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
26
+ # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
27
+ # is currently not recommended since it breaks caching.
28
+ # * <tt>:host</tt> - Overrides the default (current) host if provided.
29
+ # * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
30
+ # * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
31
+ # * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
32
+ # * <tt>:escape</tt> - Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default).
33
+ #
34
+ # ==== Relying on named routes
35
+ #
36
+ # If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter,
37
+ # you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing
38
+ # a Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
39
+ # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
40
+ #
41
+ # ==== Examples
42
+ # <%= url_for(:action => 'index') %>
43
+ # # => /blog/
44
+ #
45
+ # <%= url_for(:action => 'find', :controller => 'books') %>
46
+ # # => /books/find
47
+ #
48
+ # <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
49
+ # # => https://www.railsapplication.com/members/login/
50
+ #
51
+ # <%= url_for(:action => 'play', :anchor => 'player') %>
52
+ # # => /messages/play/#player
53
+ #
54
+ # <%= url_for(:action => 'checkout', :anchor => 'tax&ship') %>
55
+ # # => /testing/jump/#tax&amp;ship
56
+ #
57
+ # <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %>
58
+ # # => /testing/jump/#tax&ship
59
+ #
60
+ # <%= url_for(Workshop.new) %>
61
+ # # relies on Workshop answering a new_record? call (and in this case returning true)
62
+ # # => /workshops
63
+ #
64
+ # <%= url_for(@workshop) %>
65
+ # # calls @workshop.to_s
66
+ # # => /workshops/5
67
+ #
68
+ # <%= url_for("http://www.example.com") %>
69
+ # # => http://www.example.com
70
+ #
71
+ # <%= url_for(:back) %>
72
+ # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
73
+ # # => http://www.example.com
74
+ #
75
+ # <%= url_for(:back) %>
76
+ # # if request.env["HTTP_REFERER"] is not set or is blank
77
+ # # => javascript:history.back()
78
+ def url_for(options = {})
79
+ options ||= {}
80
+ url = case options
81
+ when String
82
+ escape = true
83
+ options
84
+ when Hash
85
+ options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
86
+ escape = options.key?(:escape) ? options.delete(:escape) : false
87
+ @controller.send(:url_for, options)
88
+ when :back
89
+ escape = false
90
+ @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
91
+ else
92
+ escape = false
93
+ polymorphic_path(options)
94
+ end
95
+
96
+ escape ? escape_once(url).html_safe! : url
97
+ end
98
+
99
+ # Creates a link tag of the given +name+ using a URL created by the set
100
+ # of +options+. See the valid options in the documentation for
101
+ # +url_for+. It's also possible to pass a string instead
102
+ # of an options hash to get a link tag that uses the value of the string as the
103
+ # href for the link, or use <tt>:back</tt> to link to the referrer - a JavaScript back
104
+ # link will be used in place of a referrer if none exists. If +nil+ is passed as
105
+ # a name, the link itself will become the name.
106
+ #
107
+ # ==== Signatures
108
+ #
109
+ # link_to(name, options = {}, html_options = nil)
110
+ # link_to(options = {}, html_options = nil) do
111
+ # # name
112
+ # end
113
+ #
114
+ # ==== Options
115
+ # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
116
+ # prompt with the question specified. If the user accepts, the link is
117
+ # processed normally, otherwise no action is taken.
118
+ # * <tt>:popup => true || array of window options</tt> - This will force the
119
+ # link to open in a popup window. By passing true, a default browser window
120
+ # will be opened with the URL. You can also specify an array of options
121
+ # that are passed to the <tt>window.open</tt> JavaScript call.
122
+ # * <tt>:method => symbol of HTTP verb</tt> - This modifier will dynamically
123
+ # create an HTML form and immediately submit the form for processing using
124
+ # the HTTP verb specified. Useful for having links perform a POST operation
125
+ # in dangerous actions like deleting a record (which search bots can follow
126
+ # while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt> and <tt>:put</tt>.
127
+ # Note that if the user has JavaScript disabled, the request will fall back
128
+ # to using GET. If <tt>:href => '#'</tt> is used and the user has JavaScript
129
+ # disabled clicking the link will have no effect. If you are relying on the
130
+ # POST behavior, you should check for it in your controller's action by using
131
+ # the request object's methods for <tt>post?</tt>, <tt>delete?</tt> or <tt>put?</tt>.
132
+ # * The +html_options+ will accept a hash of html attributes for the link tag.
133
+ #
134
+ # You can mix and match the +html_options+ with the exception of
135
+ # <tt>:popup</tt> and <tt>:method</tt> which will raise an
136
+ # <tt>ActionView::ActionViewError</tt> exception.
137
+ #
138
+ # ==== Examples
139
+ # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
140
+ # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
141
+ # your application on resources and use
142
+ #
143
+ # link_to "Profile", profile_path(@profile)
144
+ # # => <a href="/profiles/1">Profile</a>
145
+ #
146
+ # or the even pithier
147
+ #
148
+ # link_to "Profile", @profile
149
+ # # => <a href="/profiles/1">Profile</a>
150
+ #
151
+ # in place of the older more verbose, non-resource-oriented
152
+ #
153
+ # link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
154
+ # # => <a href="/profiles/show/1">Profile</a>
155
+ #
156
+ # Similarly,
157
+ #
158
+ # link_to "Profiles", profiles_path
159
+ # # => <a href="/profiles">Profiles</a>
160
+ #
161
+ # is better than
162
+ #
163
+ # link_to "Profiles", :controller => "profiles"
164
+ # # => <a href="/profiles">Profiles</a>
165
+ #
166
+ # You can use a block as well if your link target is hard to fit into the name parameter. ERb example:
167
+ #
168
+ # <% link_to(@profile) do %>
169
+ # <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
170
+ # <% end %>
171
+ # # => <a href="/profiles/1">
172
+ # <strong>David</strong> -- <span>Check it out!</span>
173
+ # </a>
174
+ #
175
+ # Classes and ids for CSS are easy to produce:
176
+ #
177
+ # link_to "Articles", articles_path, :id => "news", :class => "article"
178
+ # # => <a href="/articles" class="article" id="news">Articles</a>
179
+ #
180
+ # Be careful when using the older argument style, as an extra literal hash is needed:
181
+ #
182
+ # link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
183
+ # # => <a href="/articles" class="article" id="news">Articles</a>
184
+ #
185
+ # Leaving the hash off gives the wrong link:
186
+ #
187
+ # link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
188
+ # # => <a href="/articles/index/news?class=article">WRONG!</a>
189
+ #
190
+ # +link_to+ can also produce links with anchors or query strings:
191
+ #
192
+ # link_to "Comment wall", profile_path(@profile, :anchor => "wall")
193
+ # # => <a href="/profiles/1#wall">Comment wall</a>
194
+ #
195
+ # link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
196
+ # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
197
+ #
198
+ # link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
199
+ # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
200
+ #
201
+ # The three options specific to +link_to+ (<tt>:confirm</tt>, <tt>:popup</tt>, and <tt>:method</tt>) are used as follows:
202
+ #
203
+ # link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
204
+ # # => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a>
205
+ #
206
+ # link_to "Help", { :action => "help" }, :popup => true
207
+ # # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a>
208
+ #
209
+ # link_to "View Image", @image, :popup => ['new_window_name', 'height=300,width=600']
210
+ # # => <a href="/images/9" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a>
211
+ #
212
+ # link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete
213
+ # # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
214
+ # f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
215
+ # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
216
+ # m.setAttribute('value', 'delete');var s = document.createElement('input'); s.setAttribute('type', 'hidden');
217
+ # s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'Q/ttlxPYZ6R77B+vZ1sBkhj21G2isO9dpE6UtOHBApg=');
218
+ # f.appendChild(s)f.appendChild(m);f.submit(); };return false;">Delete Image</a>
219
+ def link_to(*args, &block)
220
+ if block_given?
221
+ options = args.first || {}
222
+ html_options = args.second
223
+ concat(link_to(capture(&block), options, html_options).html_safe!)
224
+ else
225
+ name = args[0]
226
+ options = args[1] || {}
227
+ html_options = args[2]
228
+
229
+ url = url_for(options)
230
+
231
+ if html_options
232
+ html_options = html_options.stringify_keys
233
+ href = html_options['href']
234
+ convert_options_to_javascript!(html_options, url)
235
+ tag_options = tag_options(html_options)
236
+ else
237
+ tag_options = nil
238
+ end
239
+
240
+ href_attr = "href=\"#{url}\"" unless href
241
+ "<a #{href_attr}#{tag_options}>#{ERB::Util.h(name || url)}</a>".html_safe!
242
+ end
243
+ end
244
+
245
+ # Generates a form containing a single button that submits to the URL created
246
+ # by the set of +options+. This is the safest method to ensure links that
247
+ # cause changes to your data are not triggered by search bots or accelerators.
248
+ # If the HTML button does not work with your layout, you can also consider
249
+ # using the +link_to+ method with the <tt>:method</tt> modifier as described in
250
+ # the +link_to+ documentation.
251
+ #
252
+ # The generated form element has a class name of <tt>button-to</tt>
253
+ # to allow styling of the form itself and its children. You can control
254
+ # the form submission and input element behavior using +html_options+.
255
+ # This method accepts the <tt>:method</tt> and <tt>:confirm</tt> modifiers
256
+ # described in the +link_to+ documentation. If no <tt>:method</tt> modifier
257
+ # is given, it will default to performing a POST operation. You can also
258
+ # disable the button by passing <tt>:disabled => true</tt> in +html_options+.
259
+ # If you are using RESTful routes, you can pass the <tt>:method</tt>
260
+ # to change the HTTP verb used to submit the form.
261
+ #
262
+ # ==== Options
263
+ # The +options+ hash accepts the same options as url_for.
264
+ #
265
+ # There are a few special +html_options+:
266
+ # * <tt>:method</tt> - Specifies the anchor name to be appended to the path.
267
+ # * <tt>:disabled</tt> - Specifies the anchor name to be appended to the path.
268
+ # * <tt>:confirm</tt> - This will add a JavaScript confirm
269
+ # prompt with the question specified. If the user accepts, the link is
270
+ # processed normally, otherwise no action is taken.
271
+ #
272
+ # ==== Examples
273
+ # <%= button_to "New", :action => "new" %>
274
+ # # => "<form method="post" action="/controller/new" class="button-to">
275
+ # # <div><input value="New" type="submit" /></div>
276
+ # # </form>"
277
+ #
278
+ # button_to "Delete Image", { :action => "delete", :id => @image.id },
279
+ # :confirm => "Are you sure?", :method => :delete
280
+ # # => "<form method="post" action="/images/delete/1" class="button-to">
281
+ # # <div>
282
+ # # <input type="hidden" name="_method" value="delete" />
283
+ # # <input onclick="return confirm('Are you sure?');"
284
+ # # value="Delete" type="submit" />
285
+ # # </div>
286
+ # # </form>"
287
+ def button_to(name, options = {}, html_options = {})
288
+ html_options = html_options.stringify_keys
289
+ convert_boolean_attributes!(html_options, %w( disabled ))
290
+
291
+ method_tag = ''
292
+ if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
293
+ method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
294
+ end
295
+
296
+ form_method = method.to_s == 'get' ? 'get' : 'post'
297
+
298
+ request_token_tag = ''
299
+ if form_method == 'post' && protect_against_forgery?
300
+ request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
301
+ end
302
+
303
+ if confirm = html_options.delete("confirm")
304
+ html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
305
+ end
306
+
307
+ url = options.is_a?(String) ? options : self.url_for(options)
308
+ name ||= url
309
+
310
+ html_options.merge!("type" => "submit", "value" => name)
311
+
312
+ ("<form method=\"#{form_method}\" action=\"#{escape_once url}\" class=\"button-to\"><div>" +
313
+ method_tag + tag("input", html_options) + request_token_tag + "</div></form>").html_safe!
314
+ end
315
+
316
+
317
+ # Creates a link tag of the given +name+ using a URL created by the set of
318
+ # +options+ unless the current request URI is the same as the links, in
319
+ # which case only the name is returned (or the given block is yielded, if
320
+ # one exists). You can give +link_to_unless_current+ a block which will
321
+ # specialize the default behavior (e.g., show a "Start Here" link rather
322
+ # than the link's text).
323
+ #
324
+ # ==== Examples
325
+ # Let's say you have a navigation menu...
326
+ #
327
+ # <ul id="navbar">
328
+ # <li><%= link_to_unless_current("Home", { :action => "index" }) %></li>
329
+ # <li><%= link_to_unless_current("About Us", { :action => "about" }) %></li>
330
+ # </ul>
331
+ #
332
+ # If in the "about" action, it will render...
333
+ #
334
+ # <ul id="navbar">
335
+ # <li><a href="/controller/index">Home</a></li>
336
+ # <li>About Us</li>
337
+ # </ul>
338
+ #
339
+ # ...but if in the "index" action, it will render:
340
+ #
341
+ # <ul id="navbar">
342
+ # <li>Home</li>
343
+ # <li><a href="/controller/about">About Us</a></li>
344
+ # </ul>
345
+ #
346
+ # The implicit block given to +link_to_unless_current+ is evaluated if the current
347
+ # action is the action given. So, if we had a comments page and wanted to render a
348
+ # "Go Back" link instead of a link to the comments page, we could do something like this...
349
+ #
350
+ # <%=
351
+ # link_to_unless_current("Comment", { :controller => 'comments', :action => 'new}) do
352
+ # link_to("Go back", { :controller => 'posts', :action => 'index' })
353
+ # end
354
+ # %>
355
+ def link_to_unless_current(name, options = {}, html_options = {}, &block)
356
+ link_to_unless current_page?(options), name, options, html_options, &block
357
+ end
358
+
359
+ # Creates a link tag of the given +name+ using a URL created by the set of
360
+ # +options+ unless +condition+ is true, in which case only the name is
361
+ # returned. To specialize the default behavior (i.e., show a login link rather
362
+ # than just the plaintext link text), you can pass a block that
363
+ # accepts the name or the full argument list for +link_to_unless+.
364
+ #
365
+ # ==== Examples
366
+ # <%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>
367
+ # # If the user is logged in...
368
+ # # => <a href="/controller/reply/">Reply</a>
369
+ #
370
+ # <%=
371
+ # link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name|
372
+ # link_to(name, { :controller => "accounts", :action => "signup" })
373
+ # end
374
+ # %>
375
+ # # If the user is logged in...
376
+ # # => <a href="/controller/reply/">Reply</a>
377
+ # # If not...
378
+ # # => <a href="/accounts/signup">Reply</a>
379
+ def link_to_unless(condition, name, options = {}, html_options = {}, &block)
380
+ if condition
381
+ if block_given?
382
+ block.arity <= 1 ? yield(name) : yield(name, options, html_options)
383
+ else
384
+ name
385
+ end
386
+ else
387
+ link_to(name, options, html_options)
388
+ end
389
+ end
390
+
391
+ # Creates a link tag of the given +name+ using a URL created by the set of
392
+ # +options+ if +condition+ is true, in which case only the name is
393
+ # returned. To specialize the default behavior, you can pass a block that
394
+ # accepts the name or the full argument list for +link_to_unless+ (see the examples
395
+ # in +link_to_unless+).
396
+ #
397
+ # ==== Examples
398
+ # <%= link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) %>
399
+ # # If the user isn't logged in...
400
+ # # => <a href="/sessions/new/">Login</a>
401
+ #
402
+ # <%=
403
+ # link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
404
+ # link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
405
+ # end
406
+ # %>
407
+ # # If the user isn't logged in...
408
+ # # => <a href="/sessions/new/">Login</a>
409
+ # # If they are logged in...
410
+ # # => <a href="/accounts/show/3">my_username</a>
411
+ def link_to_if(condition, name, options = {}, html_options = {}, &block)
412
+ link_to_unless !condition, name, options, html_options, &block
413
+ end
414
+
415
+ # Creates a mailto link tag to the specified +email_address+, which is
416
+ # also used as the name of the link unless +name+ is specified. Additional
417
+ # HTML attributes for the link can be passed in +html_options+.
418
+ #
419
+ # +mail_to+ has several methods for hindering email harvesters and customizing
420
+ # the email itself by passing special keys to +html_options+.
421
+ #
422
+ # ==== Options
423
+ # * <tt>:encode</tt> - This key will accept the strings "javascript" or "hex".
424
+ # Passing "javascript" will dynamically create and encode the mailto link then
425
+ # eval it into the DOM of the page. This method will not show the link on
426
+ # the page if the user has JavaScript disabled. Passing "hex" will hex
427
+ # encode the +email_address+ before outputting the mailto link.
428
+ # * <tt>:replace_at</tt> - When the link +name+ isn't provided, the
429
+ # +email_address+ is used for the link label. You can use this option to
430
+ # obfuscate the +email_address+ by substituting the @ sign with the string
431
+ # given as the value.
432
+ # * <tt>:replace_dot</tt> - When the link +name+ isn't provided, the
433
+ # +email_address+ is used for the link label. You can use this option to
434
+ # obfuscate the +email_address+ by substituting the . in the email with the
435
+ # string given as the value.
436
+ # * <tt>:subject</tt> - Preset the subject line of the email.
437
+ # * <tt>:body</tt> - Preset the body of the email.
438
+ # * <tt>:cc</tt> - Carbon Copy addition recipients on the email.
439
+ # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
440
+ #
441
+ # ==== Examples
442
+ # mail_to "me@domain.com"
443
+ # # => <a href="mailto:me@domain.com">me@domain.com</a>
444
+ #
445
+ # mail_to "me@domain.com", "My email", :encode => "javascript"
446
+ # # => <script type="text/javascript">eval(decodeURIComponent('%64%6f%63...%27%29%3b'))</script>
447
+ #
448
+ # mail_to "me@domain.com", "My email", :encode => "hex"
449
+ # # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>
450
+ #
451
+ # mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
452
+ # # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
453
+ #
454
+ # mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
455
+ # :subject => "This is an example email"
456
+ # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
457
+ def mail_to(email_address, name = nil, html_options = {})
458
+ html_options = html_options.stringify_keys
459
+ encode = html_options.delete("encode").to_s
460
+ cc, bcc, subject, body = html_options.delete("cc"), html_options.delete("bcc"), html_options.delete("subject"), html_options.delete("body")
461
+
462
+ string = ''
463
+ extras = ''
464
+ extras << "cc=#{CGI.escape(cc).gsub("+", "%20")}&" unless cc.nil?
465
+ extras << "bcc=#{CGI.escape(bcc).gsub("+", "%20")}&" unless bcc.nil?
466
+ extras << "body=#{CGI.escape(body).gsub("+", "%20")}&" unless body.nil?
467
+ extras << "subject=#{CGI.escape(subject).gsub("+", "%20")}&" unless subject.nil?
468
+ extras = "?" << extras.gsub!(/&?$/,"") unless extras.empty?
469
+
470
+ email_address = email_address.to_s
471
+
472
+ email_address_obfuscated = email_address.dup
473
+ email_address_obfuscated.gsub!(/@/, html_options.delete("replace_at")) if html_options.has_key?("replace_at")
474
+ email_address_obfuscated.gsub!(/\./, html_options.delete("replace_dot")) if html_options.has_key?("replace_dot")
475
+
476
+ if encode == "javascript"
477
+ "document.write('#{content_tag("a", name || email_address_obfuscated, html_options.merge({ "href" => "mailto:"+email_address+extras }))}');".each_byte do |c|
478
+ string << sprintf("%%%x", c)
479
+ end
480
+ "<script type=\"#{Mime::JS}\">eval(decodeURIComponent('#{string}'))</script>"
481
+ elsif encode == "hex"
482
+ email_address_encoded = ''
483
+ email_address_obfuscated.each_byte do |c|
484
+ email_address_encoded << sprintf("&#%d;", c)
485
+ end
486
+
487
+ protocol = 'mailto:'
488
+ protocol.each_byte { |c| string << sprintf("&#%d;", c) }
489
+
490
+ email_address.each_byte do |c|
491
+ char = c.chr
492
+ string << (char =~ /\w/ ? sprintf("%%%x", c) : char)
493
+ end
494
+ content_tag "a", name || email_address_encoded, html_options.merge({ "href" => "#{string}#{extras}" })
495
+ else
496
+ content_tag "a", name || email_address_obfuscated, html_options.merge({ "href" => "mailto:#{email_address}#{extras}" })
497
+ end
498
+ end
499
+
500
+ # True if the current request URI was generated by the given +options+.
501
+ #
502
+ # ==== Examples
503
+ # Let's say we're in the <tt>/shop/checkout?order=desc</tt> action.
504
+ #
505
+ # current_page?(:action => 'process')
506
+ # # => false
507
+ #
508
+ # current_page?(:controller => 'shop', :action => 'checkout')
509
+ # # => true
510
+ #
511
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc')
512
+ # # => false
513
+ #
514
+ # current_page?(:action => 'checkout')
515
+ # # => true
516
+ #
517
+ # current_page?(:controller => 'library', :action => 'checkout')
518
+ # # => false
519
+ #
520
+ # Let's say we're in the <tt>/shop/checkout?order=desc&page=1</tt> action.
521
+ #
522
+ # current_page?(:action => 'process')
523
+ # # => false
524
+ #
525
+ # current_page?(:controller => 'shop', :action => 'checkout')
526
+ # # => true
527
+ #
528
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'1')
529
+ # # => true
530
+ #
531
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'2')
532
+ # # => false
533
+ #
534
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc')
535
+ # # => false
536
+ #
537
+ # current_page?(:action => 'checkout')
538
+ # # => true
539
+ #
540
+ # current_page?(:controller => 'library', :action => 'checkout')
541
+ # # => false
542
+ def current_page?(options)
543
+ url_string = CGI.unescapeHTML(url_for(options))
544
+ request = @controller.request
545
+ # We ignore any extra parameters in the request_uri if the
546
+ # submitted url doesn't have any either. This lets the function
547
+ # work with things like ?order=asc
548
+ if url_string.index("?")
549
+ request_uri = request.request_uri
550
+ else
551
+ request_uri = request.request_uri.split('?').first
552
+ end
553
+ if url_string =~ /^\w+:\/\//
554
+ url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
555
+ else
556
+ url_string == request_uri
557
+ end
558
+ end
559
+
560
+ private
561
+ def convert_options_to_javascript!(html_options, url = '')
562
+ confirm, popup = html_options.delete("confirm"), html_options.delete("popup")
563
+
564
+ method, href = html_options.delete("method"), html_options['href']
565
+
566
+ html_options["onclick"] = case
567
+ when popup && method
568
+ raise ActionView::ActionViewError, "You can't use :popup and :method in the same link"
569
+ when confirm && popup
570
+ "if (#{confirm_javascript_function(confirm)}) { #{popup_javascript_function(popup)} };return false;"
571
+ when confirm && method
572
+ "if (#{confirm_javascript_function(confirm)}) { #{method_javascript_function(method, url, href)} };return false;"
573
+ when confirm
574
+ "return #{confirm_javascript_function(confirm)};"
575
+ when method
576
+ "#{method_javascript_function(method, url, href)}return false;"
577
+ when popup
578
+ "#{popup_javascript_function(popup)}return false;"
579
+ else
580
+ html_options["onclick"]
581
+ end
582
+ end
583
+
584
+ def confirm_javascript_function(confirm)
585
+ "confirm('#{escape_javascript(confirm)}')"
586
+ end
587
+
588
+ def popup_javascript_function(popup)
589
+ popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);"
590
+ end
591
+
592
+ def method_javascript_function(method, url = '', href = nil)
593
+ action = (href && url.size > 0) ? "'#{url}'" : 'this.href'
594
+ submit_function =
595
+ "var f = document.createElement('form'); f.style.display = 'none'; " +
596
+ "this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{action};"
597
+
598
+ unless method == :post
599
+ submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "
600
+ submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);"
601
+ end
602
+
603
+ if protect_against_forgery?
604
+ submit_function << "var s = document.createElement('input'); s.setAttribute('type', 'hidden'); "
605
+ submit_function << "s.setAttribute('name', '#{request_forgery_protection_token}'); s.setAttribute('value', '#{escape_javascript form_authenticity_token}'); f.appendChild(s);"
606
+ end
607
+ submit_function << "f.submit();"
608
+ end
609
+
610
+ # Processes the +html_options+ hash, converting the boolean
611
+ # attributes from true/false form into the form required by
612
+ # HTML/XHTML. (An attribute is considered to be boolean if
613
+ # its name is listed in the given +bool_attrs+ array.)
614
+ #
615
+ # More specifically, for each boolean attribute in +html_options+
616
+ # given as:
617
+ #
618
+ # "attr" => bool_value
619
+ #
620
+ # if the associated +bool_value+ evaluates to true, it is
621
+ # replaced with the attribute's name; otherwise the attribute is
622
+ # removed from the +html_options+ hash. (See the XHTML 1.0 spec,
623
+ # section 4.5 "Attribute Minimization" for more:
624
+ # http://www.w3.org/TR/xhtml1/#h-4.5)
625
+ #
626
+ # Returns the updated +html_options+ hash, which is also modified
627
+ # in place.
628
+ #
629
+ # Example:
630
+ #
631
+ # convert_boolean_attributes!( html_options,
632
+ # %w( checked disabled readonly ) )
633
+ def convert_boolean_attributes!(html_options, bool_attrs)
634
+ bool_attrs.each { |x| html_options[x] = x if html_options.delete(x) }
635
+ html_options
636
+ end
637
+ end
638
+ end
639
+ end