From 03335d014ceb42d8db670606e3960b5fd84cb7c3 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 28 Apr 2012 09:10:46 +0000 Subject: [PATCH] Restores support for :plugin support to stylesheet_link_tag and javascript_include_tag helpers. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9558 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 39 +++++++++++++++++++ .../app/views/example/say_goodbye.html.erb | 2 +- .../app/views/example/say_hello.html.erb | 2 +- lib/redmine/themes.rb | 20 ---------- test/unit/helpers/application_helper_test.rb | 16 ++++++++ 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8310a4972..743bc34e3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1045,6 +1045,45 @@ module ApplicationHelper end end + # Overrides Rails' stylesheet_link_tag with themes and plugins support. + # Examples: + # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults + # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets + # + def stylesheet_link_tag(*sources) + options = sources.last.is_a?(Hash) ? sources.pop : {} + plugin = options.delete(:plugin) + sources = sources.map do |source| + if plugin + "/plugin_assets/#{plugin}/stylesheets/#{source}" + elsif current_theme && current_theme.stylesheets.include?(source) + current_theme.stylesheet_path(source) + else + source + end + end + super sources, options + end + + # Overrides Rails' javascript_include_tag with plugins support + # Examples: + # javascript_include_tag('scripts') # => picks scripts.js from defaults + # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets + # + def javascript_include_tag(*sources) + options = sources.last.is_a?(Hash) ? sources.pop : {} + if plugin = options.delete(:plugin) + sources = sources.map do |source| + if plugin + "/plugin_assets/#{plugin}/javascripts/#{source}" + else + source + end + end + end + super sources, options + end + def content_for(name, content = nil, &block) @has_content ||= {} @has_content[name] = true diff --git a/extra/sample_plugin/app/views/example/say_goodbye.html.erb b/extra/sample_plugin/app/views/example/say_goodbye.html.erb index 978fbd9b2..3307e8770 100644 --- a/extra/sample_plugin/app/views/example/say_goodbye.html.erb +++ b/extra/sample_plugin/app/views/example/say_goodbye.html.erb @@ -3,5 +3,5 @@

<%= l(:text_say_goodbye) %>

<% content_for :header_tags do %> - <%= stylesheet_link_tag "/plugin_assets/sample_plugin/stylesheets/example.css", :media => "screen" %> + <%= stylesheet_link_tag 'example', :plugin => 'sample_plugin', :media => "screen" %> <% end %> diff --git a/extra/sample_plugin/app/views/example/say_hello.html.erb b/extra/sample_plugin/app/views/example/say_hello.html.erb index 9bfa8cc4f..505bd2f70 100644 --- a/extra/sample_plugin/app/views/example/say_hello.html.erb +++ b/extra/sample_plugin/app/views/example/say_hello.html.erb @@ -11,5 +11,5 @@ <% end %> <% content_for :header_tags do %> - <%= stylesheet_link_tag "/plugin_assets/sample_plugin/stylesheets/example.css", :media => "screen" %> + <%= stylesheet_link_tag 'example', :plugin => 'sample_plugin', :media => "screen" %> <% end %> diff --git a/lib/redmine/themes.rb b/lib/redmine/themes.rb index 51b974fd7..5e5261927 100644 --- a/lib/redmine/themes.rb +++ b/lib/redmine/themes.rb @@ -106,26 +106,6 @@ module ApplicationHelper @current_theme end - def stylesheet_path(source) - if current_theme && current_theme.stylesheets.include?(source) - super current_theme.stylesheet_path(source) - else - super - end - end - - def path_to_stylesheet(source) - stylesheet_path source - end - - def stylesheet_link_tag(source, *args) - if current_theme && current_theme.stylesheets.include?(source) - super current_theme.stylesheet_path(source), *args - else - super - end - end - # Returns the header tags for the current theme def heads_for_theme if current_theme && current_theme.javascripts.include?('theme') diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index a7a05f3e0..f09d66646 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -1045,4 +1045,20 @@ RAW User.current = User.find(4) assert_include '', principals_options_for_select(users) end + + def test_stylesheet_link_tag_should_pick_the_default_stylesheet + assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles") + end + + def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet + assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo) + end + + def test_javascript_include_tag_should_pick_the_default_javascript + assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts") + end + + def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript + assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo) + end end -- 2.39.5