From 6b018e3d08c52417034cd648952668192327a9b7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 7 Nov 2007 15:37:06 +0000 Subject: [PATCH] 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 --- actionpack/CHANGELOG | 2 ++ .../action_view/helpers/asset_tag_helper.rb | 19 +++++++++++++++---- .../test/template/asset_tag_helper_test.rb | 5 ++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 0538943f3b665..6d20c608c7297 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added :mouseover short-cut to AssetTagHelper#image_tag for doing easy image swaps #6893 [joost] + * Fixed handling of non-domain hosts #9479 [purp] * Fix syntax error in documentation example for cycle method. Closes #8735 [foca] diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index f0229980b01fc..47bbe2f995a42 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -335,13 +335,16 @@ def image_path(source) # # ==== Options # You can add HTML attributes using the +options+. The +options+ supports - # two additional keys for convienence and conformance: + # three additional keys for convienence and conformance: # # * :alt - If no alt text is given, the file name part of the # +source+ is used (capitalized and without the extension) # * :size - Supplied as "{Width}x{Height}", so "30x45" becomes # width="30" and height="45". :size will be ignored if the # value is not in the correct format. + # * :mouseover - Set an alternate image to be used when the onmouseover + # event is fired, and sets the original image to be replaced onmouseout. + # This can be used to implement an easy image toggle that fires on onmouseover. # # ==== Examples # image_tag("icon") # => @@ -356,15 +359,23 @@ def image_path(source) # Icon # image_tag("/icons/icon.gif", :class => "menu_icon") # => # Icon + # image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # => + # Mouse + # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # => + # Mouse def image_tag(source, options = {}) options.symbolize_keys! options[:src] = path_to_image(source) options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize - if options[:size] - options[:width], options[:height] = options[:size].split("x") if options[:size] =~ %r{^\d+x\d+$} - options.delete(:size) + if size = options.delete(:size) + options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} + end + + if mouseover = options.delete(:mouseover) + options[:onmouseover] = "this.src='#{image_path(mouseover)}'" + options[:onmouseout] = "this.src='#{image_path(options[:src])}'" end tag("img", options) diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 275b987f0e5bc..a27a597e98c38 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -135,7 +135,10 @@ def teardown %(image_tag("error.png", "size" => "45")) => %(Error), %(image_tag("error.png", "size" => "45 x 70")) => %(Error), %(image_tag("error.png", "size" => "x")) => %(Error), - %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails) + %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), + %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), + %(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(Mouse), + %(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(Mouse) }