Increases/decreases custom counters when taggings are created/destroyed

This commit is contained in:
kikito
2015-09-17 12:50:14 +02:00
parent 7dbda5d747
commit fedefa215f
2 changed files with 65 additions and 2 deletions

View File

@@ -1,8 +1,24 @@
ActsAsTaggableOn::Tagging.class_eval do
after_destroy :touch_taggable
after_create :increase_custom_counter
after_destroy :touch_taggable, :decrease_custom_counter
def touch_taggable
taggable.touch if taggable.present?
end
end
def increase_custom_counter
ActsAsTaggableOn::Tag.increment_counter(custom_counter_field_name, tag_id)
end
def decrease_custom_counter
ActsAsTaggableOn::Tag.decrement_counter(custom_counter_field_name, tag_id)
end
private
def custom_counter_field_name
"#{self.taggable_type.underscore.pluralize}_count"
end
end