Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request mbleigh#540 from seuros/bugfix
Browse files Browse the repository at this point in the history
[Bug fix] Fixes mbleigh#538
Version bump
  • Loading branch information
seuros committed May 16, 2014
2 parents 43cd421 + 4a159a2 commit 5826d9a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 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.not_owned.by_context(context).destroy_all(tag_id: old_tags)
end

# Create new taggings:
Expand Down
6 changes: 6 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,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

Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module ActsAsTaggableOn
VERSION = '3.2.2'
VERSION = '3.2.3'
end

28 changes: 26 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,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

0 comments on commit 5826d9a

Please sign in to comment.