Module: EasyTag::Taggable

Extended by:
ActiveSupport::Concern
Defined in:
lib/easy_tag/taggable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#is_taggable?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/easy_tag/taggable.rb', line 99

def is_taggable?
  return true
end

#set_tags(tag_list, options = {}) ⇒ Object

end of class methods



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/easy_tag/taggable.rb', line 67

def set_tags(tag_list, options = {})
  options.reverse_merge! :context => nil, 
                         :tagger => nil, 
                         :downcase => true,
                         :delimiter => ','

  if block_given?
    tags = yield(klass)
  else
    tags = EasyTag::Tag.compact_tag_list(tag_list, options.slice(:downcase, :delimiter))
  end

  context = compact_context(options[:context])
  tagger = compact_tagger(options[:tagger])

  # Remove old tags
  self.taggings.where(:tag_context_id => context.try(:id), :tagger_id => tagger.try(:id)).destroy_all
  # TODO: should remove unused tags and contexts

  if tags
    tags.each do |t|
      tag = EasyTag::Tag.where(:name => t).first_or_create
      raise SimgleTag::InvalidTag if tag.nil?
      self.taggings.where(:tagger_id => tagger.try(:id), :tag_context_id => context.try(:id), :tag_id => tag.id).first_or_create
    end
  end
end

#tags=(tag_list) ⇒ Object



95
96
97
# File 'lib/easy_tag/taggable.rb', line 95

def tags=(tag_list)
  self.set_tags(tag_list)
end