From d1992e45269faad8522209daf1be2eb9d5db78fb Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 6 Sep 2024 13:21:47 +0200 Subject: [PATCH] Use alchemy_display_name for page actor names If you have a user class that does not implement name (eg. `Spree::User` or a generic `User` class) it would return `nil` for Page actor methods (creator, updater, locker). We already use `alchemy_display_name` for the current logged in user displayed in the admin frame. Using this for the Page actor methods to be consistant and allow custom user classes to present a user by it. (cherry picked from commit 00dd34e3c2b852cfca883b8701a008825c2ad525) --- app/models/alchemy/page.rb | 6 +++--- spec/dummy/app/models/dummy_user.rb | 6 +++++- spec/models/alchemy/page_spec.rb | 8 ++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 005559b56f..7112031c5e 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -506,7 +506,7 @@ def public_until # does not respond to +#name+ it returns +'unknown'+ # def creator_name - creator.try(:name) || Alchemy.t("unknown") + creator.try(:alchemy_display_name) || Alchemy.t("unknown") end # Returns the name of the last updater of this page. @@ -515,7 +515,7 @@ def creator_name # does not respond to +#name+ it returns +'unknown'+ # def updater_name - updater.try(:name) || Alchemy.t("unknown") + updater.try(:alchemy_display_name) || Alchemy.t("unknown") end # Returns the name of the user currently editing this page. @@ -524,7 +524,7 @@ def updater_name # does not respond to +#name+ it returns +'unknown'+ # def locker_name - locker.try(:name) || Alchemy.t("unknown") + locker.try(:alchemy_display_name) || Alchemy.t("unknown") end # Key hint translations by page layout, rather than the default name. diff --git a/spec/dummy/app/models/dummy_user.rb b/spec/dummy/app/models/dummy_user.rb index 1f8f9f9811..39375def4b 100644 --- a/spec/dummy/app/models/dummy_user.rb +++ b/spec/dummy/app/models/dummy_user.rb @@ -13,13 +13,17 @@ def self.admins end def alchemy_roles - @alchemy_roles || %w(admin) + @alchemy_roles || %w[admin] end def name @name || email end + def alchemy_display_name + name + end + def human_roles_string alchemy_roles.map(&:humanize) end diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index 713bb2f8cc..8042721505 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -1800,7 +1800,7 @@ module Alchemy end end - context "with user class having a name accessor" do + context "with user class having a alchemy_display_name accessor" do let(:user) { build(:alchemy_dummy_user, name: "Paul Page") } describe "#creator_name" do @@ -1828,7 +1828,7 @@ module Alchemy end end - context "with user class returning nil for name" do + context "with user class returning nil for alchemy_display_name" do let(:user) { Alchemy.user_class.new } describe "#creator_name" do @@ -1856,11 +1856,11 @@ module Alchemy end end - context "with user class not responding to name" do + context "with user class not responding to alchemy_display_name" do let(:user) { Alchemy.user_class.new } before do - expect(user).to receive(:respond_to?).with(:name) { false } + expect(user).to receive(:respond_to?).with(:alchemy_display_name) { false } end describe "#creator_name" do