Skip to content

Commit

Permalink
Allow blank on uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerei committed Jan 26, 2013
1 parent 7aa8d94 commit 04740d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/client_side_validations/mongoid/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def client_side_hash(model, attribute, force = nil)
hash = {}
hash[:message] = model.errors.generate_message(attribute, message_type, options.except(:scope))
hash[:case_sensitive] = options[:case_sensitive] if options.key?(:case_sensitive)
hash[:allow_blank] = true if options[:allow_blank]
hash[:id] = model.id unless model.new_record?
if options.key?(:scope) && options[:scope].present?
hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
Expand Down
5 changes: 5 additions & 0 deletions test/mongoid/cases/test_uniqueness_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def test_uniqueness_client_side_hash
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@book, :age)
end

def test_uniqueness_client_side_hash_allowing_blank
expected_hash = { :message => "is already taken", :allow_blank => true }
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:author_name], :allow_blank => true).client_side_hash(@book, :author_name)
end

def test_uniqueness_client_side_hash_with_custom_message
expected_hash = { :message => "is not available" }
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :message => "is not available").client_side_hash(@book, :age)
Expand Down

0 comments on commit 04740d6

Please sign in to comment.