add support for user name changes
This commit is contained in:
@@ -3,6 +3,7 @@ class TagAlias < ActiveRecord::Base
|
||||
after_save :clear_all_cache
|
||||
after_destroy :clear_all_cache
|
||||
before_validation :initialize_creator, :on => :create
|
||||
before_validation :normalize_names
|
||||
validates_presence_of :creator_id, :antecedent_name, :consequent_name
|
||||
validates_uniqueness_of :antecedent_name
|
||||
validate :absence_of_transitive_relation
|
||||
@@ -88,6 +89,11 @@ class TagAlias < ActiveRecord::Base
|
||||
def is_active?
|
||||
status == "active"
|
||||
end
|
||||
|
||||
def normalize_names
|
||||
self.antecedent_name = antecedent_name.mb_chars.downcase.tr(" ", "_").strip
|
||||
self.consequent_name = consequent_name.downcase.tr(" ", "_").strip
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
|
||||
@@ -3,6 +3,7 @@ class TagImplication < ActiveRecord::Base
|
||||
after_save :update_descendant_names_for_parent
|
||||
belongs_to :creator, :class_name => "User"
|
||||
before_validation :initialize_creator, :on => :create
|
||||
before_validation :normalize_names
|
||||
validates_presence_of :creator_id, :antecedent_name, :consequent_name
|
||||
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
|
||||
validate :absence_of_circular_relation
|
||||
@@ -129,6 +130,11 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_names
|
||||
self.antecedent_name = antecedent_name.downcase.tr(" ", "_").strip
|
||||
self.consequent_name = consequent_name.downcase.tr(" ", "_").strip
|
||||
end
|
||||
|
||||
def is_pending?
|
||||
status == "pending"
|
||||
|
||||
@@ -531,7 +531,7 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def admins
|
||||
where("is_admin = TRUE")
|
||||
where("level = ?", Levels::ADMIN)
|
||||
end
|
||||
|
||||
def with_email(email)
|
||||
|
||||
48
app/models/user_name_change_request.rb
Normal file
48
app/models/user_name_change_request.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
class UserNameChangeRequest < ActiveRecord::Base
|
||||
validates_presence_of :user_id, :original_name, :desired_name
|
||||
validates_inclusion_of :status, :in => %w(pending approved rejected)
|
||||
belongs_to :user
|
||||
belongs_to :approver, :class_name => "User"
|
||||
validate :uniqueness_of_desired_name
|
||||
validates_length_of :desired_name, :within => 2..100, :on => :create
|
||||
validates_format_of :desired_name, :with => /\A[^\s:]+\Z/, :on => :create, :message => "cannot have whitespace or colons"
|
||||
after_create :notify_admins
|
||||
|
||||
def self.pending
|
||||
where(:status => "pending")
|
||||
end
|
||||
|
||||
def feedback
|
||||
UserFeedback.for_user(user_id).order("id desc").all
|
||||
end
|
||||
|
||||
def notify_admins
|
||||
title = "#{original_name} is requesting a name change to #{desired_name}"
|
||||
body = title + "\n\n\"See request\":/user_name_change_requests/#{id}"
|
||||
User.admins.find_each do |user|
|
||||
Dmail.create_split(:title => title, :body => body, :to_id => user.id)
|
||||
end
|
||||
end
|
||||
|
||||
def approve!
|
||||
update_attribute(:status, "approved")
|
||||
user.update_attribute(:name, desired_name)
|
||||
body = "Your name change request has been approved. Be sure to log in with your new user name."
|
||||
Dmail.create_split(:title => "Name change request approved", :body => body, :to_id => user_id)
|
||||
end
|
||||
|
||||
def reject!(reason)
|
||||
update_attributes(:status => "rejected", :rejection_reason => reason)
|
||||
body = "Your name change request has been rejected for the following reason: #{rejection_reason}"
|
||||
Dmail.create_split(:title => "Name change request rejected", :body => body, :to_id => user_id)
|
||||
end
|
||||
|
||||
def uniqueness_of_desired_name
|
||||
if User.find_by_name(desired_name)
|
||||
errors.add(:desired_name, "already exists")
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user