diff --git a/app/helpers/groups_helper.rb b/app/helpers/groups_helper.rb index c5bed08..3439905 100644 --- a/app/helpers/groups_helper.rb +++ b/app/helpers/groups_helper.rb @@ -23,7 +23,7 @@ module GroupsHelper tabs << {:name => 'general', :partial => 'groups/general', :label => :label_general} tabs << {:name => 'users', :partial => 'groups/users', :label => :label_user_plural} if group.givable? tabs << {:name => 'memberships', :partial => 'groups/memberships', :label => :label_project_plural} - tabs + Redmine::Tabs.add_to(:group, tabs, :group => group) end def render_principals_for_new_group_users(group, limit=100) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index fbfa7fb..5bd91bf 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -758,7 +758,7 @@ module IssuesHelper "'#{issue_path(@issue, :tab => 'changesets')}')" } end - tabs + Redmine::Tabs.add_to(:issue_history, tabs, :issue => @issue) end def issue_history_default_tab diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 9ec6a76..ffd4578 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -42,6 +42,8 @@ module ProjectsHelper {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :label_time_tracking} ] + # A plugin tab without a :permission needs the one of the project tab + tabs = Redmine::Tabs.add_to(:project_settings, tabs, :project => @project).each {|tab| tab[:action] ||= :edit_project} tabs. select {|tab| User.current.allowed_to?(tab[:action], @project)}. select {|tab| tab[:module].nil? || @project.module_enabled?(tab[:module])} diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index 89cbc62..6d55deb 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -39,6 +39,7 @@ module SettingsHelper {:name => 'repositories', :partial => 'settings/repositories', :label => :label_repository_plural} ] + Redmine::Tabs.add_to(:administration_settings, tabs) end def render_settings_error(errors) diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 0da236e..89b2d7e 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -98,6 +98,6 @@ module UsersHelper if Group.givable.any? tabs.insert 1, {:name => 'groups', :partial => 'users/groups', :label => :label_group_plural} end - tabs + Redmine::Tabs.add_to(:user, tabs, :user => @user) end end diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index 847517b..be6db14 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -335,6 +335,40 @@ module Redmine Redmine::MenuManager.map(menu).delete(item) end + # Adds a tab to a tabbed settings page (see Redmine::Tabs for the pages and options). + # settings_tab :user, :a_tab, :partial => 'a_plugin/user_tab', :after => :general + def settings_tab(page, name, options={}) + Redmine::Tabs.register(page, name, options) + end + + # Adds a tab to the project settings. Without a :permission the tab needs + # :edit_project, as the project tab does. + # project_settings_tab :a_tab, :label => :label_a_tab, :partial => 'a_plugin/settings', + # :permission => :manage_a_plugin, :after => :members + def project_settings_tab(name, options={}) + settings_tab(:project_settings, name, options) + end + + # Adds a tab to the administration settings. + def administration_settings_tab(name, options={}) + settings_tab(:administration_settings, name, options) + end + + # Adds a tab to the user edit page. + def user_settings_tab(name, options={}) + settings_tab(:user, name, options) + end + + # Adds a tab to the group edit page. + def group_settings_tab(name, options={}) + settings_tab(:group, name, options) + end + + # Adds a tab to the history of an issue. + def issue_history_tab(name, options={}) + settings_tab(:issue_history, name, options) + 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/lib/redmine/tabs.rb b/lib/redmine/tabs.rb new file mode 100644 index 0000000..7010888 --- /dev/null +++ b/lib/redmine/tabs.rb @@ -0,0 +1,113 @@ +# 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 tabs that plugins add to the tabbed settings pages, so that a + # plugin does not have to override the helper that builds the list of tabs. + # + # Pages: + # * :project_settings - project settings (ProjectsHelper#project_settings_tabs) + # * :administration_settings - administration settings (SettingsHelper#administration_settings_tabs) + # * :user - user edit page (UsersHelper#user_settings_tabs) + # * :group - group edit page (GroupsHelper#group_settings_tabs) + # * :issue_history - the tabs of an issue's history (IssuesHelper#issue_history_tabs); + # a tab given :remote => true and an :onclick loads its content as core's tabs do + # + # Options: + # * :partial - the partial that renders the tab (required, except for a :remote tab) + # * :label - the label of the tab, a symbol for l() or a string (default: :label_) + # * :permission - project settings only: the permission the user needs in the project + # * :module - project settings only: the project module that must be enabled + # * :if - a Proc that receives the context of the page (a Hash with :project, :user or + # :group) and returns false to hide the tab + # * :url - extra url params for the tab + # * :remote, :onclick - issue history only: a tab whose content is loaded on click + # * :after, :before - the name of the tab to place the tab after or before (appended + # to the list by default) + # + # Example, in a plugin's init.rb: + # Redmine::Plugin.register :a_plugin do + # project_settings_tab :a_tab, :label => :label_a_tab, + # :partial => 'a_plugin/settings', :permission => :manage_a_plugin, :after => :members + # end + module Tabs + PAGES = [:project_settings, :administration_settings, :user, :group, :issue_history].freeze + + @registered = Hash.new {|h, k| h[k] = []} + + class << self + def register(page, name, options={}) + page = page.to_sym + raise ArgumentError, "Unknown tabs page '#{page}'" unless PAGES.include?(page) + if options[:partial].blank? && !options[:remote] + raise ArgumentError, "Missing :partial for tab '#{name}'" + end + if options[:if] && !options[:if].respond_to?(:call) + raise ArgumentError, "Invalid option :if for tab '#{name}'" + end + + @registered[page].reject! {|tab| tab[:name] == name.to_s} + @registered[page] << options.merge(:name => name.to_s) + end + + # Returns the tabs registered for +page+ + def registered(page) + @registered[page.to_sym].dup + end + + # Removes the registered tabs (all of them or those of +page+) + def clear(page=nil) + page ? @registered.delete(page.to_sym) : @registered.clear + end + + # Returns +tabs+ (core's tabs, an Array of Hash) with the tabs registered for + # +page+ added at their place. +context+ is given to the :if option. + def add_to(page, tabs, context={}) + tabs = tabs.dup + registered(page).each do |options| + next if options[:if] && !options[:if].call(context) + + tab = {:name => options[:name], :label => options[:label] || :"label_#{options[:name]}"} + tab[:partial] = options[:partial] if options[:partial] + tab[:action] = options[:permission] if options[:permission] + tab[:module] = options[:module] if options[:module] + tab[:url] = options[:url] if options[:url] + tab[:remote] = options[:remote] if options.key?(:remote) + tab[:onclick] = options[:onclick] if options[:onclick] + insert(tabs, tab, options) + end + tabs + end + + private + + def insert(tabs, tab, options) + anchor = options[:after] || options[:before] + index = anchor && tabs.index {|t| t[:name].to_s == anchor.to_s} + if index.nil? + tabs << tab + elsif options[:after] + tabs.insert(index + 1, tab) + else + tabs.insert(index, tab) + end + end + end + end +end diff --git a/test/integration/lib/redmine/view_hooks_test.rb b/test/integration/lib/redmine/view_hooks_test.rb index 41e1c4e..56c35a9 100644 --- a/test/integration/lib/redmine/view_hooks_test.rb +++ b/test/integration/lib/redmine/view_hooks_test.rb @@ -19,9 +19,9 @@ require_relative '../../../test_helper' -# The hooks that let plugins add to pages without overriding core views or -# helpers: each one is called on its page, and what a plugin adds through them -# leaves the page's HTML as valid as it was. +# The hooks and registries that let plugins add to pages without overriding +# core views or helpers: each one is called on its page, and what a plugin adds +# through them leaves the page's HTML as valid as it was. class ViewHooksTest < Redmine::IntegrationTest # The pages and the hooks each one must call PAGES = { @@ -49,6 +49,7 @@ class ViewHooksTest < Redmine::IntegrationTest def teardown Redmine::Hook.clear_listeners + Redmine::Tabs.clear end def html_errors @@ -85,4 +86,24 @@ class ViewHooksTest < Redmine::IntegrationTest assert_select 'h2>a+img.gravatar' assert_select 'h2 + span.hook-view_layouts_base_title_after' end + + def test_registered_tabs_should_be_shown + # The label is a locale key, as for core's tabs + Redmine::Tabs.register :project_settings, :sample, :partial => 'common/preview', :label => :label_preview, :after => :members + Redmine::Tabs.register :user, :sample, :partial => 'common/preview', :label => :label_preview + Redmine::Tabs.register :group, :sample, :partial => 'common/preview', :label => :label_preview + Redmine::Tabs.register :administration_settings, :sample, :partial => 'common/preview', :label => :label_preview + Redmine::Tabs.register :issue_history, :sample, :remote => true, :label => :label_preview, :onclick => 'return false;' + ['/projects/ecookbook/settings', '/users/2/edit', '/groups/10/edit', '/settings', '/issues/1'].each do |path| + get path + assert_response :success, path + assert_select 'div.tabs a#tab-sample', {:text => I18n.t(:label_preview)}, "The tab is not on #{path}" + end + end + + def test_project_settings_tab_should_require_its_permission + Redmine::Tabs.register :project_settings, :sample, :partial => 'common/preview', :permission => :manage_sample + get '/projects/ecookbook/settings' + assert_select 'a#tab-sample', 0 + end end diff --git a/test/unit/lib/redmine/tabs_test.rb b/test/unit/lib/redmine/tabs_test.rb new file mode 100644 index 0000000..04398ca --- /dev/null +++ b/test/unit/lib/redmine/tabs_test.rb @@ -0,0 +1,103 @@ +# 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::TabsTest < ActiveSupport::TestCase + def setup + Redmine::Tabs.clear + end + + def teardown + Redmine::Tabs.clear + end + + def core_tabs + [{:name => 'general', :partial => 'x/general'}, {:name => 'members', :partial => 'x/members'}] + end + + def test_add_to_should_append_a_registered_tab + Redmine::Tabs.register :project_settings, :foo, :partial => 'foo/settings' + tabs = Redmine::Tabs.add_to(:project_settings, core_tabs) + assert_equal %w(general members foo), tabs.pluck(:name) + assert_equal({:name => 'foo', :partial => 'foo/settings', :label => :label_foo}, tabs.last) + end + + def test_add_to_should_not_change_the_given_tabs + Redmine::Tabs.register :project_settings, :foo, :partial => 'foo/settings' + tabs = core_tabs + Redmine::Tabs.add_to(:project_settings, tabs) + assert_equal 2, tabs.size + end + + def test_add_to_should_place_a_tab_after_or_before_another + Redmine::Tabs.register :user, :after_general, :partial => 'a', :after => :general + Redmine::Tabs.register :user, :before_general, :partial => 'b', :before => 'general' + tabs = Redmine::Tabs.add_to(:user, core_tabs) + assert_equal %w(before_general general after_general members), tabs.pluck(:name) + end + + def test_add_to_should_append_a_tab_placed_after_an_unknown_tab + Redmine::Tabs.register :group, :foo, :partial => 'foo', :after => :unknown + assert_equal %w(general members foo), Redmine::Tabs.add_to(:group, core_tabs).pluck(:name) + end + + def test_add_to_should_map_permission_and_module_to_the_keys_core_filters_on + Redmine::Tabs.register :project_settings, :foo, :partial => 'foo', + :permission => :manage_foo, :module => :foo_module, :label => 'Foo' + tab = Redmine::Tabs.add_to(:project_settings, []).first + assert_equal :manage_foo, tab[:action] + assert_equal :foo_module, tab[:module] + assert_equal 'Foo', tab[:label] + end + + def test_add_to_should_skip_a_tab_whose_condition_is_false + Redmine::Tabs.register :group, :foo, :partial => 'foo', :if => Proc.new {|context| context[:group].nil?} + assert_equal 1, Redmine::Tabs.add_to(:group, []).size + assert_equal 0, Redmine::Tabs.add_to(:group, [], :group => Group.new).size + end + + def test_register_should_replace_a_tab_with_the_same_name + Redmine::Tabs.register :user, :foo, :partial => 'one' + Redmine::Tabs.register :user, :foo, :partial => 'two' + assert_equal ['two'], Redmine::Tabs.registered(:user).pluck(:partial) + end + + def test_register_should_refuse_an_unknown_page_or_a_missing_partial + assert_raise(ArgumentError) {Redmine::Tabs.register :unknown_page, :foo, :partial => 'foo'} + assert_raise(ArgumentError) {Redmine::Tabs.register :user, :foo} + assert_raise(ArgumentError) {Redmine::Tabs.register :user, :foo, :partial => 'foo', :if => true} + end + + def test_plugin_should_register_tabs + Redmine::Plugin.register :tabs_test_plugin do + directory Rails.root.join('test/fixtures/plugins/foo_plugin').to_s + project_settings_tab :foo, :partial => 'foo/project' + administration_settings_tab :foo, :partial => 'foo/admin' + user_settings_tab :foo, :partial => 'foo/user' + group_settings_tab :foo, :partial => 'foo/group' + end + assert_equal ['foo/project'], Redmine::Tabs.registered(:project_settings).pluck(:partial) + assert_equal ['foo/admin'], Redmine::Tabs.registered(:administration_settings).pluck(:partial) + assert_equal ['foo/user'], Redmine::Tabs.registered(:user).pluck(:partial) + assert_equal ['foo/group'], Redmine::Tabs.registered(:group).pluck(:partial) + ensure + Redmine::Plugin.unregister(:tabs_test_plugin) + end +end