Skip to content
Browse files

Merge pull request #304 from makaroni4/master

Added method to avoid name uniqueness validations
  • Loading branch information...
2 parents abbeb9a + 116f0f7 commit 76cc28c6a57b040f7d38db18161ec38e5b58da66 @artemk artemk committed Jan 2, 2013
Showing with 34 additions and 2 deletions.
  1. +6 −1 lib/acts_as_taggable_on/tag.rb
  2. +28 −1 spec/acts_as_taggable_on/tag_spec.rb
View
7 lib/acts_as_taggable_on/tag.rb
@@ -11,9 +11,14 @@ class Tag < ::ActiveRecord::Base
### VALIDATIONS:
validates_presence_of :name
- validates_uniqueness_of :name
+ validates_uniqueness_of :name, :if => :validates_name_uniqueness?
validates_length_of :name, :maximum => 255
+ # monkey patch this method if don't need name uniqueness validation
+ def validates_name_uniqueness?
+ true
+ end
+
### SCOPES:
def self.named(name)
View
29 spec/acts_as_taggable_on/tag_spec.rb
@@ -182,4 +182,31 @@
ActsAsTaggableOn::Tag.named('cool').should_not include(uppercase_tag)
end
end
-end
+
+ describe "name uniqeness validation" do
+ let(:duplicate_tag) { ActsAsTaggableOn::Tag.new(:name => 'ror') }
+
+ before { ActsAsTaggableOn::Tag.create(:name => 'ror') }
+
+ context "when don't need unique names" do
+ it "should not run uniqueness validation" do
+ duplicate_tag.stub(:validates_name_uniqueness?).and_return(false)
+ duplicate_tag.save
+ duplicate_tag.should be_persisted
+ end
+ end
+
+ context "when do need unique names" do
+ it "should run uniqueness validation" do
+ duplicate_tag.should_not be_valid
+ end
+
+ it "add error to name" do
+ duplicate_tag.save
+
+ duplicate_tag.should have(1).errors
+ duplicate_tag.errors.messages[:name].should include('has already been taken')
+ end
+ end
+ end
+end

0 comments on commit 76cc28c

Please sign in to comment.
Something went wrong with that request. Please try again.