From 46614eb91ba467439eba7726039ef5f05da1112a Mon Sep 17 00:00:00 2001 From: abdelkader boudih Date: Fri, 3 Jan 2014 11:49:54 +0000 Subject: [PATCH] Check for #390 to prevent application breakage. --- lib/acts_as_taggable_on/tagging.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/acts_as_taggable_on/tagging.rb b/lib/acts_as_taggable_on/tagging.rb index 8460993fc..71ada1784 100644 --- a/lib/acts_as_taggable_on/tagging.rb +++ b/lib/acts_as_taggable_on/tagging.rb @@ -9,7 +9,6 @@ class Tagging < ::ActiveRecord::Base #:nodoc: :tagger, :tagger_type, :tagger_id if defined?(ActiveModel::MassAssignmentSecurity) - belongs_to :tag, :class_name => 'ActsAsTaggableOn::Tag' belongs_to :taggable, :polymorphic => true belongs_to :tagger, :polymorphic => true @@ -17,34 +16,29 @@ class Tagging < ::ActiveRecord::Base #:nodoc: validates_presence_of :context validates_presence_of :tag_id - validates_uniqueness_of :tag_id, :scope => [ :taggable_type, :taggable_id, :context, :tagger_id, :tagger_type ] + validates_uniqueness_of :tag_id, :scope => [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type] after_destroy :remove_unused_tags # Conditionally adds a counter cache when cache column is present. - # We just regenerate the association. It's the easiest way. + # We just regenerate the association. It's the easiest way. # TODO: require the counter cache in release 4.0.0 and remove these methods # @see :columns in ActsAsTaggableOn::Taggable::Cache def self.columns @acts_as_taggable_on_counter_columns ||= begin db_columns = super - if _has_tags_counter_column?(db_columns) - belongs_to :tag, :class_name => 'ActsAsTaggableOn::Tag', :counter_cache => true - end + belongs_to :tag, :class_name => 'ActsAsTaggableOn::Tag', :counter_cache => ActsAsTaggableOn::Tag.column_names.include?('taggings_count') db_columns end end - # @private - def self._has_tags_counter_column?(db_columns) - db_column_names = db_columns.map(&:name) - db_column_names.include?("taggings_count") - end + private def remove_unused_tags if ActsAsTaggableOn.remove_unused_tags + # TODO: use taggings_count in release 4.0.0 if tag.taggings.count.zero? tag.destroy end