From 68f8470d4af60c71d024059b96ad53f2968b1bf2 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 28 Apr 2012 09:47:09 +0000 Subject: [PATCH] Makes image_tag pick the image from the current theme if it exists. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9560 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 6 ++++-- lib/redmine/themes.rb | 16 ++++++++++++++-- test/unit/helpers/application_helper_test.rb | 12 ++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f54a85a02..50ecd1c27 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1065,14 +1065,16 @@ module ApplicationHelper super sources, options end - # Overrides Rails' image_tag with plugins support. + # Overrides Rails' image_tag with themes and plugins support. # Examples: - # image_tag('image.png') # => picks defaults image.png + # image_tag('image.png') # => picks image.png from the current theme or defaults # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets # def image_tag(source, options={}) if plugin = options.delete(:plugin) source = "/plugin_assets/#{plugin}/images/#{source}" + elsif current_theme && current_theme.images.include?(source) + source = current_theme.image_path(source) end super source, options end diff --git a/lib/redmine/themes.rb b/lib/redmine/themes.rb index 5e5261927..c8b711090 100644 --- a/lib/redmine/themes.rb +++ b/lib/redmine/themes.rb @@ -67,6 +67,10 @@ module Redmine @stylesheets ||= assets("stylesheets", "css") end + def images + @images ||= assets("images") + end + def javascripts @javascripts ||= assets("javascripts", "js") end @@ -75,14 +79,22 @@ module Redmine "/themes/#{dir}/stylesheets/#{source}" end + def image_path(source) + "/themes/#{dir}/images/#{source}" + end + def javascript_path(source) "/themes/#{dir}/javascripts/#{source}" end private - def assets(dir, ext) - Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')} + def assets(dir, ext=nil) + if ext + Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')} + else + Dir.glob("#{path}/#{dir}/*").collect {|f| File.basename(f)} + end end end diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 9bf25e605..6dea2cf57 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -1058,6 +1058,18 @@ RAW assert_match 'src="/images/image.png"', image_tag("image.png") end + def test_image_tag_should_pick_the_theme_image_if_it_exists + theme = Redmine::Themes.themes.last + theme.images << 'image.png' + + with_settings :ui_theme => theme.id do + assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png") + assert_match %|src="/images/other.png"|, image_tag("other.png") + end + ensure + theme.images.delete 'image.png' + end + def test_image_tag_sfor_plugin_should_pick_the_plugin_image assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo) end -- 2.39.5