Skip to content

Commit

Permalink
Allows a forced call to agregate tags even if the agregate is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos King authored and ches committed Aug 26, 2011
1 parent 173c7e2 commit b745cc0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/mongoid/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ module Mongoid::Taggable

delegate :convert_string_tags_to_array, :aggregate_tags!, :to => 'self.class'

set_callback :create, :after, :aggregate_tags!
set_callback :destroy, :after, :aggregate_tags!
set_callback :create, :after, :aggregate_tags!, :if => proc { self.class.aggregate_tags? }
set_callback :destroy, :after, :aggregate_tags!, :if => proc { self.class.aggregate_tags? }
set_callback :save, :before, :dedup_tags!, :if => proc { changes.include?(tags_field.to_s) }
set_callback :save, :after, :aggregate_tags!, :if => proc { changes.include?(tags_field.to_s) }
set_callback :save, :after, :aggregate_tags!, :if => proc { changes.include?(tags_field.to_s) and self.class.aggregate_tags? }
end

module ClassMethods
Expand Down Expand Up @@ -90,8 +90,6 @@ def tags_aggregation_collection
# Execute map/reduce operation to aggregate tag counts for document
# class
def aggregate_tags!
return unless aggregate_tags?

map = "function() {
if (!this.#{tags_field}) {
return;
Expand Down
6 changes: 6 additions & 0 deletions spec/mongoid/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ class Template
MyModel.tags.should == []
end

it "can be forced" do
MyModel.create!(:tags => "sample,tags")
MyModel.aggregate_tags!
MyModel.tags.should == %w[sample tags]
end

context "when enabled" do
before :all do
MyModel.tag_aggregation = true
Expand Down

0 comments on commit b745cc0

Please sign in to comment.