From 82d89b3c52a992e0279e7a27cde4409ef0ec8d66 Mon Sep 17 00:00:00 2001 From: Radhames Brito Date: Wed, 3 May 2017 07:06:01 -0400 Subject: [PATCH 1/2] Added test to non unique create retry --- lib/acts_as_taggable_on/tag.rb | 2 +- spec/acts_as_taggable_on/tag_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_taggable_on/tag.rb b/lib/acts_as_taggable_on/tag.rb index 05f2a6773..870ee4cb0 100644 --- a/lib/acts_as_taggable_on/tag.rb +++ b/lib/acts_as_taggable_on/tag.rb @@ -78,7 +78,7 @@ def self.find_or_create_all_with_like_by_name(*list) existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name } existing_tag || create(name: tag_name) rescue ActiveRecord::RecordNotUnique - if (tries -= 1).positive? + unless (tries -= 1).negative? ActiveRecord::Base.connection.execute 'ROLLBACK' retry end diff --git a/spec/acts_as_taggable_on/tag_spec.rb b/spec/acts_as_taggable_on/tag_spec.rb index 392e0f929..e74332e14 100644 --- a/spec/acts_as_taggable_on/tag_spec.rb +++ b/spec/acts_as_taggable_on/tag_spec.rb @@ -167,6 +167,17 @@ it 'should return an empty array if no tags are specified' do expect(ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name([])).to be_empty end + + context 'retry 3 times on not unique exception' do + it 'performs 3 tries before raising the exception' do + allow(ActsAsTaggableOn::Tag).to receive(:named_any).and_raise(ActiveRecord::RecordNotUnique) # trigger error inside block + expect(ActiveRecord::Base.connection).to receive(:execute).with('ROLLBACK').exactly(3).times + + expect { + ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name('AWESOME', 'awesome') + }.to raise_error ActsAsTaggableOn::DuplicateTagError + end + end end it 'should require a name' do From cef2a52f4315bf757a10a0dcc09b46db4cf8c786 Mon Sep 17 00:00:00 2001 From: Radhames Brito Date: Wed, 3 May 2017 10:02:52 -0400 Subject: [PATCH 2/2] add message to exceptions for preview AR versions --- spec/acts_as_taggable_on/tag_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/acts_as_taggable_on/tag_spec.rb b/spec/acts_as_taggable_on/tag_spec.rb index e74332e14..469e6bf79 100644 --- a/spec/acts_as_taggable_on/tag_spec.rb +++ b/spec/acts_as_taggable_on/tag_spec.rb @@ -170,7 +170,7 @@ context 'retry 3 times on not unique exception' do it 'performs 3 tries before raising the exception' do - allow(ActsAsTaggableOn::Tag).to receive(:named_any).and_raise(ActiveRecord::RecordNotUnique) # trigger error inside block + allow(ActsAsTaggableOn::Tag).to receive(:named_any).and_raise(ActiveRecord::RecordNotUnique.new('error')) # trigger error inside block expect(ActiveRecord::Base.connection).to receive(:execute).with('ROLLBACK').exactly(3).times expect {