Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 458 lines (437 sloc) 23.803 kb
91b0b65 @joshk split the javascript and stylesheet tag helpers into separate files as a...
joshk authored
1 require 'action_view/helpers/asset_tag_helpers/javascript_tag_helpers'
2 require 'action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers'
0ff1c59 @joshk reorganised the the common asset helpers module into a class and have it...
joshk authored
3 require 'action_view/helpers/asset_tag_helpers/asset_paths'
0ac79d0 @spohlenz Include TagHelper within AssetTagHelper
spohlenz authored
4 require 'action_view/helpers/tag_helper'
2a5fc27 @dhh Added AssetTagHelper that provides methods for linking a HTML page toget...
dhh authored
5
6 module ActionView
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored
7 # = Action View Asset Tag Helpers
6c914d9 @technoweenie Add more extensive documentation to the AssetTagHelper. Closes #6452 [B...
technoweenie authored
8 module Helpers #:nodoc:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
9 # This module provides methods for generating HTML that links views to assets such
7359597 @josh Wrap AssetTagHelper's computed public path cache in a threadsafe store
josh authored
10 # as images, javascripts, stylesheets, and feeds. These methods do not verify
39e1ac6 @lifo Merge docrails
lifo authored
11 # the assets exist before linking to them:
12 #
13 # image_tag("rails.png")
b273767 @eadz simplify alt tag generation for images
eadz authored
14 # # => <img alt="Rails" src="/images/rails.png?1230601161" />
39e1ac6 @lifo Merge docrails
lifo authored
15 # stylesheet_link_tag("application")
16 # # => <link href="/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
17 #
18 # === Using asset hosts
39e1ac6 @lifo Merge docrails
lifo authored
19 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
20 # By default, Rails links to these assets on the current host in the public
39e1ac6 @lifo Merge docrails
lifo authored
21 # folder, but you can direct Rails to link to assets from a dedicated asset
22 # server by setting ActionController::Base.asset_host in the application
23 # configuration, typically in <tt>config/environments/production.rb</tt>.
24 # For example, you'd define <tt>assets.example.com</tt> to be your asset
25 # host this way:
6c914d9 @technoweenie Add more extensive documentation to the AssetTagHelper. Closes #6452 [B...
technoweenie authored
26 #
c9260c5 @jeremy Add request protocol to asset host if not given. Prefer setting asset ho...
jeremy authored
27 # ActionController::Base.asset_host = "assets.example.com"
39e1ac6 @lifo Merge docrails
lifo authored
28 #
29 # Helpers take that into account:
30 #
90c1207 @jeremy Work around the two connection per host browser limit: use asset%d.myapp...
jeremy authored
31 # image_tag("rails.png")
39e1ac6 @lifo Merge docrails
lifo authored
32 # # => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />
d0bc724 @dhh Docfix (closes #11281) [jlindley]
dhh authored
33 # stylesheet_link_tag("application")
39e1ac6 @lifo Merge docrails
lifo authored
34 # # => <link href="http://assets.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
90c1207 @jeremy Work around the two connection per host browser limit: use asset%d.myapp...
jeremy authored
35 #
39e1ac6 @lifo Merge docrails
lifo authored
36 # Browsers typically open at most two simultaneous connections to a single
37 # host, which means your assets often have to wait for other assets to finish
38 # downloading. You can alleviate this by using a <tt>%d</tt> wildcard in the
39 # +asset_host+. For example, "assets%d.example.com". If that wildcard is
40 # present Rails distributes asset requests among the corresponding four hosts
41 # "assets0.example.com", ..., "assets3.example.com". With this trick browsers
42 # will open eight simultaneous connections rather than two.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
43 #
44 # image_tag("rails.png")
39e1ac6 @lifo Merge docrails
lifo authored
45 # # => <img alt="Rails" src="http://assets0.example.com/images/rails.png?1230601161" />
d0bc724 @dhh Docfix (closes #11281) [jlindley]
dhh authored
46 # stylesheet_link_tag("application")
39e1ac6 @lifo Merge docrails
lifo authored
47 # # => <link href="http://assets2.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
90c1207 @jeremy Work around the two connection per host browser limit: use asset%d.myapp...
jeremy authored
48 #
39e1ac6 @lifo Merge docrails
lifo authored
49 # To do this, you can either setup four actual hosts, or you can use wildcard
50 # DNS to CNAME the wildcard to a single asset host. You can read more about
51 # setting up your DNS CNAME records from your ISP.
90c1207 @jeremy Work around the two connection per host browser limit: use asset%d.myapp...
jeremy authored
52 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
53 # Note: This is purely a browser performance optimization and is not meant
90c1207 @jeremy Work around the two connection per host browser limit: use asset%d.myapp...
jeremy authored
54 # for server load balancing. See http://www.die.net/musings/page_load_time/
55 # for background.
5ae821a @dhh Add documentation about asset timestamps (just for koz)
dhh authored
56 #
39e1ac6 @lifo Merge docrails
lifo authored
57 # Alternatively, you can exert more control over the asset host by setting
58 # +asset_host+ to a proc like this:
56e6462 @dhh Added option to pass proc to ActionController::Base.asset_host for maxim...
dhh authored
59 #
39e1ac6 @lifo Merge docrails
lifo authored
60 # ActionController::Base.asset_host = Proc.new { |source|
53d3baf ruby 1.9.2 and other ruby implementations may not return the same hash v...
Evan Petrie authored
61 # "http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com"
39e1ac6 @lifo Merge docrails
lifo authored
62 # }
56e6462 @dhh Added option to pass proc to ActionController::Base.asset_host for maxim...
dhh authored
63 # image_tag("rails.png")
aa4fe9f @marius fix documentation typo
marius authored
64 # # => <img alt="Rails" src="http://assets1.example.com/images/rails.png?1230601161" />
d0bc724 @dhh Docfix (closes #11281) [jlindley]
dhh authored
65 # stylesheet_link_tag("application")
aa4fe9f @marius fix documentation typo
marius authored
66 # # => <link href="http://assets2.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
39e1ac6 @lifo Merge docrails
lifo authored
67 #
68 # The example above generates "http://assets1.example.com" and
cbdb4aa @retr0h Updated ActionController::Base.asset_host example
retr0h authored
69 # "http://assets2.example.com". This option is useful for example if
39e1ac6 @lifo Merge docrails
lifo authored
70 # you need fewer/more than four hosts, custom host names, etc.
56e6462 @dhh Added option to pass proc to ActionController::Base.asset_host for maxim...
dhh authored
71 #
39e1ac6 @lifo Merge docrails
lifo authored
72 # As you see the proc takes a +source+ parameter. That's a string with the
73 # absolute path of the asset with any extensions and timestamps in place,
74 # for example "/images/rails.png?1230601161".
56e6462 @dhh Added option to pass proc to ActionController::Base.asset_host for maxim...
dhh authored
75 #
76 # ActionController::Base.asset_host = Proc.new { |source|
77 # if source.starts_with?('/images')
78 # "http://images.example.com"
79 # else
80 # "http://assets.example.com"
81 # end
82 # }
83 # image_tag("rails.png")
39e1ac6 @lifo Merge docrails
lifo authored
84 # # => <img alt="Rails" src="http://images.example.com/images/rails.png?1230601161" />
d0bc724 @dhh Docfix (closes #11281) [jlindley]
dhh authored
85 # stylesheet_link_tag("application")
39e1ac6 @lifo Merge docrails
lifo authored
86 # # => <link href="http://assets.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
56e6462 @dhh Added option to pass proc to ActionController::Base.asset_host for maxim...
dhh authored
87 #
39e1ac6 @lifo Merge docrails
lifo authored
88 # Alternatively you may ask for a second parameter +request+. That one is
89 # particularly useful for serving assets from an SSL-protected page. The
90 # example proc below disables asset hosting for HTTPS connections, while
91 # still sending assets for plain HTTP requests from asset hosts. If you don't
92 # have SSL certificates for each of the asset hosts this technique allows you
93 # to avoid warnings in the client about mixed media.
a1b0349 @jeremy The asset_host block takes the controller request as an optional second ...
jeremy authored
94 #
95 # ActionController::Base.asset_host = Proc.new { |source, request|
96 # if request.ssl?
97 # "#{request.protocol}#{request.host_with_port}"
98 # else
99 # "#{request.protocol}assets.example.com"
100 # end
101 # }
102 #
39e1ac6 @lifo Merge docrails
lifo authored
103 # You can also implement a custom asset host object that responds to +call+
104 # and takes either one or two parameters just like the proc.
229f959 @dhh Added the option to declare an asset_host as an object that responds to ...
dhh authored
105 #
106 # config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
107 # "http://asset%d.example.com", "https://asset1.example.com"
108 # )
109 #
201e898 @wycats Updated asset_template_path to asset_path and have it also support a Str...
wycats authored
110 # === Customizing the asset path
5ae821a @dhh Add documentation about asset timestamps (just for koz)
dhh authored
111 #
39e1ac6 @lifo Merge docrails
lifo authored
112 # By default, Rails appends asset's timestamps to all asset paths. This allows
113 # you to set a cache-expiration date for the asset far into the future, but
114 # still be able to instantly invalidate it by simply updating the file (and
115 # hence updating the timestamp, which then updates the URL as the timestamp
116 # is part of that, which in turn busts the cache).
117 #
118 # It's the responsibility of the web server you use to set the far-future
119 # expiration date on cache assets that you need to take advantage of this
120 # feature. Here's an example for Apache:
121 #
122 # # Asset Expiration
123 # ExpiresActive On
124 # <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
125 # ExpiresDefault "access plus 1 year"
126 # </FilesMatch>
127 #
128 # Also note that in order for this to work, all your application servers must
129 # return the same timestamps. This means that they must have their clocks
130 # synchronized. If one of them drifts out of sync, you'll see different
131 # timestamps at random and the cache won't work. In that case the browser
132 # will request the same assets over and over again even thought they didn't
133 # change. You can use something like Live HTTP Headers for Firefox to verify
134 # that the cache is indeed working.
49bc6a2 @craigw Allow customisation of asset path generation using a proc.
craigw authored
135 #
136 # This strategy works well enough for most server setups and requires the
137 # least configuration, but if you deploy several application servers at
138 # different times - say to handle a temporary spike in load - then the
139 # asset time stamps will be out of sync. In a setup like this you may want
140 # to set the way that asset paths are generated yourself.
141 #
142 # Altering the asset paths that Rails generates can be done in two ways.
143 # The easiest is to define the RAILS_ASSET_ID environment variable. The
144 # contents of this variable will always be used in preference to
145 # calculated timestamps. A more complex but flexible way is to set
201e898 @wycats Updated asset_template_path to asset_path and have it also support a Str...
wycats authored
146 # <tt>ActionController::Base.config.asset_path</tt> to a proc
49bc6a2 @craigw Allow customisation of asset path generation using a proc.
craigw authored
147 # that takes the unmodified asset path and returns the path needed for
148 # your asset caching to work. Typically you'd do something like this in
149 # <tt>config/environments/production.rb</tt>:
150 #
151 # # Normally you'd calculate RELEASE_NUMBER at startup.
152 # RELEASE_NUMBER = 12345
e695c40 @andreacampi Fix example that became outdated after a code change.
andreacampi authored
153 # config.action_controller.asset_path = proc { |asset_path|
49bc6a2 @craigw Allow customisation of asset path generation using a proc.
craigw authored
154 # "/release-#{RELEASE_NUMBER}#{asset_path}"
155 # }
156 #
71d18ce @oem Changed a few instances of of words in the API docs written in British E...
oem authored
157 # This example would cause the following behavior on all servers no
49bc6a2 @craigw Allow customisation of asset path generation using a proc.
craigw authored
158 # matter when they were deployed:
159 #
160 # image_tag("rails.png")
161 # # => <img alt="Rails" src="/release-12345/images/rails.png" />
162 # stylesheet_link_tag("application")
163 # # => <link href="/release-12345/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
164 #
201e898 @wycats Updated asset_template_path to asset_path and have it also support a Str...
wycats authored
165 # Changing the asset_path does require that your web servers have
49bc6a2 @craigw Allow customisation of asset path generation using a proc.
craigw authored
166 # knowledge of the asset template paths that you rewrite to so it's not
167 # suitable for out-of-the-box use. To use the example given above you
168 # could use something like this in your Apache VirtualHost configuration:
169 #
170 # <LocationMatch "^/release-\d+/(images|javascripts|stylesheets)/.*$">
171 # # Some browsers still send conditional-GET requests if there's a
172 # # Last-Modified header or an ETag header even if they haven't
173 # # reached the expiry date sent in the Expires header.
174 # Header unset Last-Modified
175 # Header unset ETag
176 # FileETag None
177 #
178 # # Assets requested using a cache-busting filename should be served
179 # # only once and then cached for a really long time. The HTTP/1.1
180 # # spec frowns on hugely-long expiration times though and suggests
181 # # that assets which never expire be served with an expiration date
182 # # 1 year from access.
183 # ExpiresActive On
184 # ExpiresDefault "access plus 1 year"
185 # </LocationMatch>
186 #
187 # # We use cached-busting location names with the far-future expires
188 # # headers to ensure that if a file does change it can force a new
189 # # request. The actual asset filenames are still the same though so we
190 # # need to rewrite the location from the cache-busting location to the
191 # # real asset location so that we can serve it.
192 # RewriteEngine On
193 # RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
2a5fc27 @dhh Added AssetTagHelper that provides methods for linking a HTML page toget...
dhh authored
194 module AssetTagHelper
0ac79d0 @spohlenz Include TagHelper within AssetTagHelper
spohlenz authored
195 include TagHelper
91b0b65 @joshk split the javascript and stylesheet tag helpers into separate files as a...
joshk authored
196 include JavascriptTagHelpers
197 include StylesheetTagHelpers
6a609db @joshk incorporated most of the feedback from José
joshk authored
198 # Returns a link tag that browsers and news readers can use to auto-detect
199 # an RSS or ATOM feed. The +type+ can either be <tt>:rss</tt> (default) or
200 # <tt>:atom</tt>. Control the link options in url_for format using the
201 # +url_options+. You can modify the LINK tag itself in +tag_options+.
202 #
203 # ==== Options
204 # * <tt>:rel</tt> - Specify the relation of this link, defaults to "alternate"
205 # * <tt>:type</tt> - Override the auto-generated mime type
206 # * <tt>:title</tt> - Specify the title of the link, defaults to the +type+
207 #
208 # ==== Examples
209 # auto_discovery_link_tag # =>
210 # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
211 # auto_discovery_link_tag(:atom) # =>
212 # <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
213 # auto_discovery_link_tag(:rss, {:action => "feed"}) # =>
214 # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
215 # auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # =>
216 # <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
217 # auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"}) # =>
218 # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
219 # auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"}) # =>
220 # <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
221 def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
222 tag(
223 "link",
224 "rel" => tag_options[:rel] || "alternate",
225 "type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s,
226 "title" => tag_options[:title] || type.to_s.upcase,
227 "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options
228 )
229 end
230
231 # <%= favicon_link_tag %>
232 #
233 # generates
234 #
9d77956 @marcbowes * A `/` is required if you want to explicitly skip the assets directory....
marcbowes authored
235 # <link href="/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
6a609db @joshk incorporated most of the feedback from José
joshk authored
236 #
237 # You may specify a different file in the first argument:
238 #
9d77956 @marcbowes * A `/` is required if you want to explicitly skip the assets directory....
marcbowes authored
239 # <%= favicon_link_tag '/myicon.ico' %>
6a609db @joshk incorporated most of the feedback from José
joshk authored
240 #
241 # That's passed to +path_to_image+ as is, so it gives
242 #
9d77956 @marcbowes * A `/` is required if you want to explicitly skip the assets directory....
marcbowes authored
243 # <link href="/myicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
6a609db @joshk incorporated most of the feedback from José
joshk authored
244 #
245 # The helper accepts an additional options hash where you can override "rel" and "type".
246 #
247 # For example, Mobile Safari looks for a different LINK tag, pointing to an image that
248 # will be used if you add the page to the home screen of an iPod Touch, iPhone, or iPad.
249 # The following call would generate such a tag:
250 #
251 # <%= favicon_link_tag 'mb-icon.png', :rel => 'apple-touch-icon', :type => 'image/png' %>
252 #
253 def favicon_link_tag(source='/favicon.ico', options={})
254 tag('link', {
255 :rel => 'shortcut icon',
256 :type => 'image/vnd.microsoft.icon',
257 :href => path_to_image(source)
258 }.merge(options.symbolize_keys))
259 end
260
261 # Computes the path to an image asset in the public images directory.
262 # Full paths from the document root will be passed through.
263 # Used internally by +image_tag+ to build the image path:
264 #
265 # image_path("edit") # => "/images/edit"
266 # image_path("edit.png") # => "/images/edit.png"
267 # image_path("icons/edit.png") # => "/images/icons/edit.png"
268 # image_path("/icons/edit.png") # => "/icons/edit.png"
783007a @tilsammans Replace example hostname with "example.com".
tilsammans authored
269 # image_path("http://www.example.com/img/edit.png") # => "http://www.example.com/img/edit.png"
6a609db @joshk incorporated most of the feedback from José
joshk authored
270 #
271 # If you have images as application resources this method may conflict with their named routes.
272 # The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
273 # plugin authors are encouraged to do so.
274 def image_path(source)
4b79029 @wycats Start moving some logic from being embedded in AV into the Rails Sprocke...
wycats authored
275 asset_paths.compute_public_path(source, 'images')
6a609db @joshk incorporated most of the feedback from José
joshk authored
276 end
277 alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
278
279 # Computes the path to a video asset in the public videos directory.
280 # Full paths from the document root will be passed through.
281 # Used internally by +video_tag+ to build the video path.
282 #
283 # ==== Examples
284 # video_path("hd") # => /videos/hd
285 # video_path("hd.avi") # => /videos/hd.avi
286 # video_path("trailers/hd.avi") # => /videos/trailers/hd.avi
287 # video_path("/trailers/hd.avi") # => /trailers/hd.avi
783007a @tilsammans Replace example hostname with "example.com".
tilsammans authored
288 # video_path("http://www.example.com/vid/hd.avi") # => http://www.example.com/vid/hd.avi
6a609db @joshk incorporated most of the feedback from José
joshk authored
289 def video_path(source)
4b79029 @wycats Start moving some logic from being embedded in AV into the Rails Sprocke...
wycats authored
290 asset_paths.compute_public_path(source, 'videos')
6a609db @joshk incorporated most of the feedback from José
joshk authored
291 end
292 alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
293
294 # Computes the path to an audio asset in the public audios directory.
295 # Full paths from the document root will be passed through.
296 # Used internally by +audio_tag+ to build the audio path.
297 #
298 # ==== Examples
299 # audio_path("horse") # => /audios/horse
300 # audio_path("horse.wav") # => /audios/horse.wav
301 # audio_path("sounds/horse.wav") # => /audios/sounds/horse.wav
302 # audio_path("/sounds/horse.wav") # => /sounds/horse.wav
783007a @tilsammans Replace example hostname with "example.com".
tilsammans authored
303 # audio_path("http://www.example.com/sounds/horse.wav") # => http://www.example.com/sounds/horse.wav
6a609db @joshk incorporated most of the feedback from José
joshk authored
304 def audio_path(source)
4b79029 @wycats Start moving some logic from being embedded in AV into the Rails Sprocke...
wycats authored
305 asset_paths.compute_public_path(source, 'audios')
6a609db @joshk incorporated most of the feedback from José
joshk authored
306 end
307 alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route
308
0d7d3a6 @spastorino Add font_path helper method
spastorino authored
309 # Computes the path to a font asset in the public fonts directory.
310 # Full paths from the document root will be passed through.
311 #
312 # ==== Examples
313 # font_path("font") # => /fonts/font
314 # font_path("font.ttf") # => /fonts/font.ttf
315 # font_path("dir/font.ttf") # => /fonts/dir/font.ttf
316 # font_path("/dir/font.ttf") # => /dir/font.ttf
317 # font_path("http://www.example.com/dir/font.ttf") # => http://www.example.com/dir/font.ttf
318 def font_path(source)
319 asset_paths.compute_public_path(source, 'fonts')
320 end
321 alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
322
6a609db @joshk incorporated most of the feedback from José
joshk authored
323 # Returns an html image tag for the +source+. The +source+ can be a full
324 # path or a file that exists in your public images directory.
325 #
326 # ==== Options
327 # You can add HTML attributes using the +options+. The +options+ supports
328 # three additional keys for convenience and conformance:
329 #
330 # * <tt>:alt</tt> - If no alt text is given, the file name part of the
331 # +source+ is used (capitalized and without the extension)
332 # * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
333 # width="30" and height="45". <tt>:size</tt> will be ignored if the
334 # value is not in the correct format.
335 # * <tt>:mouseover</tt> - Set an alternate image to be used when the onmouseover
336 # event is fired, and sets the original image to be replaced onmouseout.
337 # This can be used to implement an easy image toggle that fires on onmouseover.
338 #
339 # ==== Examples
340 # image_tag("icon") # =>
341 # <img src="/images/icon" alt="Icon" />
342 # image_tag("icon.png") # =>
343 # <img src="/images/icon.png" alt="Icon" />
344 # image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # =>
345 # <img src="/images/icon.png" width="16" height="10" alt="Edit Entry" />
346 # image_tag("/icons/icon.gif", :size => "16x16") # =>
347 # <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
348 # image_tag("/icons/icon.gif", :height => '32', :width => '32') # =>
349 # <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
350 # image_tag("/icons/icon.gif", :class => "menu_icon") # =>
351 # <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
352 # image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # =>
353 # <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
354 # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # =>
355 # <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
356 def image_tag(source, options = {})
357 options.symbolize_keys!
358
359 src = options[:src] = path_to_image(source)
360
361 unless src =~ /^cid:/
a0960ec @fxn the generated ALT attribute for images no longer contains the digest, if...
fxn authored
362 options[:alt] = options.fetch(:alt){ image_alt(src) }
6a609db @joshk incorporated most of the feedback from José
joshk authored
363 end
364
365 if size = options.delete(:size)
366 options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
367 end
368
369 if mouseover = options.delete(:mouseover)
370 options[:onmouseover] = "this.src='#{path_to_image(mouseover)}'"
371 options[:onmouseout] = "this.src='#{src}'"
372 end
373
374 tag("img", options)
375 end
376
a0960ec @fxn the generated ALT attribute for images no longer contains the digest, if...
fxn authored
377 def image_alt(src)
9378d7e @fxn sub suffices here
fxn authored
378 File.basename(src, '.*').sub(/-[[:xdigit:]]{32}\z/, '').capitalize
a0960ec @fxn the generated ALT attribute for images no longer contains the digest, if...
fxn authored
379 end
380
6a609db @joshk incorporated most of the feedback from José
joshk authored
381 # Returns an html video tag for the +sources+. If +sources+ is a string,
382 # a single video tag will be returned. If +sources+ is an array, a video
383 # tag with nested source tags for each source will be returned. The
384 # +sources+ can be full paths or files that exists in your public videos
385 # directory.
386 #
387 # ==== Options
388 # You can add HTML attributes using the +options+. The +options+ supports
389 # two additional keys for convenience and conformance:
390 #
391 # * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
392 # before the video loads. The path is calculated like the +src+ of +image_tag+.
393 # * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
394 # width="30" and height="45". <tt>:size</tt> will be ignored if the
395 # value is not in the correct format.
396 #
397 # ==== Examples
398 # video_tag("trailer") # =>
399 # <video src="/videos/trailer" />
400 # video_tag("trailer.ogg") # =>
401 # <video src="/videos/trailer.ogg" />
402 # video_tag("trailer.ogg", :controls => true, :autobuffer => true) # =>
403 # <video autobuffer="autobuffer" controls="controls" src="/videos/trailer.ogg" />
404 # video_tag("trailer.m4v", :size => "16x10", :poster => "screenshot.png") # =>
405 # <video src="/videos/trailer.m4v" width="16" height="10" poster="/images/screenshot.png" />
406 # video_tag("/trailers/hd.avi", :size => "16x16") # =>
407 # <video src="/trailers/hd.avi" width="16" height="16" />
408 # video_tag("/trailers/hd.avi", :height => '32', :width => '32') # =>
409 # <video height="32" src="/trailers/hd.avi" width="32" />
410 # video_tag(["trailer.ogg", "trailer.flv"]) # =>
411 # <video><source src="trailer.ogg" /><source src="trailer.ogg" /><source src="trailer.flv" /></video>
412 # video_tag(["trailer.ogg", "trailer.flv"] :size => "160x120") # =>
413 # <video height="120" width="160"><source src="trailer.ogg" /><source src="trailer.flv" /></video>
414 def video_tag(sources, options = {})
415 options.symbolize_keys!
416
417 options[:poster] = path_to_image(options[:poster]) if options[:poster]
418
419 if size = options.delete(:size)
420 options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
421 end
422
423 if sources.is_a?(Array)
424 content_tag("video", options) do
425 sources.map { |source| tag("source", :src => source) }.join.html_safe
426 end
427 else
428 options[:src] = path_to_video(sources)
429 tag("video", options)
430 end
431 end
432
433 # Returns an html audio tag for the +source+.
434 # The +source+ can be full path or file that exists in
435 # your public audios directory.
436 #
437 # ==== Examples
438 # audio_tag("sound") # =>
439 # <audio src="/audios/sound" />
440 # audio_tag("sound.wav") # =>
441 # <audio src="/audios/sound.wav" />
442 # audio_tag("sound.wav", :autoplay => true, :controls => true) # =>
443 # <audio autoplay="autoplay" controls="controls" src="/audios/sound.wav" />
444 def audio_tag(source, options = {})
445 options.symbolize_keys!
446 options[:src] = path_to_audio(source)
447 tag("audio", options)
448 end
449
0ff1c59 @joshk reorganised the the common asset helpers module into a class and have it...
joshk authored
450 private
451
452 def asset_paths
22fcef9 @josevalim Actually add an abstract class, so it is easier to get rid of old asset ...
josevalim authored
453 @asset_paths ||= AssetTagHelper::AssetPaths.new(config, controller)
0ff1c59 @joshk reorganised the the common asset helpers module into a class and have it...
joshk authored
454 end
2a5fc27 @dhh Added AssetTagHelper that provides methods for linking a HTML page toget...
dhh authored
455 end
456 end
59c83af @akaspick allow absolute paths for the asset caches
akaspick authored
457 end
Something went wrong with that request. Please try again.