Module: LeanTag::Taggable

Defined in:
lib/lean_tag/taggable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/lean_tag/taggable.rb', line 4

def self.included(base)
  base.class_eval do
    has_many :taggings, class_name: "LeanTag::Tagging", as: :record, inverse_of: :record, dependent: :destroy
    has_many :tags, through: :taggings

    accepts_nested_attributes_for :taggings, allow_destroy: true

    scope :with_tags, -> { includes(:tags) }
  end
end

Instance Method Details

#add_tag(tag) ⇒ Object

Adds a single tag on parent save



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lean_tag/taggable.rb', line 17

def add_tag(tag)
  if tag.is_a?(String)
    tag_name = tag
    tag = Tag.find_by_name(tag_name)

    if tag.nil?
      self.tags.build(name: tag_name)
    elsif !self.taggings.exists?(tag_id: tag.id)
      self.taggings.build(tag_id: tag.id)
    end
  else
    self.taggings.build(tag_id: tag.id)
  end
end

#add_tag!(tag) ⇒ Object

Adds a single tag immediately



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lean_tag/taggable.rb', line 34

def add_tag!(tag)
   if tag.is_a?(String)
    tag_name = tag
    tag = Tag.find_by_name(tag_name)

    if tag.nil?
      self.tags.create(name: tag_name)
    elsif !self.taggings.exists?(tag_id: tag.id)
      self.taggings.create(tag_id: tag.id)
    end
  else
    self.taggings.create(tag_id: tag.id)
  end
end

#excluded_tags(tag_names) ⇒ Object

Finds current tags on this record which aren’t in the passed list



51
52
53
# File 'lib/lean_tag/taggable.rb', line 51

def excluded_tags(tag_names)
  self.tags.reject { |t| t.name.in?(tag_names) }
end

#included_tags(tag_names) ⇒ Object

Finds current tags on this record which are in the passed list



57
58
59
# File 'lib/lean_tag/taggable.rb', line 57

def included_tags(tag_names)
  self.tags.select { |t| t.name.in?(tag_names) }
end

#remove_tag(tag) ⇒ Object

Removes a single tag on parent save



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

def remove_tag(tag)
  tag = self.tags.where(name: tag).first if tag.is_a?(String)

  self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
end

#remove_tag!(tag) ⇒ Object

Removes a single tag immediately



71
72
73
74
75
# File 'lib/lean_tag/taggable.rb', line 71

def remove_tag!(tag)
  tag = self.tags.where(name: tag).first if tag.is_a?(String)

  self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
end

#tag_listObject

Returns a delimited list of tag names



97
98
99
# File 'lib/lean_tag/taggable.rb', line 97

def tag_list
  self.tags.map(&:name).join(',')
end

#tag_list=(value) ⇒ Object

Set a list of tags



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lean_tag/taggable.rb', line 79

def tag_list=(value)
  if value.is_a?(String)
    tag_names = value.split(LeanTag.config.delimiter)
  elsif value.is_a?(Array)
    tag_names = value
  else
    tag_names = []
  end

  # Get rid of existing tags that aren't in the list
  self.excluded_tags(tag_names).each { |t| self.remove_tag(t) }

  # Add any new tags
  tag_names.each { |t| self.add_tag(t) }
end