retrospring/app/helpers/user_helper.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

2022-06-24 16:59:09 +02:00
# frozen_string_literal: true
2014-11-02 17:57:37 +01:00
module UserHelper
2014-11-11 07:09:36 +01:00
# Decides what user name to show.
# @param context_user [User] the user whose the profile preferences should be applied
# @param author_identifier [nil, String] the author identifier of the question (questions only)
2014-11-11 07:09:36 +01:00
# @return [String] The user name
def user_screen_name(user, context_user: nil, author_identifier: nil, url: true, link_only: false)
return unmask(user, context_user, author_identifier) if should_unmask?(author_identifier)
return anonymous_name(context_user) if anonymous?(user, author_identifier.present?)
if url
2022-06-24 16:59:09 +02:00
return show_user_profile_path(user.screen_name) if link_only
2022-06-24 16:59:09 +02:00
return profile_link(user)
end
2022-06-24 16:59:09 +02:00
user.profile.safe_name.strip
2014-11-11 07:09:36 +01:00
end
2022-06-21 16:27:07 +02:00
def moderation_view?
current_user&.mod? && session[:moderation_view] == true
end
2022-06-21 16:27:07 +02:00
private
2022-06-24 16:59:09 +02:00
def profile_link(user)
link_to(user.profile.safe_name, show_user_profile_path(user.screen_name), class: ("user--banned" if user.banned?).to_s)
end
def should_unmask?(author_identifier)
moderation_view? && author_identifier.present?
end
def unmask(user, context_user, author_identifier)
return profile_link(user) if user.present?
content_tag(:abbr, anonymous_name(context_user), title: author_identifier)
2022-06-24 16:59:09 +02:00
end
2022-06-21 16:27:07 +02:00
def anonymous_name(context_user)
2022-06-25 10:37:56 +02:00
sanitize(context_user&.profile&.anon_display_name.presence || APP_CONFIG["anonymous_name"], tags: [])
2022-06-21 16:27:07 +02:00
end
2022-06-24 16:59:09 +02:00
def anonymous?(user, anonymous)
user.nil? || anonymous
end
2014-11-02 17:57:37 +01:00
end