Sha256: 8d546e41f658e79afced75d19ac90afd1570de699c0f5e27cde0c852f7e9b0ce

Contents?: true

Size: 761 Bytes

Versions: 1

Compression:

Stored size: 761 Bytes

Contents

class Tag < ActiveRecord::Base
  class << self
    def find_or_initialize_with_name_like_and_kind(name, kind)
      name = collapse_whitespace(name)
      with_name_like_and_kind(name, kind).first || new(:name => name, :kind => kind)
    end
  end

  has_many :taggings, :dependent => :destroy

  validates_presence_of :name
  validates_uniqueness_of :name, :scope => :kind

  named_scope :with_name_like_and_kind, lambda { |name, kind| { :conditions => ["name like ? AND kind = ?", name, kind] } }
  named_scope :of_kind,                 lambda { |kind| { :conditions => {:kind => kind} } }
  
  def name=(name)
    super self.class.collapse_whitespace(name)
  end
  
protected
  
  def self.collapse_whitespace(name)
    name.gsub(/\s+/, ' ').strip
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
martinemde-is_taggable-0.1.3 lib/tag.rb