diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 5bd91bf..2269d02 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -522,6 +522,10 @@ module IssuesHelper # Returns the textual representation of a single journal detail def show_detail(detail, no_html=false, options={}) + if (renderer = Redmine::JournalDetails.renderer_for(detail)) + return renderer.call(detail, self, no_html, options) + end + multiple = false show_diff = false no_details = false diff --git a/lib/redmine/journal_details.rb b/lib/redmine/journal_details.rb new file mode 100644 index 0000000..590990f --- /dev/null +++ b/lib/redmine/journal_details.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006- Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + # Registry of the renderers for journal details that plugins write with a + # property of their own (journal_details.property), so that a plugin does not + # have to override IssuesHelper#show_detail. + # + # A renderer responds to +call+ with the detail, the view and the options of + # show_detail, and returns the line as HTML or, when +no_html+ is true, as text + # (the text is also used in emails): + # + # class APlugin::JournalDetail + # def self.call(detail, view, no_html, options) + # label = no_html ? 'Sample' : view.content_tag('strong', 'Sample') + # view.l(:text_journal_set_to, :label => label, :value => detail.value) + # end + # end + # + # Redmine::Plugin.register :a_plugin do + # journal_detail :property => 'a_plugin', :renderer => APlugin::JournalDetail + # end + # + # A renderer registered with a :prop_key is used for that key only and comes + # before one registered for the whole property. + module JournalDetails + @renderers = {} + + class << self + def register(property, renderer, prop_key: nil) + unless renderer.respond_to?(:call) + raise ArgumentError, "Invalid renderer for journal detail property '#{property}'" + end + + @renderers[[property.to_s, prop_key&.to_s]] = renderer + end + + # Returns the renderer for +detail+, or nil when none is registered + def renderer_for(detail) + property = detail.property.to_s + @renderers[[property, detail.prop_key.to_s]] || @renderers[[property, nil]] + end + + def clear + @renderers.clear + end + end + end +end diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index be6db14..83e3cb9 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -369,6 +369,14 @@ module Redmine settings_tab(:issue_history, name, options) end + # Registers how the journal details of a +property+ of the plugin (optionally only + # those with a given +prop_key+) are rendered in the issue history, as text and in + # emails (see Redmine::JournalDetails). + # journal_detail :property => 'a_plugin', :renderer => APlugin::JournalDetail + def journal_detail(property:, renderer:, prop_key: nil) + Redmine::JournalDetails.register(property, renderer, prop_key: prop_key) + end + # Defines a permission called +name+ for the given +actions+. # # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): diff --git a/test/integration/lib/redmine/view_hooks_test.rb b/test/integration/lib/redmine/view_hooks_test.rb index 56c35a9..c2b0b9f 100644 --- a/test/integration/lib/redmine/view_hooks_test.rb +++ b/test/integration/lib/redmine/view_hooks_test.rb @@ -42,6 +42,13 @@ class ViewHooksTest < Redmine::IntegrationTest end end + class DetailRenderer + def self.call(detail, view, no_html, options) + text = "Sample changed to #{detail.value}" + no_html ? text : view.content_tag('span', text, :class => 'sample-detail') + end + end + def setup Redmine::Hook.clear_listeners log_user('admin', 'admin') @@ -50,6 +57,7 @@ class ViewHooksTest < Redmine::IntegrationTest def teardown Redmine::Hook.clear_listeners Redmine::Tabs.clear + Redmine::JournalDetails.clear end def html_errors @@ -106,4 +114,11 @@ class ViewHooksTest < Redmine::IntegrationTest get '/projects/ecookbook/settings' assert_select 'a#tab-sample', 0 end + + def test_registered_journal_detail_should_be_rendered_by_its_renderer + Redmine::JournalDetails.register 'sample_plugin', DetailRenderer + JournalDetail.create!(:journal => Journal.find(1), :property => 'sample_plugin', :prop_key => 'level', :value => 'high') + get '/issues/1' + assert_select 'span.sample-detail', :text => 'Sample changed to high' + end end diff --git a/test/unit/lib/redmine/journal_details_test.rb b/test/unit/lib/redmine/journal_details_test.rb new file mode 100644 index 0000000..ce5c2e9 --- /dev/null +++ b/test/unit/lib/redmine/journal_details_test.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006- Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require_relative '../../../test_helper' + +class Redmine::JournalDetailsTest < ActiveSupport::TestCase + def setup + Redmine::JournalDetails.clear + end + + def teardown + Redmine::JournalDetails.clear + end + + def test_renderer_for_should_return_the_renderer_of_the_property + renderer = Proc.new {|detail, view, no_html, options| 'foo'} + Redmine::JournalDetails.register 'foo_plugin', renderer + assert_equal renderer, Redmine::JournalDetails.renderer_for(JournalDetail.new(:property => 'foo_plugin', :prop_key => 'any')) + assert_nil Redmine::JournalDetails.renderer_for(JournalDetail.new(:property => 'attr', :prop_key => 'subject')) + end + + def test_renderer_for_should_prefer_the_renderer_of_the_prop_key + whole = Proc.new {'whole'} + one = Proc.new {'one'} + Redmine::JournalDetails.register 'foo_plugin', whole + Redmine::JournalDetails.register 'foo_plugin', one, prop_key: 'bar' + assert_equal one, Redmine::JournalDetails.renderer_for(JournalDetail.new(:property => 'foo_plugin', :prop_key => 'bar')) + assert_equal whole, Redmine::JournalDetails.renderer_for(JournalDetail.new(:property => 'foo_plugin', :prop_key => 'baz')) + end + + def test_register_should_refuse_a_renderer_that_does_not_respond_to_call + assert_raise(ArgumentError) {Redmine::JournalDetails.register 'foo_plugin', 'not a renderer'} + end + + def test_plugin_should_register_a_journal_detail_renderer + renderer = Proc.new {'foo'} + Redmine::Plugin.register :journal_details_test_plugin do + directory Rails.root.join('test/fixtures/plugins/foo_plugin').to_s + journal_detail :property => 'foo_plugin', :renderer => renderer + end + assert_equal renderer, Redmine::JournalDetails.renderer_for(JournalDetail.new(:property => 'foo_plugin')) + ensure + Redmine::Plugin.unregister(:journal_details_test_plugin) + end +end