Skip to content

Commit

Permalink
Use alchemy_display_name for page actor names
Browse files Browse the repository at this point in the history
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 00dd34e)
  • Loading branch information
tvdeyen committed Sep 6, 2024
1 parent 9dff0e7 commit d1992e4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion spec/dummy/app/models/dummy_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d1992e4

Please sign in to comment.