From e4fc6e02ed1a26193132d652bafbfed52589474c Mon Sep 17 00:00:00 2001 From: Juan de Dios Herrero Date: Sun, 7 Jul 2013 12:40:35 +0200 Subject: [PATCH 1/2] Added discourse hub nickname operations simplify and avoid repeated code --- app/models/user.rb | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 1ae91f02e41..22db9d07ae4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 - 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 + discourse_hub_nickname_operation do + match, available, suggestion = DiscourseHub.nickname_match?(username, email) + username = suggestion unless match || available 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 @@ -604,6 +588,20 @@ class User < ActiveRecord::Base email_token: email_tokens.first.token ) 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 # == Schema Information From 27b038cd40314b3b47fe22f0b6b3dcfda1e0b922 Mon Sep 17 00:00:00 2001 From: Juan de Dios Herrero Date: Sun, 7 Jul 2013 13:05:18 +0200 Subject: [PATCH 2/2] Refactored username validations to avoid repeated code --- app/models/group.rb | 5 +---- app/models/user.rb | 23 ++++++++++------------- app/models/username_validator.rb | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index 94685f3a91f..3d45987b0e0 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 22db9d07ae4..81b8513cf05 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -399,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? @@ -591,17 +588,17 @@ class User < ActiveRecord::Base 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 + 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 diff --git a/app/models/username_validator.rb b/app/models/username_validator.rb index 779afa0b3f3..5a273e778d7 100644 --- a/app/models/username_validator.rb +++ b/app/models/username_validator.rb @@ -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 = []