123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- module HasTags
- extend ActiveSupport::Concern
- included do
- before_destroy :tag_destroy
- end
- =begin
- add an tag to model
- model = Model.find(123)
- model.tag_add(name)
- =end
- def tag_add(name, current_user_id = nil, sourceable: nil)
- Tag.tag_add(
- object: self.class.to_s,
- o_id: id,
- item: name,
- created_by_id: current_user_id,
- sourceable: sourceable,
- )
- end
- =begin
- remove an tag of model
- model = Model.find(123)
- model.tag_remove(name)
- =end
- def tag_remove(name, current_user_id = nil, sourceable: nil)
- Tag.tag_remove(
- object: self.class.to_s,
- o_id: id,
- item: name,
- created_by_id: current_user_id,
- sourceable: sourceable,
- )
- end
- =begin
- update tags of model
- model = Model.find(123)
- model.tag_update(['name', 'tag'])
- =end
- def tag_update(items, current_user_id = nil)
- Tag.tag_update(
- object: self.class.to_s,
- o_id: id,
- items: items,
- created_by_id: current_user_id,
- )
- end
- =begin
- tag list of model
- model = Model.find(123)
- tags = model.tag_list
- =end
- def tag_list
- Tag.tag_list(
- object: self.class.to_s,
- o_id: id,
- )
- end
- =begin
- destroy all tags of an object
- model = Model.find(123)
- model.tag_destroy
- =end
- def tag_destroy(current_user_id = nil)
- Tag.tag_destroy(
- object: self.class.to_s,
- o_id: id,
- created_by_id: current_user_id,
- )
- true
- end
- class_methods do
- =begin
- Lists references to objects of this type with certain tag.
- Returns array containing object IDs.
- @param [String] tag name
- @example
- Model.tag_references('Tag') # [1, 4, ...]
- @return [Array<Integer>]
- =end
- def tag_references(tag)
- Tag
- .tag_references(tag: tag, object: name)
- .pluck(1)
- end
- =begin
- Lists objects of this type with certain tag
- @param [String] tag name
- @example
- Model.tag_objects('tag')
- @return [ActiveRecord::Relation]
- =end
- def tag_objects(tag)
- where id: tag_references(tag)
- end
- end
- end
|