Skip to content

Commit

Permalink
[Bug fix] Removing a tag from a record was affecting all records with…
Browse files Browse the repository at this point in the history
… the same tag. Fixes mbleigh#538
  • Loading branch information
seuros committed May 16, 2014
1 parent d1ade8c commit 2df38be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def save_tags

# Destroy old taggings:
if old_tags.present?
ActsAsTaggableOn::Tagging.destroy_all(tagger_type: nil, tagger_id: nil, context: context.to_s, tag_id: old_tags)
self.taggings.owned_by.by_context(context).destroy_all(tag_id: old_tags)
end

# Create new taggings:
Expand Down
3 changes: 3 additions & 0 deletions lib/acts_as_taggable_on/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Tagging < ::ActiveRecord::Base #:nodoc:
belongs_to :taggable, polymorphic: true
belongs_to :tagger, polymorphic: true

scope :owned_by, ->(owner = nil) { where(tagger: owner) }
scope :by_context, ->(context= :tags) { where(context: context) }

validates_presence_of :context
validates_presence_of :tag_id

Expand Down
25 changes: 23 additions & 2 deletions spec/acts_as_taggable_on/tagging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,26 @@
2.times { ActsAsTaggableOn::Tagging.create(taggable: @taggable, tag: @tag, context: 'tags') }
}).to change(ActsAsTaggableOn::Tagging, :count).by(1)
end

end

it 'should not delete tags of other records' do
6.times { TaggableModel.create(name: 'Bob Jones', tag_list: 'very, serious, bug') }
expect(ActsAsTaggableOn::Tag.count).to eq(3)
taggable = TaggableModel.first
taggable.update(tag_list: 'bug')
expect(taggable.tag_list).to eq(['bug'])

another_taggable = TaggableModel.where('id != ?', taggable.id).first
expect(another_taggable.tag_list).to eq(%w(very serious bug))
end

context 'scopes' do
xdescribe '.by_context' do
# TODO
end

xdescribe '.owned_by' do
# TODO
end
end

end

0 comments on commit 2df38be

Please sign in to comment.