Sha256: 3f147d86595cc0d88eb1c476131a9d2b886bd07f1d8afdcc440dc6e9f943778e

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

class Gutentag::Tag < ActiveRecord::Base
  self.table_name = "gutentag_tags"

  has_many :taggings,
    :class_name => "Gutentag::Tagging",
    :dependent  => :destroy

  attr_accessible :name if ActiveRecord::VERSION::MAJOR == 3

  scope :by_weight, lambda { order("gutentag_tags.taggings_count DESC") }

  def self.find_by_name(name)
    where(:name => Gutentag.normaliser.call(name)).first
  end

  def self.find_or_create(name)
    find_by_name(name) || create(:name => name)
  end

  def self.names_for_scope(scope)
    join_conditions = {:taggable_type => scope.name}
    if scope.is_a?(ActiveRecord::Relation)
      return Gutentag::Tag.none unless scope.current_scope.present?

      join_conditions[:taggable_id] = scope.select(:id)
    end

    joins(:taggings).where(
      Gutentag::Tagging.table_name => join_conditions
    ).distinct.pluck(:name)
  end

  def name=(value)
    super(Gutentag.normaliser.call(value))
  end
end

require "gutentag/tag_validations"
Gutentag.tag_validations.call Gutentag::Tag

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gutentag-2.6.2 app/models/gutentag/tag.rb
gutentag-2.6.1 app/models/gutentag/tag.rb
gutentag-2.6.0 app/models/gutentag/tag.rb
gutentag-2.5.4 app/models/gutentag/tag.rb
gutentag-2.5.3 app/models/gutentag/tag.rb