Module: LeanTag::Taggable::InstanceMethods
- Defined in:
- lib/lean_tag/taggable.rb
Instance Method Summary collapse
-
#_add_tag(tag, filter = "tags") ⇒ Object
Adds a single tag on parent save.
-
#_add_tag!(tag, filter = "tags") ⇒ Object
Adds a single tag immediately.
-
#_get_tag_list(filter = "tags") ⇒ Object
Returns a delimited list of tag names.
-
#_remove_tag(tag, filter = "tags") ⇒ Object
Removes a single tag on parent save.
-
#_remove_tag!(tag, filter = "tags") ⇒ Object
Removes a single tag immediately.
-
#_set_tag_list(list, filter = "tags") ⇒ Object
Set a list of tags.
-
#_taggings(filter) ⇒ Object
Gets relevant taggings association.
-
#_tags(filter) ⇒ Object
Gets relevant tag association.
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 |
# File 'lib/lean_tag/taggable.rb', line 57 def _add_tag(tag, filter="tags") if tag.is_a?(String) record = Tag.find_or_create_by(name: tag) end unless 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
69 70 71 72 |
# File 'lib/lean_tag/taggable.rb', line 69 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
76 77 78 |
# File 'lib/lean_tag/taggable.rb', line 76 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
81 82 83 84 85 86 87 |
# File 'lib/lean_tag/taggable.rb', line 81 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
91 92 93 94 |
# File 'lib/lean_tag/taggable.rb', line 91 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lean_tag/taggable.rb', line 98 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 = self._taggings(filter).map(&:tag) .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
123 124 125 |
# File 'lib/lean_tag/taggable.rb', line 123 def _taggings(filter) self.send("#{filter}_taggings") end |
#_tags(filter) ⇒ Object
Gets relevant tag association
117 118 119 |
# File 'lib/lean_tag/taggable.rb', line 117 def (filter) self.send(filter) end |