Module: MongoidExt::Tags::ClassMethods
- Defined in:
- lib/mongoid_ext/tags.rb
Instance Method Summary collapse
- #find_tags(regex, conditions = {}, limit = 30) ⇒ Object
-
#find_with_tags(*tags) ⇒ Object
Model.find_with_tags(“budget”, “big”).limit(4).
- #tag_cloud(conditions = {}, limit = 30) ⇒ Object
Instance Method Details
#find_tags(regex, conditions = {}, limit = 30) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid_ext/tags.rb', line 34 def (regex, conditions = {}, limit = 30) 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 |
#find_with_tags(*tags) ⇒ Object
Model.find_with_tags(“budget”, “big”).limit(4)
30 31 32 |
# File 'lib/mongoid_ext/tags.rb', line 30 def (*) self.where({:tags.in => }) end |
#tag_cloud(conditions = {}, limit = 30) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mongoid_ext/tags.rb', line 13 def tag_cloud(conditions = {}, limit = 30) 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 |