Module: TagAble

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/tag_able.rb

Overview

Public: Support taggable entities

Instance Method Summary collapse

Instance Method Details

#sync_tags(updated_tags) ⇒ Object

This method is abstract.

Update the tags associated with the model

First update what is from the updated tags Then delete any that are no longer associated with them

Parameters:

  • updated_tags

    Array<Types::Tag> - tags to update with



20
21
22
23
24
25
26
27
# File 'lib/app/models/concerns/tag_able.rb', line 20

def sync_tags(updated_tags)
  updated_tags.each do |t|
    tag = tags.where(key: t.key).first_or_initialize(key: t.key, model: self)
    tag.update! value: t.value
  end
  keys = updated_tags.map(&:key)
  tags.each { |t| t.destroy unless keys.include?(t.key) }
end

#tag_value(key, default = nil) ⇒ Object



29
30
31
32
33
# File 'lib/app/models/concerns/tag_able.rb', line 29

def tag_value(key, default = nil)
  tags.find_by(key: key.to_s).value
rescue StandardError
  default
end