Added discourse hub nickname operations simplify and avoid repeated code

This commit is contained in:
Juan de Dios Herrero 2013-07-07 12:40:35 +02:00
parent c6c8246399
commit e4fc6e02ed

View File

@ -93,26 +93,16 @@ class User < ActiveRecord::Base
def self.create_for_email(email, opts={}) def self.create_for_email(email, opts={})
username = UserNameSuggester.suggest(email) username = UserNameSuggester.suggest(email)
if SiteSetting.call_discourse_hub? discourse_hub_nickname_operation do
begin match, available, suggestion = DiscourseHub.nickname_match?(username, email)
match, available, suggestion = DiscourseHub.nickname_match?(username, email) username = suggestion unless match || available
username = suggestion unless match || available
rescue => e
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
end
end end
user = User.new(email: email, username: username, name: username) user = User.new(email: email, username: username, name: username)
user.trust_level = opts[:trust_level] if opts[:trust_level].present? user.trust_level = opts[:trust_level] if opts[:trust_level].present?
user.save! user.save!
if SiteSetting.call_discourse_hub? discourse_hub_nickname_operation { DiscourseHub.register_nickname(username, email) }
begin
DiscourseHub.register_nickname(username, email)
rescue => e
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
end
end
user user
end end
@ -162,14 +152,8 @@ class User < ActiveRecord::Base
current_username = self.username current_username = self.username
self.username = new_username self.username = new_username
if current_username.downcase != new_username.downcase && SiteSetting.call_discourse_hub? && valid? if current_username.downcase != new_username.downcase && valid?
begin User.discourse_hub_nickname_operation { DiscourseHub.change_nickname(current_username, new_username) }
DiscourseHub.change_nickname(current_username, new_username)
rescue DiscourseHub::NicknameUnavailable
false
rescue => e
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
end
end end
save save
@ -604,6 +588,20 @@ class User < ActiveRecord::Base
email_token: email_tokens.first.token email_token: email_tokens.first.token
) )
end end
private
def self.discourse_hub_nickname_operation(&block)
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 end
# == Schema Information # == Schema Information