Sha256: bca5c7e4e3cb550178fef085c9f97846220cdc03213de800dcd675f2f54779db

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module ActsAsTaggableOn
  class Tagging < ::ActiveRecord::Base #:nodoc:
    self.table_name = ActsAsTaggableOn.taggings_table

    DEFAULT_CONTEXT = 'tags'
    belongs_to :tag, class_name: '::ActsAsTaggableOn::Tag', counter_cache: ActsAsTaggableOn.tags_counter
    belongs_to :taggable, polymorphic: true

    belongs_to :tagger, polymorphic: true, optional: true

    scope :owned_by, ->(owner) { where(tagger: owner) }
    scope :not_owned, -> { where(tagger_id: nil, tagger_type: nil) }

    scope :by_contexts, ->(contexts) { where(context: (contexts || DEFAULT_CONTEXT)) }
    scope :by_context, ->(context = DEFAULT_CONTEXT) { by_contexts(context.to_s) }

    scope :by_tenant, ->(tenant) { where(tenant: tenant) }

    validates_presence_of :context
    validates_presence_of :tag_id

    validates_uniqueness_of :tag_id, scope: [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type]

    after_destroy :remove_unused_tags

    private

    def remove_unused_tags
      if ActsAsTaggableOn.remove_unused_tags
        if ActsAsTaggableOn.tags_counter
          tag.destroy if tag.reload.taggings_count.zero?
        else
          tag.destroy if tag.reload.taggings.none?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
acts-as-taggable-on-fix-8.1.1 lib/acts_as_taggable_on/tagging.rb
acts-as-taggable-on-fix-8.1.0 lib/acts_as_taggable_on/tagging.rb
acts-as-taggable-on-8.1.0 lib/acts_as_taggable_on/tagging.rb
acts-as-taggable-on-8.0.0 lib/acts_as_taggable_on/tagging.rb