Refactor how model visibility works in index actions: * Call `visible` in the controller instead of in model `search` methods. This decouples model visibility from model searching. * Explicitly pass CurrentUser when calling `visible`. This reduces hidden dependencies on the current user inside models. * Standardize on calling the method `visible`. In some places it was called `permitted` instead. * Add a `visible` base method to ApplicationModel.
31 lines
755 B
Ruby
31 lines
755 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_admin?
|
|
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
|