[deploy] Gracefully handle missing linked users from comments (#7941)
This commit is contained in:
parent
5fcc44677e
commit
c59db25387
5 changed files with 47 additions and 17 deletions
|
|
@ -91,7 +91,7 @@ class UserDecorator < ApplicationDecorator
|
|||
},
|
||||
]
|
||||
colors |= WHITE_TEXT_COLORS
|
||||
colors[id % 10]
|
||||
colors[(id || rand(100)) % 10]
|
||||
end
|
||||
|
||||
# returns true if the user has been suspended and has no content
|
||||
|
|
|
|||
|
|
@ -1,4 +1,16 @@
|
|||
module ApplicationHelper
|
||||
# rubocop:disable Performance/OpenStruct
|
||||
DELETED_USER = OpenStruct.new(
|
||||
id: nil,
|
||||
darker_color: HexComparer.new(bg: "#19063A", text: "#dce9f3").brightness,
|
||||
username: "[deleted user]",
|
||||
name: "[Deleted User]",
|
||||
summary: nil,
|
||||
twitter_username: nil,
|
||||
github_username: nil,
|
||||
)
|
||||
# rubocop:enable Performance/OpenStruct
|
||||
|
||||
def user_logged_in_status
|
||||
user_signed_in? ? "logged-in" : "logged-out"
|
||||
end
|
||||
|
|
@ -113,6 +125,8 @@ module ApplicationHelper
|
|||
end
|
||||
|
||||
def follow_button(followable, style = "full")
|
||||
return if followable == DELETED_USER
|
||||
|
||||
tag :button, # Yikes
|
||||
class: "cta follow-action-button",
|
||||
data: {
|
||||
|
|
@ -127,6 +141,8 @@ module ApplicationHelper
|
|||
end
|
||||
|
||||
def user_colors(user)
|
||||
return { bg: "#19063A", text: "#dce9f3" } if user == DELETED_USER
|
||||
|
||||
user.decorate.enriched_colors
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -13,18 +13,26 @@ class UserTag < LiquidTagBase
|
|||
ActionController::Base.new.render_to_string(
|
||||
partial: PARTIAL,
|
||||
locals: {
|
||||
user: @user.decorate,
|
||||
user: user_object_for_partial(@user),
|
||||
follow_button: @follow_button,
|
||||
user_colors: @user_colors
|
||||
user_colors: @user_colors,
|
||||
user_path: path_to_profile(@user)
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
def parse_username_to_user(user)
|
||||
user = User.find_by(username: user)
|
||||
raise StandardError, "Invalid username" if user.nil?
|
||||
private
|
||||
|
||||
user
|
||||
def parse_username_to_user(user)
|
||||
User.find_by(username: user) || DELETED_USER
|
||||
end
|
||||
|
||||
def path_to_profile(user)
|
||||
user == DELETED_USER ? nil : user.path
|
||||
end
|
||||
|
||||
def user_object_for_partial(user)
|
||||
user == DELETED_USER ? user : user.decorate
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -6,17 +6,21 @@
|
|||
border-color: <%= user_colors[:bg].casecmp("#ffffff").zero? ? user_colors[:text] : user_colors[:bg] %> !important;
|
||||
}
|
||||
</style>
|
||||
<a href="<%= user.path %>" class="ltag__user__link profile-image-link">
|
||||
<% if user_path.present? %>
|
||||
<a href="<%= user_path %>" class="ltag__user__link profile-image-link">
|
||||
<div class="ltag__user__pic">
|
||||
<img src="<%= ProfileImage.new(user).get(width: 150) %>" alt="<%= "#{user.username} image" %>" />
|
||||
</div>
|
||||
</a>
|
||||
<% else %>
|
||||
<div class="ltag__user__pic">
|
||||
<img src="<%= ProfileImage.new(user).get(width: 150) %>" alt="<%= "#{user.username} image" %>" />
|
||||
</div>
|
||||
</a>
|
||||
<% end %>
|
||||
<div class="ltag__user__content">
|
||||
<h2><a href="<%= user.path %>" class="ltag__user__link"><%= user.name %></a><%= follow_button %></h2>
|
||||
<h2><%= link_to_if user_path.present?, user.name, user_path, class: "ltag__user__link" %><%= follow_button %></h2>
|
||||
<div class="ltag__user__summary">
|
||||
<a href="<%= user.path %>" class="ltag__user__link">
|
||||
<%= user.summary %>
|
||||
</a>
|
||||
<%= link_to_if user_path.present?, user.summary, user_path, class: "ltag__user__link" %>
|
||||
</div>
|
||||
<p class="ltag__user__social">
|
||||
<% if !user.twitter_username.blank? %>
|
||||
|
|
|
|||
|
|
@ -21,9 +21,11 @@ RSpec.describe UserTag, type: :liquid_tag do
|
|||
end
|
||||
end
|
||||
|
||||
it "rejects invalid id_code" do
|
||||
expect do
|
||||
generate_user_tag("this should fail")
|
||||
end.to raise_error(StandardError)
|
||||
context "when given an invalid username" do
|
||||
it "renders a missing username and name", aggregate_failures: true do
|
||||
liquid = generate_user_tag("nonexistent user")
|
||||
expect(liquid.render).to include("[deleted user]")
|
||||
expect(liquid.render).to include("[Deleted User]")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue