From 1bfe84f6bb2c6d9a864e4de2bb74a31cf0c174f2 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sun, 15 Nov 2020 22:11:46 +0900 Subject: [PATCH 2/2] Add link from group name to group page --- app/helpers/application_helper.rb | 25 ++++++++++++++++-------- app/models/group.rb | 4 ++++ app/views/projects/_members_box.html.erb | 2 +- test/helpers/application_helper_test.rb | 11 +++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index dd4a4f195..3212a2bbe 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -49,17 +49,26 @@ module ApplicationHelper # Displays a link to user's account page if active def link_to_user(user, options={}) - if user.is_a?(User) - name = h(user.name(options[:format])) - if user.active? || (User.current.admin? && user.logged?) - only_path = options[:only_path].nil? ? true : options[:only_path] - link_to name, user_url(user, :only_path => only_path), :class => user.css_classes - else - name + user.is_a?(User) ? link_to_principal(user, options) : h(user.to_s) + end + + # Displays a link to user's account page or group page + def link_to_principal(principal, options={}) + only_path = options[:only_path].nil? ? true : options[:only_path] + case principal + when User + name = h(principal.name(options[:format])) + if principal.active? || (User.current.admin? && principal.logged?) + url = user_url(principal, :only_path => only_path) end + when Group + name = h(principal.to_s) + url = group_url(principal, :only_path => only_path) else - h(user.to_s) + name = h(principal.to_s) end + + url ? link_to(name, url, :class => principal.css_classes) : name end # Displays a link to edit group page if current user is admin diff --git a/app/models/group.rb b/app/models/group.rb index db3a51d0f..6600cc40e 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -50,6 +50,10 @@ class Group < Principal name.to_s end + def css_classes + 'group' + end + def name lastname end diff --git a/app/views/projects/_members_box.html.erb b/app/views/projects/_members_box.html.erb index e915ab910..ff7170e48 100644 --- a/app/views/projects/_members_box.html.erb +++ b/app/views/projects/_members_box.html.erb @@ -2,7 +2,7 @@

<%=l(:label_member_plural)%>

<% @principals_by_role.keys.sort.each do |role| %> -

<%= role %>: <%= @principals_by_role[role].sort.collect{|p| link_to_user p}.join(", ").html_safe %>

+

<%= role %>: <%= @principals_by_role[role].sort.collect{|p| link_to_principal p}.join(", ").html_safe %>

<% end %>
<% end %> diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index ec196771e..b42ec278c 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -1642,6 +1642,17 @@ class ApplicationHelperTest < Redmine::HelperTest end end + def test_link_to_principal_should_link_to_user + user = User.find(2) + assert_equal link_to_user(user), link_to_principal(user) + end + + def test_link_to_principal_should_link_to_group + group = Group.find(10) + result = link_to('A Team', '/groups/10', :class => 'group') + assert_equal result, link_to_principal(group) + end + def test_link_to_group_should_return_only_group_name_for_non_admin_users User.current = nil group = Group.find(10) -- 2.26.2