Module: MongoMapper::Plugins::Taggregator::ClassMethods
- Defined in:
- lib/taggregator.rb
Instance Method Summary collapse
-
#aggregation_collection_for(context) ⇒ Object
Collection name for storing results of tag count aggregation.
- #convert_array_to_string(ary = [], seperator = " ") ⇒ Object
-
#convert_string_to_array(str = "", seperator = " ") ⇒ Object
Helper method to convert a String to an Array based on the configured tag separator.
-
#find_similar(article, options = {:through => :tags}) ⇒ Object
an array returned with similar items.
- #get_tag_separator_for(context) ⇒ Object
- #set_tag_separator_for(context, value) ⇒ Object
- #tag_contexts ⇒ Object
- #tag_options_for(context) ⇒ Object
-
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.
-
#tagged_with(context, tags) ⇒ Criteria
Find documents tagged with all tags passed as a parameter, given as an Array or a String using the configured separator.
- #tags_for(context, conditions = {}) ⇒ Object
-
#tags_with_weight_for(context, conditions = {}) ⇒ Object
retrieve the list of tag with weight(count), this is useful for creating tag clouds.
Instance Method Details
#aggregation_collection_for(context) ⇒ Object
Collection name for storing results of tag count aggregation
172 173 174 |
# File 'lib/taggregator.rb', line 172 def aggregation_collection_for(context) "#{collection_name}_#{context}_aggregation" end |
#convert_array_to_string(ary = [], seperator = " ") ⇒ Object
233 234 235 |
# File 'lib/taggregator.rb', line 233 def convert_array_to_string(ary = [], seperator = " ") ary.uniq.compact.join(seperator) end |
#convert_string_to_array(str = "", seperator = " ") ⇒ Object
Helper method to convert a String to an Array based on the configured tag separator.
229 230 231 |
# File 'lib/taggregator.rb', line 229 def convert_string_to_array(str = "", seperator = " ") str.split(seperator).map(&:strip).uniq.compact end |
#find_similar(article, options = {:through => :tags}) ⇒ Object
an array returned with similar items
220 221 222 223 224 225 |
# File 'lib/taggregator.rb', line 220 def find_similar(article, ={:through => :tags}) context = [:through] array_field = ([:through])[:array_field] = article[array_field] where(:"#{array_field}".in => ) end |
#get_tag_separator_for(context) ⇒ Object
190 191 192 |
# File 'lib/taggregator.rb', line 190 def get_tag_separator_for(context) [context][:separator] || ' ' end |
#set_tag_separator_for(context, value) ⇒ Object
194 195 196 |
# File 'lib/taggregator.rb', line 194 def set_tag_separator_for(context, value) [context][:separator] = value.nil? ? " " : value.to_s end |
#tag_contexts ⇒ Object
163 164 165 |
# File 'lib/taggregator.rb', line 163 def tag_contexts .keys end |
#tag_options_for(context) ⇒ Object
167 168 169 |
# File 'lib/taggregator.rb', line 167 def (context) [context] end |
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/taggregator.rb', line 43 def taggable(*args) # init variables = args. = (args.blank? ? :tags : args.shift).to_sym .reverse_merge!( :separator => ' ', :array_field => "#{}_array".to_sym ) = [:array_field] # register / update settings = self. || {} [] = self. = # setup fields & indexes key .to_sym, String, :default => "" key .to_sym, Array, :default => [] self.ensure_index .to_sym # singleton methods class_eval <<-END class << self def #{} tags_for(:"#{}") end def #{}_with_weight tags_with_weight_for(:"#{}") end def #{}_separator get_tag_separator_for(:"#{}") end def #{}_separator=(value) set_tag_separator_for(:"#{}", value) end def #{}_tagged_with(tags) tagged_with(:"#{}", tags) end end END # instance methods class_eval <<-END def #{}=(s) super write_attribute(:#{}, self.convert_string_to_array(s, get_tag_separator_for(:"#{}"))) end def #{}=(a) super write_attribute(:#{}, self.convert_array_to_string(a, get_tag_separator_for(:"#{}"))) end def increment_tags_agregation # if document is created by using MyDocument.new # and attributes are individually assigned # #previous_changes won't be empty and aggregation # is updated in after_save, so we simply skip it. return unless changes.empty? # if the document is created by using MyDocument.create(:tags => "tag1 tag2") # #previous_changes hash is empty and we have to update aggregation here tag_contexts.each do |context| coll = MongoMapper.database.collection(self.class.aggregation_collection_for(context)) field_name = self.class.tag_options_for(context)[:array_field] tags = self.send field_name || [] tags.each do |t| coll.update({:_id => t}, {'$inc' => {:value => 1}}, :upsert => true) end end end def decrement_tags_aggregation tag_contexts.each do |context| coll = MongoMapper.database.collection(self.class.aggregation_collection_for(context)) field_name = self.class.tag_options_for(context)[:array_field] tags = self.send field_name || [] tags.each do |t| coll.update({:_id => t}, {'$inc' => {:value => -1}}, :upsert => true) end end end def update_tags_aggregation return unless self.need_update_tags_aggregation? changed_contexts.each do |context| coll = MongoMapper.database.collection(self.class.aggregation_collection_for(context)) field_name = self.class.tag_options_for(context)[:array_field] old_tags, new_tags = changes[field_name] old_tags ||= [] new_tags ||= [] unchanged_tags = old_tags & new_tags tags_removed = old_tags - unchanged_tags tags_added = new_tags - unchanged_tags tags_removed.each do |t| coll.update({:_id => t}, {'$inc' => {:value => -1}}, :upsert => true) end tags_added.each do |t| coll.update({:_id => t}, {'$inc' => {:value => 1}}, :upsert => true) end end end def need_update_tags_aggregation? !changed_contexts.empty? end def changed_contexts tag_contexts & changes.keys.map(&:to_sym) end END end |
#tagged_with(context, tags) ⇒ Criteria
Find documents tagged with all tags passed as a parameter, given as an Array or a String using the configured separator.
209 210 211 212 213 |
# File 'lib/taggregator.rb', line 209 def tagged_with(context, ) = convert_string_to_array(, get_tag_separator_for(context)) if .is_a? String array_field = (context)[:array_field] where(:"#{array_field}".in => ) end |
#tags_for(context, conditions = {}) ⇒ Object
176 177 178 179 |
# File 'lib/taggregator.rb', line 176 def (context, conditions={}) conditions = {:sort => '_id'}.merge(conditions) MongoMapper.database.collection(aggregation_collection_for(context)).find({:value => {"$gt" => 0 }}, conditions).to_a.map{ |t| t["_id"] } end |
#tags_with_weight_for(context, conditions = {}) ⇒ Object
retrieve the list of tag with weight(count), this is useful for creating tag clouds
183 184 185 186 187 188 |
# File 'lib/taggregator.rb', line 183 def (context, conditions={}) conditions = {:sort => '_id'}.merge(conditions) MongoMapper.database.collection(aggregation_collection_for(context)).find({:value => {"$gt" => 0 }}, conditions). to_a.map{ |t| [t["_id"], t["value"]] }. sort{ |a,b| b[1] <=> a[1] } end |