Skip to content
Browse files

Merge pull request #224 from unindented/tagger_sti

Tag ownership and STI
  • Loading branch information...
2 parents 757aeac + fe4090b commit 320748a9d8c427fdcc59fca3ac6d416130617aa1 @artemk artemk committed
View
2 lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
@@ -149,7 +149,7 @@ def tagged_with(tags, options = {})
sanitize_sql([
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
- owned_by.class.to_s
+ owned_by.class.base_class.to_s
])
end
View
6 lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb
@@ -35,7 +35,7 @@ def owner_tags_on(owner, context)
else
scope = base_tags.where([%(#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND
#{ActsAsTaggableOn::Tagging.table_name}.tagger_id = ? AND
- #{ActsAsTaggableOn::Tagging.table_name}.tagger_type = ?), context.to_s, owner.id, owner.class.to_s])
+ #{ActsAsTaggableOn::Tagging.table_name}.tagger_type = ?), context.to_s, owner.id, owner.class.base_class.to_s])
end
# when preserving tag order, return tags in created order
# if we added the order to the association this would always apply
@@ -103,7 +103,7 @@ def save_owned_tags
# have the correct context, and are removed from the list.
if old_tags.present?
old_taggings = ActsAsTaggableOn::Tagging.where(:taggable_id => id, :taggable_type => self.class.base_class.to_s,
- :tagger_type => owner.class.to_s, :tagger_id => owner.id,
+ :tagger_type => owner.class.base_class.to_s, :tagger_id => owner.id,
:tag_id => old_tags, :context => context).all
end
@@ -123,4 +123,4 @@ def save_owned_tags
end
end
end
-end
+end
View
26 spec/acts_as_taggable_on/tagger_spec.rb
@@ -107,6 +107,32 @@
lambda {
@user.tag(@taggable, :with => 'epic', :on => :tags, :skip_save => true)
}.should_not change(ActsAsTaggableOn::Tagging, :count)
+ end
+ describe "Single Table Inheritance" do
+ before do
+ @user3 = InheritingTaggableUser.create
+ end
+
+ it "should have taggings" do
+ @user3.tag(@taggable, :with=>'ruby,scheme', :on=>:tags)
+ @user3.owned_taggings.size == 2
+ end
+
+ it "should have tags" do
+ @user3.tag(@taggable, :with=>'ruby,scheme', :on=>:tags)
+ @user3.owned_tags.size == 2
+ end
+
+ it "should return tags for the inheriting tagger" do
+ @user3.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
+ @taggable.tags_from(@user3).sort.should == %w(ruby scheme).sort
+ end
+
+ it "should scope objects returned by tagged_with by owners" do
+ @user3.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
+ TaggableModel.tagged_with(%w(ruby scheme), :owned_by => @user3).count.should == 1
+ end
end
+
end
View
3 spec/models.rb
@@ -30,6 +30,9 @@ class TaggableUser < ActiveRecord::Base
acts_as_tagger
end
+class InheritingTaggableUser < TaggableUser
+end
+
class UntaggableModel < ActiveRecord::Base
belongs_to :taggable_model
end

0 comments on commit 320748a

Please sign in to comment.
Something went wrong with that request. Please try again.