enforce a maximum username length of 25

This commit is contained in:
joshua stein 2014-03-18 00:57:13 -05:00
parent 7f9c227ed0
commit c09542a80a
3 changed files with 10 additions and 3 deletions

View file

@ -27,7 +27,8 @@ class User < ActiveRecord::Base
validates :password, :presence => true, :on => :create
validates :username, :format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\Z/ },
validates :username,
:format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]{0,24}\Z/ },
:uniqueness => { :case_sensitive => false }
validates_each :username do |record,attr,value|
@ -56,6 +57,12 @@ class User < ActiveRecord::Base
end
end
def self.username_regex
User.validators_on(:username).select{|v|
v.class == ActiveModel::Validations::FormatValidator }.first.
options[:with].inspect
end
def as_json(options = {})
h = super(:only => [
:username,

View file

@ -14,7 +14,7 @@
<%= f.label :username, "Username:", :class => "required" %>
<%= f.text_field :username, :size => 15 %>
<span class="hint">
<tt>[A-Za-z0-9][A-Za-z0-9_-]*</tt>
<tt><%= User.username_regex %></tt>
</span>
</div>

View file

@ -28,7 +28,7 @@
<%= f.label :username, "Username:", :class => "required" %>
<%= f.text_field :username, :size => 30 %>
<span class="hint">
<tt>[A-Za-z0-9][A-Za-z0-9_-]*</tt>
<tt><%= User.username_regex %></tt>
</span>
<br />