]> source.dussan.org Git - redmine.git/commitdiff
Makes image_tag pick the image from the current theme if it exists.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 28 Apr 2012 09:47:09 +0000 (09:47 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 28 Apr 2012 09:47:09 +0000 (09:47 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9560 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
lib/redmine/themes.rb
test/unit/helpers/application_helper_test.rb

index f54a85a02cdf2dcf61d482b8bfcdbf0df8f616cc..50ecd1c27ccb249ba91dcdc7a3eca6bab55035fe 100644 (file)
@@ -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
index 5e5261927fc9f8d522854edad0cde9b1397b99d5..c8b71109068aa3b2af834d0930259cae4314db0f 100644 (file)
@@ -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
 
index 9bf25e605a3268f1ed3a8087006cecd3382a31ab..6dea2cf57f74fdbcdade9fbc47d22b8b3293481f 100644 (file)
@@ -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