Module: Tagtical::Taggable::Core

Defined in:
lib/tagtical/taggable/core.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
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
# File 'lib/tagtical/taggable/core.rb', line 3

def self.included(base)
  base.class_eval do
    include Tagtical::Taggable::Core::InstanceMethods
    extend Tagtical::Taggable::Core::ClassMethods

    after_save :save_tags

    initialize_tagtical_core

    # Handy scope for filtering records, which *doesn't* have associated tags
    scope :filter_by_empty_tags, lambda { |empty_tags_type_names|
      empty_tag_types = tag_types.select do |tag_type|
        Array.wrap(empty_tags_type_names).map(&:to_s).include?(tag_type)
      end
      empty_tag_type_classes = empty_tag_types.map { |t| t.klass.to_s }.to_a
      if empty_tag_type_classes.present?
        tagging_table = Tagtical::Tagging.arel_table
        tag_table = Tagtical::Tag.arel_table
        joins(
          arel_table.
            join(tagging_table, Arel::Nodes::OuterJoin).
              on(arel_table[:id].eq(tagging_table[:taggable_id]).and(tagging_table[:taggable_type].eq(name))).
            join(tag_table, Arel::Nodes::OuterJoin).
              on(tag_table[:id].eq(tagging_table[:tag_id]).and(tag_table[:type].in(empty_tag_type_classes))).
            join_sql
        ).
          group("#{table_name}.id").
          having("GROUP_CONCAT(tags.type) IS NULL")
      else
        scoped
      end
    }
  end
end