Browse files

TaggableModel.popular_tags method + Gemfile updated

  • Loading branch information...
1 parent 0d85ea6 commit 60386e987312c738f0ff894208306a9ae1eda838 @mlitwiniuk mlitwiniuk committed May 5, 2012
Showing with 58 additions and 18 deletions.
  1. +2 −2 Gemfile
  2. +13 −13 Gemfile.lock
  3. +30 −2 lib/rocket_tag/taggable.rb
  4. +13 −1 spec/rocket_tag/taggable_spec.rb
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,22 +34,22 @@ 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
ruby
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
32 lib/rocket_tag/taggable.rb
@@ -183,6 +183,36 @@ def tagged_with tags_list, options = {}
end
+ 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
+
+ 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")
+ end
+
def setup_for_rocket_tag
unless @setup_for_rocket_tag
@setup_for_rocket_tag = true
@@ -284,8 +314,6 @@ def attr_taggable *contexts
end
end
end
-
end
-
end
end
View
14 spec/rocket_tag/taggable_spec.rb
@@ -317,7 +317,19 @@
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('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'
+ end
+ end
end
end
end

0 comments on commit 60386e9

Please sign in to comment.