Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13 from mlitwiniuk/master

New class method popular_tags
  • Loading branch information...
commit 468d05bf91396b737f2c12108b4fefec98583770 2 parents 7def600 + 57c7d3e
@bradphelan authored
View
4 Gemfile
@@ -3,14 +3,14 @@ source "http://rubygems.org"
# Example:
# gem "activesupport", ">= 2.3.5"
gem "activerecord", ">= 3.2.0"
-gem "squeel", :require => false
+gem "squeel", '~> 1.0.0', :require => false
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rspec", "~> 2.3.0"
gem "yard", "~> 0.6.0"
- gem "bundler", "~> 1.0.0"
+ gem "bundler", "~> 1.1.0"
gem "jeweler", "~> 1.6.4"
#gem "rcov", ">= 0"
gem 'sqlite3'
View
26 Gemfile.lock
@@ -1,18 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.2.0)
- activesupport (= 3.2.0)
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
builder (~> 3.0.0)
- activerecord (3.2.0)
- activemodel (= 3.2.0)
- activesupport (= 3.2.0)
- arel (~> 3.0.0)
+ activerecord (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activesupport (3.2.0)
+ activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
- arel (3.0.0)
+ arel (3.0.2)
builder (3.0.0)
diff-lcs (1.1.3)
git (1.2.5)
@@ -21,7 +21,7 @@ GEM
bundler (~> 1.0)
git (>= 1.2.5)
rake
- multi_json (1.0.4)
+ multi_json (1.3.4)
polyamorous (0.5.0)
activerecord (~> 3.0)
rake (0.9.2.2)
@@ -34,11 +34,11 @@ GEM
diff-lcs (~> 1.1.2)
rspec-mocks (2.3.0)
sqlite3 (1.3.5)
- squeel (0.9.5)
+ squeel (1.0.1)
activerecord (~> 3.0)
activesupport (~> 3.0)
polyamorous (~> 0.5.0)
- tzinfo (0.3.31)
+ tzinfo (0.3.33)
yard (0.6.8)
PLATFORMS
@@ -46,10 +46,10 @@ PLATFORMS
DEPENDENCIES
activerecord (>= 3.2.0)
- bundler (~> 1.0.0)
+ bundler (~> 1.1.0)
jeweler (~> 1.6.4)
rake
rspec (~> 2.3.0)
sqlite3
- squeel
+ squeel (~> 1.0.0)
yard (~> 0.6.0)
View
46 lib/rocket_tag/taggable.rb
@@ -183,6 +183,50 @@ def tagged_with tags_list, options = {}
end
+ # Generates a query that returns list of popular tags
+ # for given model with an extra column :tags_count.
+ def popular_tags options={}
+ context = options.delete :on
+ if context
+ if context.class == Array
+ contexts = context
+ else
+ contexts = [context]
+ end
+ else
+ contexts = []
+ end
+
+ conditions = contexts.map do |context|
+ squeel do
+ (taggings.context == my{context})
+ end
+ end
+
+ condition = conditions.inject do |s, t|
+ s | t
+ end
+ inner = RocketTag::Tag.select{count(~id).as(tags_count)}.
+ select("#{RocketTag::Tag.table_name}.*").
+ joins{taggings.outer}.where{condition}.
+ where{ taggings.taggable_type == my{self.to_s} }.
+ group{~id} #.
+ #order("tags_count DESC")
+ r = from("(#{inner.to_sql}) #{table_name}")
+
+ if min = options.delete(:min)
+ r = r.where{tags_count>=my{min}}
+ end
+
+ if options.delete :sifter
+ squeel do
+ id.in(r.select{"id"})
+ end
+ else
+ r.select("*")
+ end
+ end
+
def setup_for_rocket_tag
unless @setup_for_rocket_tag
@setup_for_rocket_tag = true
@@ -284,8 +328,6 @@ def attr_taggable *contexts
end
end
end
-
end
-
end
end
View
17 spec/rocket_tag/taggable_spec.rb
@@ -322,8 +322,21 @@
end
end
-
+
+ describe "#popular_tags" do
+ it "should return correct list (and correctly ordered) of popular tags for class and context" do
+ TaggableModel.popular_tags.all.length.should == RocketTag::Tag.all.count
+ TaggableModel.popular_tags.limit(10).all.length.should == 10
+ TaggableModel.popular_tags.order('tags_count desc, name desc').first.name.should == 'c'
+ TaggableModel.popular_tags.order('id asc').first.name.should == 'a'
+ TaggableModel.popular_tags.order('id asc').last.name.should == 'jinglish'
+ TaggableModel.popular_tags(:on=>:skills).order('name asc').first.name.should == 'a'
+ TaggableModel.popular_tags(:on=>:skills).order('name asc').last.name.should == 'y'
+ TaggableModel.popular_tags(:on=>[:skills, :languages]).order('id asc').first.name.should == 'a'
+ TaggableModel.popular_tags(:on=>[:skills, :languages]).order('id asc').last.name.should == 'jinglish'
+ TaggableModel.popular_tags(:min=>2).all.length.should == 6 ## dirty!
+ end
+ end
end
end
end
-
Please sign in to comment.
Something went wrong with that request. Please try again.