Updated

app/models/concerns / taggable.rb

B
54 lines of codes
10 methods
5.5 complexity/method
2 churn
54.58 complexity
0 duplications
require 'active_support/concern' module Taggable
  1. Taggable has no descriptive comment
extend ActiveSupport::Concern included do validate :tags_exist end class_methods do def tagged_with_all(*tags) where(tags_column.contains(normalize_tags(tags))) end alias_method :tagged_with, :tagged_with_all def tagged_with_any(*tags) where(tags_column.overlaps(normalize_tags(tags))) end def tagged where(tags_column.not_eq([])) end def untagged where(tags_column.eq([])) end def tags_column arel_table[:tags] end def normalize_tags(tags) Array(tags).flatten.map(&:to_i).compact.reject(&:zero?) end end def normalize_tags(tags) self.class.normalize_tags(tags) end def tags=(tags) super(normalize_tags(tags)) end def tag_names Tag.where(id: tags).pluck(:name) end def tags_exist unless tags.all? { |tag| Tag.exists?(tag) } errors.add :tags, :invalid end end end