Skip to content

Commit

Permalink
owned_by in tagged_with mbleigh#138
Browse files Browse the repository at this point in the history
  • Loading branch information
artemk committed Sep 26, 2011
1 parent cee0753 commit 8787a8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Expand Up @@ -59,12 +59,14 @@ def grouped_column_names_for(object)
# * <tt>:exclude</tt> - if set to true, return objects that are *NOT* tagged with the specified tags
# * <tt>:any</tt> - if set to true, return objects that are tagged with *ANY* of the specified tags
# * <tt>:match_all</tt> - if set to true, return objects that are *ONLY* tagged with the specified tags
# * <tt>:owned_by</tt> - return objects that are *ONLY* owned by the owner
#
# Example:
# User.tagged_with("awesome", "cool") # Users that are tagged with awesome and cool
# User.tagged_with("awesome", "cool", :exclude => true) # Users that are not tagged with awesome or cool
# User.tagged_with("awesome", "cool", :any => true) # Users that are tagged with awesome or cool
# User.tagged_with("awesome", "cool", :match_all => true) # Users that are tagged with just awesome and cool
# User.tagged_with("awesome", "cool", :owned_by => foo ) # Users that are tagged with just awesome and cool by 'foo'
def tagged_with(tags, options = {})
tag_list = ActsAsTaggableOn::TagList.from(tags)
empty_result = scoped(:conditions => "1 = 0")
Expand All @@ -75,6 +77,7 @@ def tagged_with(tags, options = {})
conditions = []

context = options.delete(:on)
owned_by = options.delete(:owned_by)
alias_base_name = undecorated_table_name.gsub('.','_')

if options.delete(:exclude)
Expand Down Expand Up @@ -119,6 +122,15 @@ def tagged_with(tags, options = {})
" AND #{taggings_alias}.tag_id = #{tag.id}"
tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context

if owned_by
tagging_join << " AND " +
sanitize_sql([
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
owned_by.class.to_s
])
end

joins << tagging_join
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/acts_as_taggable_on/tagger_spec.rb
Expand Up @@ -17,6 +17,20 @@
@user.owned_tags.size == 2
end

it "should scope objects returned by tagged_with by owners" do
@taggable2 = TaggableModel.create(:name => "Jim Jones")
@taggable3 = TaggableModel.create(:name => "Jane Doe")

@user2 = TaggableUser.new
@user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
@user2.tag(@taggable2, :with => 'ruby, scheme', :on => :tags)
@user2.tag(@taggable3, :with => 'ruby, scheme', :on => :tags)

TaggableModel.tagged_with(%w(ruby scheme), :owned_by => @user).count.should == 1
TaggableModel.tagged_with(%w(ruby scheme), :owned_by => @user2).count.should == 2

end

it "should not overlap tags from different taggers" do
@user2 = TaggableUser.new
lambda{
Expand Down

0 comments on commit 8787a8e

Please sign in to comment.