Project

General

Profile

Patch #44660 » 03_journal_details.patch

Jimmy Westberg, 2026-10-27 20:02

View differences:

app/helpers/issues_helper.rb
# 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
lib/redmine/journal_details.rb
# 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
lib/redmine/plugin.rb
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):
test/integration/lib/redmine/view_hooks_test.rb
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')
......
def teardown
Redmine::Hook.clear_listeners
Redmine::Tabs.clear
Redmine::JournalDetails.clear
end
def html_errors
......
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
test/unit/lib/redmine/journal_details_test.rb
# 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
(7-7/11)