diff --git a/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb b/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb index 59b0c9780..d1f57528f 100644 --- a/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb +++ b/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb @@ -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: diff --git a/lib/acts_as_taggable_on/tagging.rb b/lib/acts_as_taggable_on/tagging.rb index 61feea31c..5ee2fce14 100644 --- a/lib/acts_as_taggable_on/tagging.rb +++ b/lib/acts_as_taggable_on/tagging.rb @@ -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 diff --git a/spec/acts_as_taggable_on/tagging_spec.rb b/spec/acts_as_taggable_on/tagging_spec.rb index 6b3c7e5b1..21fe3a589 100644 --- a/spec/acts_as_taggable_on/tagging_spec.rb +++ b/spec/acts_as_taggable_on/tagging_spec.rb @@ -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 \ No newline at end of file