mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 18:43:38 +08:00
Merge pull request #1159 from jd-erreape/user_refactors
Refactors in user model
This commit is contained in:
commit
807b9d438a
|
@ -145,10 +145,7 @@ class Group < ActiveRecord::Base
|
|||
protected
|
||||
|
||||
def name_format_validator
|
||||
validator = UsernameValidator.new(name)
|
||||
unless validator.valid_format?
|
||||
validator.errors.each { |e| errors.add(:name, e) }
|
||||
end
|
||||
UsernameValidator.perform_validation(self, 'name')
|
||||
end
|
||||
|
||||
# hack around AR
|
||||
|
|
|
@ -93,26 +93,16 @@ class User < ActiveRecord::Base
|
|||
def self.create_for_email(email, opts={})
|
||||
username = UserNameSuggester.suggest(email)
|
||||
|
||||
if SiteSetting.call_discourse_hub?
|
||||
begin
|
||||
discourse_hub_nickname_operation do
|
||||
match, available, suggestion = DiscourseHub.nickname_match?(username, email)
|
||||
username = suggestion unless match || available
|
||||
rescue => e
|
||||
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
||||
end
|
||||
end
|
||||
|
||||
user = User.new(email: email, username: username, name: username)
|
||||
user.trust_level = opts[:trust_level] if opts[:trust_level].present?
|
||||
user.save!
|
||||
|
||||
if SiteSetting.call_discourse_hub?
|
||||
begin
|
||||
DiscourseHub.register_nickname(username, email)
|
||||
rescue => e
|
||||
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
||||
end
|
||||
end
|
||||
discourse_hub_nickname_operation { DiscourseHub.register_nickname(username, email) }
|
||||
|
||||
user
|
||||
end
|
||||
|
@ -162,14 +152,8 @@ class User < ActiveRecord::Base
|
|||
current_username = self.username
|
||||
self.username = new_username
|
||||
|
||||
if current_username.downcase != new_username.downcase && SiteSetting.call_discourse_hub? && valid?
|
||||
begin
|
||||
DiscourseHub.change_nickname(current_username, new_username)
|
||||
rescue DiscourseHub::NicknameUnavailable
|
||||
false
|
||||
rescue => e
|
||||
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
||||
end
|
||||
if current_username.downcase != new_username.downcase && valid?
|
||||
User.discourse_hub_nickname_operation { DiscourseHub.change_nickname(current_username, new_username) }
|
||||
end
|
||||
|
||||
save
|
||||
|
@ -415,10 +399,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def username_format_validator
|
||||
validator = UsernameValidator.new(username)
|
||||
unless validator.valid_format?
|
||||
validator.errors.each { |e| errors.add(:username, e) }
|
||||
end
|
||||
UsernameValidator.perform_validation(self, 'username')
|
||||
end
|
||||
|
||||
def email_confirmed?
|
||||
|
@ -604,6 +585,20 @@ class User < ActiveRecord::Base
|
|||
email_token: email_tokens.first.token
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.discourse_hub_nickname_operation
|
||||
if SiteSetting.call_discourse_hub?
|
||||
begin
|
||||
yield
|
||||
rescue DiscourseHub::NicknameUnavailable
|
||||
false
|
||||
rescue => e
|
||||
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -1,4 +1,18 @@
|
|||
class UsernameValidator
|
||||
# Public: Perform the validation of a field in a given object
|
||||
# it adds the errors (if any) to the object that we're giving as parameter
|
||||
#
|
||||
# object - Object in which we're performing the validation
|
||||
# field_name - name of the field that we're validating
|
||||
#
|
||||
# Example: UsernameValidator.perform_validation(user, 'name')
|
||||
def self.perform_validation(object, field_name)
|
||||
validator = UsernameValidator.new(object.send(field_name))
|
||||
unless validator.valid_format?
|
||||
validator.errors.each { |e| object.errors.add(field_name.to_sym, e) }
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(username)
|
||||
@username = username
|
||||
@errors = []
|
||||
|
|
Loading…
Reference in New Issue
Block a user