Skip to content

Commit

Permalink
just another attempt to comply SQL standard
Browse files Browse the repository at this point in the history
  • Loading branch information
stiff committed Sep 8, 2014
1 parent 54060d4 commit 24d4dd7
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/acts_as_taggable_on/taggable/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def tagged_with(tags, options = {})
"#{alias_base_name[0..4]}#{taggings_context[0..6]}_taggings_#{ActsAsTaggableOn::Utils.sha_prefix(tags.map(&:name).join('_'))}"
)

order_by_matching_tag_count = options.delete(:order_by_matching_tag_count)
tagging_cond = (order_by_matching_tag_count ? "SELECT count(*) " : "SELECT 1 ") +
" FROM #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
tagging_cond = "#{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
" WHERE #{taggings_alias}.taggable_id = #{quote}#{table_name}#{quote}.#{primary_key}" +
" AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name, nil)}"

Expand All @@ -166,12 +164,9 @@ def tagged_with(tags, options = {})
])
end

if order_by_matching_tag_count
select_clause << "(#{tagging_cond}) as #{taggings_alias}_count"
conditions << "#{taggings_alias}_count > 0"
order_by << "#{taggings_alias}_count desc"
else
conditions << "EXISTS (#{tagging_cond})"
conditions << "EXISTS (SELECT 1 FROM #{tagging_cond})"
if options.delete(:order_by_matching_tag_count)
order_by << "(SELECT count(*) FROM #{tagging_cond}) desc"
end
else
tags = ActsAsTaggableOn::Tag.named_any(tag_list)
Expand Down

0 comments on commit 24d4dd7

Please sign in to comment.