Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

add User.select_for_name to fetch columns required for formatting user name

Ivan Kuchin6612299bbe80
diff --git a/app/models/custom_actions/actions/assigned_to.rb b/app/models/custom_actions/actions/assigned_to.rb
index c9de63d3267..cb07040dab1 100644
--- a/app/models/custom_actions/actions/assigned_to.rb
+++ b/app/models/custom_actions/actions/assigned_to.rb
@@ -36,7 +36,8 @@ class CustomActions::Actions::AssignedTo < CustomActions::Actions::Base
def available_principles
principal_class
.not_locked
- .select(:id, :firstname, :lastname, :type)
+ .select(:id, :type)
+ .select_for_name
.ordered_by_name
.map { |u| [u.id, u.name] }
end
diff --git a/app/models/custom_actions/actions/notify.rb b/app/models/custom_actions/actions/notify.rb
index 96c20d56552..ab750301e65 100644
--- a/app/models/custom_actions/actions/notify.rb
+++ b/app/models/custom_actions/actions/notify.rb
@@ -61,7 +61,8 @@ class CustomActions::Actions::Notify < CustomActions::Actions::Base
def principals
Principal
.not_locked
- .select(:id, :firstname, :lastname, :type)
+ .select(:id, :type)
+ .select_for_name
.ordered_by_name
end
end
diff --git a/app/models/custom_actions/actions/responsible.rb b/app/models/custom_actions/actions/responsible.rb
index 78c3efc83b1..521135ec3f9 100644
--- a/app/models/custom_actions/actions/responsible.rb
+++ b/app/models/custom_actions/actions/responsible.rb
@@ -32,7 +32,8 @@ class CustomActions::Actions::Responsible < CustomActions::Actions::Base
def available_principles
User
.not_locked
- .select(:id, :firstname, :lastname, :type)
+ .select(:id, :type)
+ .select_for_name
.ordered_by_name
.map { |u| [u.id, u.name] }
end
diff --git a/app/models/group.rb b/app/models/group.rb
index a1cb415fd93..b15997ae0c3 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -58,6 +58,11 @@ class Group < Principal
scopes :visible
+ # Columns required for formatting the group's name.
+ def self.columns_for_name(_formatter = nil)
+ [:lastname]
+ end
+
def to_s
lastname
end
diff --git a/app/models/placeholder_user.rb b/app/models/placeholder_user.rb
index 57d06ec554a..fc5c6858566 100644
--- a/app/models/placeholder_user.rb
+++ b/app/models/placeholder_user.rb
@@ -37,6 +37,11 @@ class PlaceholderUser < Principal
scopes :visible
+ # Columns required for formatting the placeholder user's name.
+ def self.columns_for_name(_formatter = nil)
+ [:lastname]
+ end
+
def to_s
lastname
end
diff --git a/app/models/principal.rb b/app/models/principal.rb
index 1afda9a0e7a..5e6b4bb3bed 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -122,6 +122,18 @@ class Principal < ApplicationRecord
before_create :set_default_empty_values
+ # Columns required for formatting the principal's name.
+ def self.columns_for_name(formatter = nil)
+ raise NotImplementedError, "Redefine in subclass" unless self == Principal
+
+ [User, Group, PlaceholderUser].map { _1.columns_for_name(formatter) }.inject(:|)
+ end
+
+ # Select columns for formatting the user's name.
+ def self.select_for_name(formatter = nil)
+ select(*columns_for_name(formatter))
+ end
+
def name(_formatter = nil)
to_s
end
diff --git a/app/models/user.rb b/app/models/user.rb
index d7cd19c38e7..ca6f7e0e192 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -262,8 +262,21 @@ class User < Principal
end
end
+ # Columns required for formatting the user's name.
+ def self.columns_for_name(formatter = nil)
+ case formatter || Setting.user_format
+ when :firstname
+ [:firstname]
+ when :username
+ [:login]
+ else
+ %i[firstname lastname]
+ end
+ end
+
# Formats the user's name.
def name(formatter = nil)
+ # Don't forget to check columns_for_name
case formatter || Setting.user_format
when :firstname_lastname then "#{firstname} #{lastname}"
diff --git a/spec/models/custom_action_spec.rb b/spec/models/custom_action_spec.rb
index 6c59e0bc777..defcd10d4e7 100644
--- a/spec/models/custom_action_spec.rb
+++ b/spec/models/custom_action_spec.rb
@@ -121,6 +121,19 @@ RSpec.describe CustomAction do
expect(stubbed_instance.all_actions.map { |a| [a.key, a.values] })
.to include([:assigned_to, [1]], [:status, []])
end
+
+ it "fetches data for login format of user name" do
+ create(:user)
+
+ allow(Setting).to receive(:user_format).and_return(:username)
+
+ expect do
+ stubbed_instance
+ .all_actions
+ .select { _1.type == :associated_property }
+ .each(&:allowed_values)
+ end.not_to raise_error
+ end
end
describe ".conditions" do
diff --git a/spec/models/custom_actions/actions/assigned_to_spec.rb b/spec/models/custom_actions/actions/assigned_to_spec.rb
index 126cc00d0c0..84241de8114 100644
--- a/spec/models/custom_actions/actions/assigned_to_spec.rb
+++ b/spec/models/custom_actions/actions/assigned_to_spec.rb
@@ -35,7 +35,7 @@ RSpec.describe CustomActions::Actions::AssignedTo do
users = [build_stubbed(:user),
build_stubbed(:group)]
allow(Principal)
- .to receive_message_chain(:not_locked, :select, :ordered_by_name)
+ .to receive_message_chain(:not_locked, :select, :select_for_name, :ordered_by_name)
.and_return(users)
[{ value: nil, label: "-" },
diff --git a/spec/models/custom_actions/actions/notify_spec.rb b/spec/models/custom_actions/actions/notify_spec.rb
index 47afed140f0..7d60b7959ef 100644
--- a/spec/models/custom_actions/actions/notify_spec.rb
+++ b/spec/models/custom_actions/actions/notify_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe CustomActions::Actions::Notify do
build_stubbed(:group)]
allow(Principal)
- .to receive_message_chain(:not_locked, :select, :ordered_by_name)
+ .to receive_message_chain(:not_locked, :select, :select_for_name, :ordered_by_name)
.and_return(users)
[{ value: nil, label: "-" },
@@ -65,7 +65,7 @@ RSpec.describe CustomActions::Actions::Notify do
build_stubbed(:user)]
allow(Principal)
- .to receive_message_chain(:not_locked, :select, :ordered_by_name, :where)
+ .to receive_message_chain(:not_locked, :select, :select_for_name, :ordered_by_name, :where)
.and_return(principals)
instance.values = principals.map(&:id)
diff --git a/spec/models/custom_actions/actions/responsible_spec.rb b/spec/models/custom_actions/actions/responsible_spec.rb
index a13062b1e7e..2fc7e6d16eb 100644
--- a/spec/models/custom_actions/actions/responsible_spec.rb
+++ b/spec/models/custom_actions/actions/responsible_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe CustomActions::Actions::Responsible do
build_stubbed(:group)]
allow(User)
- .to receive_message_chain(:not_locked, :select, :ordered_by_name)
+ .to receive_message_chain(:not_locked, :select, :select_for_name, :ordered_by_name)
.and_return(principals)
[{ value: nil, label: "-" },
diff --git a/spec/models/principal_spec.rb b/spec/models/principal_spec.rb
index 76bab20a405..b9bb5bb8eda 100644
--- a/spec/models/principal_spec.rb
+++ b/spec/models/principal_spec.rb
@@ -128,4 +128,56 @@ RSpec.describe Principal do
.to contain_exactly(active_project_member, global_member)
end
end
+
+ describe "#name" do
+ shared_let(:user_id) { create(:user, firstname: "John", lastname: "Smith", login: "john_smith").id }
+ shared_let(:group_id) { create(:group, name: "Folk").id }
+ shared_let(:placeholder_user_id) { create(:placeholder_user, name: "Wannabejohn").id }
+
+ shared_examples "name formatting" do
+ context "for lastname_coma_firstname formatter", with_settings: { user_format: :lastname_coma_firstname } do
+ it "returns formatted user name" do
+ expect(user.name).to eq "Smith, John"
+ end
+
+ it "returns formatted group name" do
+ expect(group.name).to eq "Folk"
+ end
+
+ it "returns formatted placeholder user name" do
+ expect(placeholder_user.name).to eq "Wannabejohn"
+ end
+ end
+
+ context "for username formatter", with_settings: { user_format: :username } do
+ it "returns formatted user name" do
+ expect(user.name).to eq "john_smith"
+ end
+
+ it "returns formatted group name" do
+ expect(group.name).to eq "Folk"
+ end
+
+ it "returns formatted placeholder user name" do
+ expect(placeholder_user.name).to eq "Wannabejohn"
+ end
+ end
+ end
+
+ context "when fetched individually" do
+ let(:user) { User.select_for_name.find(user_id) }
+ let(:group) { Group.select_for_name.find(group_id) }
+ let(:placeholder_user) { PlaceholderUser.select_for_name.find(placeholder_user_id) }
+
+ include_examples "name formatting"
+ end
+
+ context "when fetched as Principal" do
+ let(:user) { described_class.select(:type).select_for_name.find(user_id) }
+ let(:group) { described_class.select(:type).select_for_name.find(group_id) }
+ let(:placeholder_user) { described_class.select(:type).select_for_name.find(placeholder_user_id) }
+
+ include_examples "name formatting"
+ end
+ end
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index ab9055a9595..57aab5660bd 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -303,7 +303,7 @@ RSpec.describe User do
end
describe "#name" do
- let(:user) do
+ before do
create(:user,
firstname: "John",
lastname: "Smith",
@@ -311,28 +311,56 @@ RSpec.describe User do
end
- context "for firstname_lastname", with_settings: { user_format: :firstname_lastname } do
- it { expect(user.name).to eq "#{user.firstname} #{user.lastname}" }
- end
+ context "when formatting according to setting" do
+ subject { user.name }
- context "for firstname", with_settings: { user_format: :firstname } do
- it { expect(user.name).to eq user.firstname }
- end
+ let(:user) { described_class.select_for_name.last }
- context "for lastname_firstname", with_settings: { user_format: :lastname_firstname } do
- it { expect(user.name).to eq "#{user.lastname} #{user.firstname}" }
- end
+ context "for firstname_lastname", with_settings: { user_format: :firstname_lastname } do
+ it { is_expected.to eq "John Smith" }
+ end
- context "for lastname_n_firstname", with_settings: { user_format: :lastname_n_firstname } do
- it { expect(user.name).to eq "#{user.lastname}#{user.firstname}" }
- end
+ context "for firstname", with_settings: { user_format: :firstname } do
+ it { is_expected.to eq "John" }
+ end
+
+ context "for lastname_firstname", with_settings: { user_format: :lastname_firstname } do
+ it { is_expected.to eq "Smith John" }
+ end
+
+ context "for lastname_n_firstname", with_settings: { user_format: :lastname_n_firstname } do
+ it { is_expected.to eq "SmithJohn" }
+ end
- context "for lastname_coma_firstname", with_settings: { user_format: :lastname_coma_firstname } do
- it { expect(user.name).to eq "#{user.lastname}, #{user.firstname}" }
+ context "for lastname_coma_firstname", with_settings: { user_format: :lastname_coma_firstname } do
+ it { is_expected.to eq "Smith, John" }
+ end
+
+ context "for username", with_settings: { user_format: :username } do
+ it { is_expected.to eq "username" }
+ end
+
+ context "for nil", with_settings: { user_format: nil } do
+ it { is_expected.to eq "John Smith" }
+ end
end
- context "for username", with_settings: { user_format: :username } do
- it { expect(user.name).to eq user.login }
+ context "when specifying format explicitly" do
+ subject { user.name(formatter) }
+
+ let(:user) { described_class.select_for_name(formatter).last }
+
+ context "for lastname_coma_firstname" do
+ let(:formatter) { :lastname_coma_firstname }
+
+ it { is_expected.to eq "Smith, John" }
+ end
+
+ context "for username", with_settings: { user_format: :username } do
+ let(:formatter) { :username }
+
+ it { is_expected.to eq "username" }
+ end
end
end