Sha256: 4466e332999837f6fe9d8077f761e1309c5064d80aff5515931dd15c8d42cb12

Contents?: true

Size: 865 Bytes

Versions: 1

Compression:

Stored size: 865 Bytes

Contents

class Tag < ActiveRecord::Base
  has_many :taggings
  
  validates_presence_of :name
  validates_uniqueness_of :name

  # Find all tags for a specific context.
  # USAGE: Tag.for_context('collection_tags') #=> Would return an array of unique tags used in the specified context
  named_scope :for_context, lambda { |context| { :select => 'DISTINCT `tags`.`id`, `tags`.`name`', :joins => 'INNER JOIN taggings ON `taggings`.`tag_id` = `tags`.`id`', :conditions => ['`taggings`.`context` = ?', context] } }
  
  # LIKE is used for cross-database case-insensitivity
  def self.find_or_create_with_like_by_name(name)
    find(:first, :conditions => ["name LIKE ?", name]) || create(:name => name)
  end
  
  def ==(object)
    super || (object.is_a?(Tag) && name == object.name)
  end
  
  def to_s
    name
  end
  
  def count
    read_attribute(:count).to_i
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts-as-taggable-on-for-domains-0.1.1 lib/acts_as_taggable_on/tag.rb