mirror of
https://github.com/discourse/discourse.git
synced 2025-02-08 04:18:23 +00:00
Refactored username validations to avoid repeated code
This commit is contained in:
parent
e4fc6e02ed
commit
27b038cd40
@ -145,10 +145,7 @@ class Group < ActiveRecord::Base
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
def name_format_validator
|
def name_format_validator
|
||||||
validator = UsernameValidator.new(name)
|
UsernameValidator.perform_validation(self, 'name')
|
||||||
unless validator.valid_format?
|
|
||||||
validator.errors.each { |e| errors.add(:name, e) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# hack around AR
|
# hack around AR
|
||||||
|
@ -399,10 +399,7 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def username_format_validator
|
def username_format_validator
|
||||||
validator = UsernameValidator.new(username)
|
UsernameValidator.perform_validation(self, 'username')
|
||||||
unless validator.valid_format?
|
|
||||||
validator.errors.each { |e| errors.add(:username, e) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def email_confirmed?
|
def email_confirmed?
|
||||||
@ -591,17 +588,17 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.discourse_hub_nickname_operation(&block)
|
def self.discourse_hub_nickname_operation
|
||||||
if SiteSetting.call_discourse_hub?
|
if SiteSetting.call_discourse_hub?
|
||||||
begin
|
begin
|
||||||
yield
|
yield
|
||||||
rescue DiscourseHub::NicknameUnavailable
|
rescue DiscourseHub::NicknameUnavailable
|
||||||
false
|
false
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
@ -1,4 +1,18 @@
|
|||||||
class UsernameValidator
|
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)
|
def initialize(username)
|
||||||
@username = username
|
@username = username
|
||||||
@errors = []
|
@errors = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user