Module: Insights::API::Common::ActAsTaggableOn

Defined in:
app/models/concerns/insights/api/common/act_as_taggable_on.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_taggable_onObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/concerns/insights/api/common/act_as_taggable_on.rb', line 5

def acts_as_taggable_on
  class_eval do
    def self.tagging_relation_name
      "#{name.underscore}_tags".to_sym
    end

    has_many tagging_relation_name
    has_many :tags, :through => tagging_relation_name

    def self.taggable?
      true
    end

    def tag_list
      tags.pluck(:name)
    end

    def tag_add(tag_list, options = {})
      Tag.transaction do
        model_tag_class.transaction do
          Array(tag_list).each do |tag_name|
            next if tagged_with?(tag_name, options)
            tag_params = {:name => tag_name, :tenant_id => tenant.id}
            tag_params.merge!(options)
            tag = Tag.find_or_create_by(tag_params)
            tagging_params = {self.class.name.underscore.to_sym => self, :tag_id => tag.id}
            public_send(self.class.tagging_relation_name).create(tagging_params)
          end
        end
      end
    end

    def tagged_with?(tag_name, options = {})
      options[:value] ||= ""
      options[:namespace] ||= ""
      model_tag_class.joins(:tag)
                     .exists?(:tags => {:name => tag_name, :namespace => options[:namespace], :value => options[:value]}, self.class.name.underscore.to_sym => self)
    end

    def tag_remove(tag_list, options = {})
      options[:value] ||= ""
      options[:namespace] ||= ""
      Tag.joins(self.class.tagging_relation_name)
         .where(:name => Array(tag_list), :namespace => options[:namespace], :value => options[:value], self.class.tagging_relation_name => {self.class.name.underscore.to_sym => self})
         .destroy_all
    end

    def model_tag_class
      self.class.tagging_relation_name.to_s.classify.constantize
    end
  end
end

#tagged_with(tag_name, options = {}) ⇒ Object



58
59
60
61
62
# File 'app/models/concerns/insights/api/common/act_as_taggable_on.rb', line 58

def tagged_with(tag_name, options = {})
  options[:value] ||= ""
  options[:namespace] ||= ""
  joins(tagging_relation_name => :tag).where(:tags => {:name => tag_name, :namespace => options[:namespace], :value => options[:value]})
end