* Let moderators see name changes for deleted users on the user name change requests index and show pages. Before they could see name changes for deleted users on user profiles, but not on the user name changes index. * Let members see previous names on profile pages. Before they could see previous names on the user name changes index, but not on profile pages (ref: #4382).
31 lines
759 B
Ruby
31 lines
759 B
Ruby
class UserNameChangeRequest < ApplicationRecord
|
|
belongs_to :user
|
|
belongs_to :approver, class_name: "User", optional: true
|
|
|
|
validate :not_limited, on: :create
|
|
validates :desired_name, user_name: true, confirmation: true, on: :create
|
|
validates_presence_of :original_name, :desired_name
|
|
|
|
after_create :update_name!
|
|
|
|
def self.visible(user)
|
|
if user.is_moderator?
|
|
all
|
|
elsif user.is_member?
|
|
where(user: User.undeleted)
|
|
else
|
|
none
|
|
end
|
|
end
|
|
|
|
def update_name!
|
|
user.update!(name: desired_name)
|
|
end
|
|
|
|
def not_limited
|
|
if UserNameChangeRequest.unscoped.where(user: user).where("created_at >= ?", 1.week.ago).exists?
|
|
errors[:base] << "You can only submit one name change request per week"
|
|
end
|
|
end
|
|
end
|