1require 'active_support/concern'
 
2
 
3module Taggable
 
4  extend ActiveSupport::Concern
 
5
 
6  included do
 
7    validate :tags_exist
 
8  end
 
 9
 
10  class_methods do
  • Complexity 1 » saikuro
11    def tagged_with_all(*tags)
 
12      where(tags_column.contains(normalize_tags(tags)))
 
13    end
 
14    alias_method :tagged_with, :tagged_with_all
 
  • Complexity 1 » saikuro
16    def tagged_with_any(*tags)
 
17      where(tags_column.overlaps(normalize_tags(tags)))
 
18    end
 
  • Complexity 1 » saikuro
20    def tagged
 
21      where(tags_column.not_eq([]))
 
22    end
 
  • Complexity 1 » saikuro
24    def untagged
 
25      where(tags_column.eq([]))
 
26    end
 
  • Complexity 1 » saikuro
28    def tags_column
 
29      arel_table[:tags]
 
30    end
 
 
32    def normalize_tags(tags)
 
33      Array(tags).flatten.map(&:to_i).compact.reject(&:zero?)
 
34    end
 
35  end
 
  • Complexity 1 » saikuro
  • Complexity 1 » saikuro
37  def normalize_tags(tags)
 
38    self.class.normalize_tags(tags)
 
39  end
 
  • Complexity 1 » saikuro
41  def tags=(tags)
 
42    super(normalize_tags(tags))
 
43  end
 
  • Complexity 1 » saikuro
45  def tag_names
 
46    Tag.where(id: tags).pluck(:name)
 
47  end
 
  • Complexity 3 » saikuro
49  def tags_exist
 
50    unless tags.all? { |tag| Tag.exists?(tag) }
 
51      errors.add :tags, :invalid
 
52    end
 
53  end
 
54end