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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed May 24, 2014
1 parent a9eeae3 commit 7b6abf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/acts_as_taggable_on/taggable/core.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module ActsAsTaggableOn::Taggable
module Core
def self.included(base)
Expand Down Expand Up @@ -82,7 +83,7 @@ def grouped_column_names_for(object)
# 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)
tag_list = ActsAsTaggableOn::TagListParser.parse(tags)
options = options.dup
empty_result = where('1 = 0')

Expand Down Expand Up @@ -141,7 +142,7 @@ def tagged_with(tags, options = {})

# don't need to sanitize sql, map all ids and join with OR logic
conditions << tags.map { |t| "#{taggings_alias}.tag_id = #{quote_value(t.id, nil)}" }.join(' OR ')
select_clause = " #{table_name}.*" unless context and tag_types.one?
select_clause << " #{table_name}.*" unless context and tag_types.one?

if owned_by
tagging_join << ' AND ' +
Expand All @@ -154,6 +155,7 @@ def tagged_with(tags, options = {})

joins << tagging_join
group = "#{table_name}.#{primary_key}"
select_clause << group
else
tags = ActsAsTaggableOn::Tag.named_any(tag_list)

Expand Down Expand Up @@ -183,7 +185,7 @@ def tagged_with(tags, options = {})

group ||= [] # Rails interprets this as a no-op in the group() call below
if options.delete(:order_by_matching_tag_count)
select_clause = "#{table_name}.*, COUNT(#{taggings_alias}.tag_id) AS #{taggings_alias}_count"
select_clause << "#{table_name}.*, COUNT(#{taggings_alias}.tag_id) AS #{taggings_alias}_count"
group_columns = ActsAsTaggableOn::Utils.using_postgresql? ? grouped_column_names_for(self) : "#{table_name}.#{primary_key}"
group = group_columns
order_by << "#{taggings_alias}_count DESC"
Expand All @@ -203,8 +205,10 @@ def tagged_with(tags, options = {})

order_by << options[:order] if options[:order].present?

select(select_clause)
.joins(joins.join(' '))

query = self
query = self.select(select_clause.join(',')) unless select_clause.empty?
query.joins(joins.join(' '))
.where(conditions.join(' AND '))
.group(group)
.having(having)
Expand Down Expand Up @@ -259,7 +263,7 @@ def tag_list_cache_on(context)
if instance_variable_get(variable_name)
instance_variable_get(variable_name)
elsif cached_tag_list_on(context) && self.class.caching_tag_list_on?(context)
instance_variable_set(variable_name, ActsAsTaggableOn::TagList.from(cached_tag_list_on(context)))
instance_variable_set(variable_name, ActsAsTaggableOn::TagListParser.parse(cached_tag_list_on(context)))
else
instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new(tags_on(context).map(&:name)))
end
Expand Down Expand Up @@ -309,7 +313,7 @@ def set_tag_list_on(context, new_list)
variable_name = "@#{context.to_s.singularize}_list"
process_dirty_object(context, new_list) unless custom_contexts.include?(context.to_s)

instance_variable_set(variable_name, ActsAsTaggableOn::TagList.from(new_list))
instance_variable_set(variable_name, ActsAsTaggableOn::TagListParser.parse(new_list))
end

def tagging_contexts
Expand Down
8 changes: 8 additions & 0 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@
expect(TaggableModel.tagged_with('ruby').group(:created_at).count.count).to eq(1)
end

it 'can be used as scope' do
@taggable.skill_list = 'ruby'
@taggable.save
untaggable_model = @taggable.untaggable_models.create!(name:'foobar')
scope_tag = TaggableModel.tagged_with('ruby', :any => true, order: 'taggable_models.name asc')
expect(UntaggableModel.joins(:taggable_model).merge(scope_tag).except(:select)).to eq([untaggable_model])
end

it "shouldn't generate a query with DISTINCT by default" do
@taggable.skill_list = 'ruby, rails, css'
@taggable.save
Expand Down

0 comments on commit 7b6abf9

Please sign in to comment.