2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-14 22:27:53 -04:00
|
|
|
class UserNameChangeRequest < ApplicationRecord
|
2018-04-02 13:51:26 -04:00
|
|
|
after_initialize :initialize_attributes, if: :new_record?
|
2019-09-05 08:59:51 -04:00
|
|
|
validates :user_id, :original_name, :desired_name, presence: true
|
|
|
|
validates :status, inclusion: { :in => %w(pending approved rejected) }
|
2013-03-26 01:03:42 -04:00
|
|
|
belongs_to :user
|
2018-04-02 13:51:26 -04:00
|
|
|
belongs_to :approver, :class_name => "User", optional: true
|
2016-05-27 15:40:12 -04:00
|
|
|
validate :not_limited, :on => :create
|
2017-02-25 02:47:57 -05:00
|
|
|
validates :desired_name, user_name: true
|
2019-12-29 09:43:06 -05:00
|
|
|
attr_accessor :skip_limited_validation
|
2018-04-02 13:51:26 -04:00
|
|
|
|
|
|
|
def initialize_attributes
|
|
|
|
self.user_id ||= CurrentUser.user.id
|
|
|
|
self.original_name ||= CurrentUser.user.name
|
|
|
|
end
|
2019-12-29 09:43:06 -05:00
|
|
|
|
2013-03-26 01:03:42 -04:00
|
|
|
def self.pending
|
|
|
|
where(:status => "pending")
|
|
|
|
end
|
2013-03-26 18:13:03 -04:00
|
|
|
|
|
|
|
def self.approved
|
|
|
|
where(:status => "approved")
|
|
|
|
end
|
2017-01-11 14:07:39 -05:00
|
|
|
|
2022-01-22 10:36:50 -05:00
|
|
|
def self.search(params)
|
|
|
|
q = super
|
|
|
|
|
2023-08-03 16:01:53 -04:00
|
|
|
q = q.where_user(:user_id, :current, params)
|
2022-01-22 10:36:50 -05:00
|
|
|
|
|
|
|
if params[:original_name].present?
|
2022-12-30 13:10:39 -05:00
|
|
|
q = q.where_ilike(:original_name, User.normalize_name(params[:original_name]))
|
2022-01-22 10:36:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if params[:desired_name].present?
|
2022-12-30 13:10:39 -05:00
|
|
|
q = q.where_ilike(:desired_name, User.normalize_name(params[:desired_name]))
|
2022-01-22 10:36:50 -05:00
|
|
|
end
|
|
|
|
|
2023-07-07 08:32:57 -04:00
|
|
|
q.apply_basic_order(params)
|
2022-01-22 10:36:50 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 18:13:03 -04:00
|
|
|
def rejected?
|
|
|
|
status == "rejected"
|
|
|
|
end
|
2019-12-29 09:43:06 -05:00
|
|
|
|
2013-03-29 15:40:38 -04:00
|
|
|
def approved?
|
|
|
|
status == "approved"
|
|
|
|
end
|
2017-01-14 19:29:45 -05:00
|
|
|
|
|
|
|
def pending?
|
|
|
|
status == "pending"
|
|
|
|
end
|
2019-12-29 09:43:06 -05:00
|
|
|
|
2013-03-26 01:03:42 -04:00
|
|
|
def approve!
|
2019-09-09 12:37:58 -04:00
|
|
|
update(:status => "approved", :approver_id => CurrentUser.user.id)
|
2013-03-26 01:03:42 -04:00
|
|
|
user.update_attribute(:name, desired_name)
|
|
|
|
body = "Your name change request has been approved. Be sure to log in with your new user name."
|
2017-02-23 21:15:29 -05:00
|
|
|
Dmail.create_automated(:title => "Name change request approved", :body => body, :to_id => user_id)
|
2013-03-26 01:03:42 -04:00
|
|
|
end
|
2019-12-29 09:43:06 -05:00
|
|
|
|
2013-03-26 18:13:03 -04:00
|
|
|
def not_limited
|
2019-12-29 09:43:06 -05:00
|
|
|
return true if skip_limited_validation == true
|
2013-03-26 18:13:03 -04:00
|
|
|
if UserNameChangeRequest.where("user_id = ? and created_at >= ?", CurrentUser.user.id, 1.week.ago).exists?
|
|
|
|
errors.add(:base, "You can only submit one name change request per week")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2017-01-19 18:38:27 -05:00
|
|
|
|
|
|
|
def hidden_attributes
|
|
|
|
if CurrentUser.is_admin? || user == CurrentUser.user
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
super + [:change_reason, :rejection_reason]
|
|
|
|
end
|
|
|
|
end
|
2013-03-26 01:03:42 -04:00
|
|
|
end
|