From 19ac5369c51bb52e8692eb309a9b7b2815f3d7b1 Mon Sep 17 00:00:00 2001 From: Carl Chenet Date: Sat, 20 May 2017 15:29:35 +0200 Subject: [PATCH] Extract username regex --- app/models/user.rb | 3 ++- extras/markdowner.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index a5b4638..cf12ce0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,8 +55,9 @@ class User < ActiveRecord::Base validates :password, :presence => true, :on => :create + VALID_USERNAME = /[A-Za-z0-9][A-Za-z0-9_-]{0,24}/ validates :username, - :format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]{0,24}\Z/ }, + :format => { :with => /\A#{VALID_USERNAME}\Z/ }, :uniqueness => { :case_sensitive => false } validates_each :username do |record,attr,value| diff --git a/extras/markdowner.rb b/extras/markdowner.rb index 2194dfb..7af5fe3 100644 --- a/extras/markdowner.rb +++ b/extras/markdowner.rb @@ -44,7 +44,7 @@ class Markdowner def self.postprocess_text_node(node) while node - return unless node.string_content =~ /\B(@[\w\-]+)/ + return unless node.string_content =~ /\B(@#{User::VALID_USERNAME})/ before, user, after = $`, $1, $' node.string_content = before