Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tagging.remove_unused_tags should gracefully handle already deleted tags #988

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m

* Fixes
* [@nbulaj Add support for Ruby 2.7 and it's kwargs](https://github.com/mbleigh/acts-as-taggable-on/pull/910)
* [@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)

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 if !ActsAsTaggableOn.remove_unused_tags || tag.blank?

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