Module: Skyline::Taggable

Defined in:
lib/skyline/taggable.rb

Overview

Usage: class Model < ActiveRecord::Base

include Skyline::Taggable

end

1) Registers your model in the Skyline::Tag.taggable_models list

2) Gives your Model the following interface:

class  Model < ActiveRecord::Base
  has_many :associated_tags, :class_name => "Skyline::AssociatedTag", :as => "taggable"
  has_many :tags, :through => :associated_tags, :class_name => "Skyline::Tag"    

  scope :with_tags (tags)      (scope to return items that have at least one of the tags supplied; or all if no tags are passed)

  def raw_tags 
  def raw_tags=(str)
  def available_tags
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#available_tagsObject



59
60
61
# File 'lib/skyline/taggable.rb', line 59

def available_tags
  self.class.available_tags
end

#dup_with_associated_tagsObject



63
64
65
66
67
# File 'lib/skyline/taggable.rb', line 63

def dup_with_associated_tags
  dup_without_associated_tags.tap{|dup|
    dup.associated_tags = self.associated_tags.collect{|at| at.dup }
  }
end

#raw_tagsObject



45
46
47
# File 'lib/skyline/taggable.rb', line 45

def raw_tags
  self.tags.collect{|t| t.tag}.join(" ")
end

#raw_tags=(str) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/skyline/taggable.rb', line 49

def raw_tags=(str)
  self.tags.clear
  if str.present?
    str.split.each do |t|
      tag = Skyline::Tag.find_or_create_by_taggable_type_and_tag(self.tag_taggable_type, t)
      self.tags << tag unless self.tags.include?(tag)
    end
  end
end