Skip to content

Commit

Permalink
Merge pull request #46 from mamebro/fix-name-validation
Browse files Browse the repository at this point in the history
Fix name validation
  • Loading branch information
kurotaky committed Sep 28, 2013
2 parents 102ae68 + 54ceb6a commit 885097a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/brother.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Brother < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }

VALID_NAME_REGEX = /[a-z0-9]+\z/i
VALID_NAME_REGEX = /\A[a-z0-9]+\z/i
validates :name, presence: true, length: {within: 3..20},
format: { with: VALID_NAME_REGEX }, uniqueness: true

Expand Down
7 changes: 6 additions & 1 deletion spec/models/brother_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@
it { should_not be_valid }
end

describe "when name contains white space" do
before { @brother.name = "kun shi ka" }
it { should_not be_valid }
end

describe "when name is too long" do
before { @brother.name = "a" * 51 }
it { should_not be_valid }
end

describe "when name format is invalid" do
it "should be invalid" do
brothername = %w[@@@, #, ||, ---]
brothername = %w[@@@, #, ||, ---, www.nakaotakashi.com]
brothername.each do |invalid_name|
@brother.name = invalid_name
@brother.should_not be_valid
Expand Down

0 comments on commit 885097a

Please sign in to comment.