Skip to content

Commit

Permalink
Tagging.remove_unused_tags should gracefully handle already deleted tags
Browse files Browse the repository at this point in the history
  • Loading branch information
irphilli committed Feb 26, 2020
1 parent 6055f96 commit 03a4d70
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Each change should fall into categories that would affect whether the release is

As such, _Breaking Changes_ are major. _Features_ would map to either major or minor. _Fixes_, _Performance_, and _Misc_ are either minor or patch, the difference being kind of fuzzy for the purposes of history. Adding _Documentation_ (including tests) would be patch level.

### [Master / Unreleased](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.5.0...master)
* Fixes
* [@irphilli Tagging.remove_unused_tags should gracefully handle already deleted tags](https://github.com/mbleigh/acts-as-taggable-on/pull/988)

### [6.5.0 / 2019-11-07](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.0.0...v6.5.0)

* Features
Expand Down
14 changes: 8 additions & 6 deletions lib/acts_as_taggable_on/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class Tagging < ::ActiveRecord::Base #:nodoc:
private

def remove_unused_tags
if ActsAsTaggableOn.remove_unused_tags
if ActsAsTaggableOn.tags_counter
tag.destroy if tag.reload.taggings_count.zero?
else
tag.destroy if tag.reload.taggings.count.zero?
end
return unless ActsAsTaggableOn.remove_unused_tags

if ActsAsTaggableOn.tags_counter
tag.destroy if tag.reload.taggings_count.zero?
else
tag.destroy if tag.reload.taggings.count.zero?
end
rescue ActiveRecord::RecordNotFound
# Tag already deleted - nothing else to do
end
end
end
20 changes: 20 additions & 0 deletions spec/acts_as_taggable_on/tagging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@
ActsAsTaggableOn.remove_unused_tags = previous_setting
end

it 'should succesfully destroy taggings if tag already destroyed' do
previous_setting = ActsAsTaggableOn.remove_unused_tags
ActsAsTaggableOn.remove_unused_tags = true
ActsAsTaggableOn::Tag.destroy_all
expect(ActsAsTaggableOn::Tagging.count).to eql(0)
@taggable = TaggableModel.create(name: 'Bob Jones')
@taggable.update_attribute :tag_list, 'aaa,bbb,ccc'

# Initalize taggings with preloaded tags
taggings = ActsAsTaggableOn::Tagging.preload(:tag).all
taggings.to_a

# Delete tags before destroying taggings
ActsAsTaggableOn::Tag.delete_all
taggings.destroy_all

expect(ActsAsTaggableOn::Tagging.count).to eql(0)
ActsAsTaggableOn.remove_unused_tags = previous_setting
end

describe 'context scopes' do
before do
@tagging_2 = ActsAsTaggableOn::Tagging.new
Expand Down

0 comments on commit 03a4d70

Please sign in to comment.