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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lean_tag/taggable.rb', line 57

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



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

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



78
79
80
# File 'lib/lean_tag/taggable.rb', line 78

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



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

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



93
94
95
96
# File 'lib/lean_tag/taggable.rb', line 93

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lean_tag/taggable.rb', line 100

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



124
125
126
# File 'lib/lean_tag/taggable.rb', line 124

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

#_tags(filter) ⇒ Object

Gets relevant tag association



118
119
120
# File 'lib/lean_tag/taggable.rb', line 118

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