Module: LeanTag::Taggable::InstanceMethods

Defined in:
lib/lean_tag/taggable.rb

Instance Method Summary collapse

Instance Method Details

#_add_tag(tag, filter = "tags") ⇒ Object

Adds a single tag on parent save



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

def _add_tag(tag, filter="tags")
  if tag.is_a?(String)
    record = Tag.find_by_name(tag)
  end

  if record.nil?
    self._tags(filter).build(name: tag)
  elsif !self._taggings(filter).exists?(tag_id: record.id)
    self._taggings(filter).build(tag_id: record.id)
  end
end

#_add_tag!(tag, filter = "tags") ⇒ Object

Adds a single tag immediately



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

def _add_tag!(tag, filter="tags")
  self._add_tag(tag, filter)
  self.save!
end

#_get_tag_list(filter = "tags") ⇒ Object

Returns a delimited list of tag names



69
70
71
# File 'lib/lean_tag/taggable.rb', line 69

def _get_tag_list(filter="tags")
  self._taggings(filter).map(&:tag).map(&:name).join(',')
end

#_remove_tag(tag, filter = "tags") ⇒ Object

Removes a single tag on parent save



74
75
76
77
78
79
80
# File 'lib/lean_tag/taggable.rb', line 74

def _remove_tag(tag, filter="tags")
  if tag.is_a?(String)
    tag = Tag.find_by_name(tag)
  end

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

#_remove_tag!(tag, filter = "tags") ⇒ Object

Removes a single tag immediately



84
85
86
87
# File 'lib/lean_tag/taggable.rb', line 84

def _remove_tag!(tag, filter="tags")
  self._remove_tag(tag, filter)
  self.save!
end

#_set_tag_list(list, filter = "tags") ⇒ Object

Set a list of tags



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lean_tag/taggable.rb', line 91

def _set_tag_list(list, filter="tags")
  if list.is_a?(String)
    tag_names = list.split(LeanTag.config.delimiter)
  elsif list.is_a?(Array)
    tag_names = list
  else
    tag_names = []
  end

  tags = Tag.where(id: self._taggings(filter).select(:tag_id).distinct)
  tags.reject { |t| t.name.in?(tag_names) }.each { |t| self._remove_tag(t, filter) }

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

#_taggings(filter) ⇒ Object

Gets relevant taggings association



115
116
117
# File 'lib/lean_tag/taggable.rb', line 115

def _taggings(filter)
  self.send("#{filter}_taggings")
end

#_tags(filter) ⇒ Object

Gets relevant tag association



109
110
111
# File 'lib/lean_tag/taggable.rb', line 109

def _tags(filter)
  self.send(filter)
end