Skip to content
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 @@ -17,6 +17,7 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m
* Reduce packaged gem size
* Fixes
* [@iainbeeston Fix mismatched indentations warning message](https://github.com/mbleigh/acts-as-taggable-on/pull/1150)
* [@james-reading Fix issue when reordering tags and @@remove_unused_tags is true](https://github.com/mbleigh/acts-as-taggable-on/pull/1154)

### [v12.0.0) / 2024-11-09](https://github.com/mbleigh/acts-as-taggable-on/compare/v11.0.0...v12.0.0)

Expand Down
20 changes: 11 additions & 9 deletions lib/acts-as-taggable-on/taggable/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,17 @@ def save_tags
# Delete discarded tags and create new tags
end

# Destroy old taggings:
taggings.not_owned.by_context(context).where(tag_id: old_tags).destroy_all if old_tags.present?

# Create new taggings:
new_tags.each do |tag|
if taggable_tenant
taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self, tenant: taggable_tenant)
else
taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self)
transaction do
# Destroy old taggings:
taggings.not_owned.by_context(context).where(tag_id: old_tags).destroy_all if old_tags.present?

# Create new taggings:
new_tags.each do |tag|
if taggable_tenant
taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self, tenant: taggable_tenant)
else
taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/acts-as-taggable-on/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Tagging < ActsAsTaggableOn.base_class.constantize # :nodoc:

validates_uniqueness_of :tag_id, scope: %i[taggable_type taggable_id context tagger_id tagger_type]

after_destroy :remove_unused_tags
after_destroy_commit :remove_unused_tags

private

Expand Down
17 changes: 17 additions & 0 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@
ids = @taggable.tags.map { |t| t.taggings.first.id }
expect(ids).to eq(ids.sort)
end

it 'can reorder tags successfully when @@remove_unused_tags is true' do
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly uses @@remove_unused_tags (class variable notation) when it should reference the configuration setting remove_unused_tags. The double @@ is misleading as this is an ActsAsTaggableOn configuration attribute, not a class variable.

Suggested change
it 'can reorder tags successfully when @@remove_unused_tags is true' do
it 'can reorder tags successfully when remove_unused_tags is true' do

Copilot uses AI. Check for mistakes.
previous_setting = ActsAsTaggableOn.remove_unused_tags
ActsAsTaggableOn.remove_unused_tags = true

@taggable.tag_list = 'pow, ruby, rails'
@taggable.save!
@taggable.reload
expect(@taggable.tag_list).to eq(%w(pow ruby rails))

@taggable.tag_list = 'rails, pow, ruby'
@taggable.save!
@taggable.reload
expect(@taggable.tag_list).to eq(%w(rails pow ruby))
ensure
ActsAsTaggableOn.remove_unused_tags = previous_setting
end
end

RSpec.describe 'Taggable' do
Expand Down
Loading