Skip to content

Commit

Permalink
Implements MongoidExt::Tags module using mongodb's aggregation framework
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Cuadrado <kuadrosxx@gmail.com>
  • Loading branch information
kuadrosx committed Mar 6, 2013
1 parent de5fa74 commit ea4337c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
32 changes: 16 additions & 16 deletions Gemfile.lock
@@ -1,18 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.7)
activesupport (= 3.2.7)
activemodel (3.2.12)
activesupport (= 3.2.12)
builder (~> 3.0.0)
activesupport (3.2.7)
activesupport (3.2.12)
i18n (~> 0.6)
multi_json (~> 1.0)
builder (3.0.0)
builder (3.0.4)
coderay (1.0.7)
differ (0.1.2)
encryptor (1.1.3)
git (1.2.5)
i18n (0.6.0)
i18n (0.6.4)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
Expand All @@ -22,20 +22,20 @@ GEM
json (1.7.4)
metaclass (0.0.1)
method_source (0.8)
mime-types (1.19)
mime-types (1.21)
mocha (0.11.4)
metaclass (~> 0.0.1)
mongoid (3.0.2)
activemodel (~> 3.1)
moped (~> 1.1.3)
origin (~> 1.0.3)
mongoid (3.1.2)
activemodel (~> 3.2)
moped (~> 1.4.2)
origin (~> 1.0)
tzinfo (~> 0.3.22)
mongoid-grid_fs (1.3.1)
mongoid-grid_fs (1.7.0)
mime-types (~> 1.19)
mongoid (~> 3.0.1)
moped (1.1.5)
multi_json (1.3.6)
origin (1.0.4)
mongoid (~> 3.0)
moped (1.4.3)
multi_json (1.6.1)
origin (1.0.11)
pry (0.9.10)
coderay (~> 1.0.5)
method_source (~> 0.8)
Expand All @@ -47,7 +47,7 @@ GEM
shoulda (2.11.3)
slop (3.3.2)
timecop (0.4.1)
tzinfo (0.3.33)
tzinfo (0.3.36)
uuidtools (2.1.3)
yard (0.6.8)

Expand Down
40 changes: 32 additions & 8 deletions lib/mongoid_ext/tags.rb
Expand Up @@ -11,10 +11,19 @@ def self.included(klass)

module ClassMethods
def tag_cloud(conditions = {}, limit = 30)
Mongoid.session(:default).command({
:eval => "function(collection, q,l) { return tag_cloud(collection, q,l); }",
:args => [self.collection_name, conditions, limit]
})['retval']
pipeline = []
if !conditions.blank?
match = {:$match => conditions }
pipeline << match
end

pipeline << {:$project => {:tags => 1}}
pipeline << {:$unwind => "$tags"}
pipeline << {:$group => {:_id => "$tags", :count => { :$sum => 1}}}
pipeline << {:$project => {:_id => 0, :name => '$_id', :count => 1}}
pipeline << {:$sort => {:count => -1}}
pipeline << {:$limit => limit}
self.collection.aggregate(pipeline)
end

# Model.find_with_tags("budget", "big").limit(4)
Expand All @@ -23,10 +32,25 @@ def find_with_tags(*tags)
end

def find_tags(regex, conditions = {}, limit = 30)
Mongoid.session(:default).command({
:eval => "function(collection, a,b,c) { return find_tags(collection, a,b,c); }",
:args => [self.collection_name, regex, conditions, limit]
})['retval']
pipeline = []
if regex.is_a? String
regex = /#{Regexp.escape(regex)}/
end
match = {:$match => {:tags => {:$in => [regex]}}}

if !conditions.blank?
match[:$match].merge! conditions
end
pipeline << match

pipeline << {:$project => {:tags => 1}}
pipeline << {:$unwind => "$tags"}
pipeline << {:$match => {:tags => regex}}
pipeline << {:$group => {:_id => "$tags", :count => { :$sum => 1}}}
pipeline << {:$project => {:_id => 0, :name => '$_id', :count => 1}}
pipeline << {:$sort => {:count => -1}}
pipeline << {:$limit => limit}
self.collection.aggregate(pipeline)
end
end
end
Expand Down

0 comments on commit ea4337c

Please sign in to comment.