From 4a159a2ed96c0b862d8f1f1b915ade47aa45d6a8 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 16 May 2014 16:29:32 +0000 Subject: [PATCH] [Bug fix] Removing a tag from a record was affecting all records with the same tag. Fixes #538 --- .../acts_as_taggable_on/core.rb | 2 +- lib/acts_as_taggable_on/tagging.rb | 6 ++++ lib/acts_as_taggable_on/version.rb | 2 +- spec/acts_as_taggable_on/tagging_spec.rb | 28 +++++++++++++++++-- 4 files changed, 34 insertions(+), 4 deletions(-) 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..178be62af 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.not_owned.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..b369f8282 100644 --- a/lib/acts_as_taggable_on/tagging.rb +++ b/lib/acts_as_taggable_on/tagging.rb @@ -15,6 +15,12 @@ class Tagging < ::ActiveRecord::Base #:nodoc: belongs_to :taggable, polymorphic: true belongs_to :tagger, polymorphic: true + scope :owned_by, ->(owner) { where(tagger: owner) } + scope :not_owned, -> { where(tagger_id: nil, tagger_type: nil) } + + scope :by_contexts, ->(contexts = ['tags']) { where(context: contexts) } + scope :by_context, ->(context= 'tags') { by_contexts(context.to_s) } + validates_presence_of :context validates_presence_of :tag_id diff --git a/lib/acts_as_taggable_on/version.rb b/lib/acts_as_taggable_on/version.rb index 24fa9fd55..1a0e5d0a4 100644 --- a/lib/acts_as_taggable_on/version.rb +++ b/lib/acts_as_taggable_on/version.rb @@ -1,4 +1,4 @@ module ActsAsTaggableOn - VERSION = '3.2.2' + VERSION = '3.2.3' end diff --git a/spec/acts_as_taggable_on/tagging_spec.rb b/spec/acts_as_taggable_on/tagging_spec.rb index 6b3c7e5b1..c21d9fe61 100644 --- a/spec/acts_as_taggable_on/tagging_spec.rb +++ b/spec/acts_as_taggable_on/tagging_spec.rb @@ -23,5 +23,29 @@ 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.tag_list = 'bug' + taggable.save + + expect(taggable.tag_list).to eq(['bug']) + + another_taggable = TaggableModel.where('id != ?', taggable.id).sample + expect(another_taggable.tag_list).to eq(%w(very serious bug)) + end + + pending 'context scopes' do + describe '.by_context' + + describe '.by_contexts' + + describe '.owned_by' + + describe '.not_owned' + + end + +end \ No newline at end of file