Skip to content

Commit 6b018e3

Browse files
author
David Heinemeier Hansson
committed
Added :mouseover short-cut to AssetTagHelper#image_tag for doing easy image swaps (closes #6893) [joost]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 37adea6 commit 6b018e3

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

actionpack/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*SVN*
22

3+
* Added :mouseover short-cut to AssetTagHelper#image_tag for doing easy image swaps #6893 [joost]
4+
35
* Fixed handling of non-domain hosts #9479 [purp]
46

57
* Fix syntax error in documentation example for cycle method. Closes #8735 [foca]

actionpack/lib/action_view/helpers/asset_tag_helper.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,16 @@ def image_path(source)
335335
#
336336
# ==== Options
337337
# You can add HTML attributes using the +options+. The +options+ supports
338-
# two additional keys for convienence and conformance:
338+
# three additional keys for convienence and conformance:
339339
#
340340
# * <tt>:alt</tt> - If no alt text is given, the file name part of the
341341
# +source+ is used (capitalized and without the extension)
342342
# * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
343343
# width="30" and height="45". <tt>:size</tt> will be ignored if the
344344
# value is not in the correct format.
345+
# * <tt>:mouseover</tt> - Set an alternate image to be used when the onmouseover
346+
# event is fired, and sets the original image to be replaced onmouseout.
347+
# This can be used to implement an easy image toggle that fires on onmouseover.
345348
#
346349
# ==== Examples
347350
# image_tag("icon") # =>
@@ -356,15 +359,23 @@ def image_path(source)
356359
# <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
357360
# image_tag("/icons/icon.gif", :class => "menu_icon") # =>
358361
# <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
362+
# image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # =>
363+
# <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
364+
# image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # =>
365+
# <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
359366
def image_tag(source, options = {})
360367
options.symbolize_keys!
361368

362369
options[:src] = path_to_image(source)
363370
options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize
364371

365-
if options[:size]
366-
options[:width], options[:height] = options[:size].split("x") if options[:size] =~ %r{^\d+x\d+$}
367-
options.delete(:size)
372+
if size = options.delete(:size)
373+
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
374+
end
375+
376+
if mouseover = options.delete(:mouseover)
377+
options[:onmouseover] = "this.src='#{image_path(mouseover)}'"
378+
options[:onmouseout] = "this.src='#{image_path(options[:src])}'"
368379
end
369380

370381
tag("img", options)

actionpack/test/template/asset_tag_helper_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ def teardown
135135
%(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
136136
%(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
137137
%(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
138-
%(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />)
138+
%(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />),
139+
%(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />),
140+
%(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />),
141+
%(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />)
139142
}
140143

141144

0 commit comments

Comments
 (0)