Module: Atreides::Base::Taggable

Included in:
Feature, Page, Post, Product
Defined in:
lib/atreides/base/taggable.rb

Overview

Module providing common tagging functionality. Uses the acts_as_taggable mixin.

Class Method Summary collapse

Class Method Details

.included(recipient) ⇒ Object



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
# File 'lib/atreides/base/taggable.rb', line 8

def self.included(recipient)
  recipient.class_eval do

    # Throws errors if table doesn't exist on first project setup
    if recipient.table_exists?
      acts_as_taggable

      if Rails.env.development? || Rails.env.staging?
        Settings.load! # Needed for dev env when reloading class caches
      end

      if defined?(Settings.tags[self.table_name]) && defined?(Settings.tags[self.table_name]['groups'])
        acts_as_taggable_on Settings.tags[self.table_name]['groups']
        scope :tagged, lambda { |tags|
          tags_sql = tags.is_a?(Array) ? tags.map{|t|"'#{t}'"}.join(",") : "'#{tags}'"
          select("#{table_name}.*").
          joins("JOIN taggings ON taggings.taggable_id = #{table_name}.id AND taggings.taggable_type IN ('#{to_s}')").
          where("taggings.tag_id in (SELECT id from tags where LOWER(name) IN (#{tags_sql.downcase}))")
        }
      end
    end

    # Add callback to normalize tags
    before_save :normalize_tags

    private

    def normalize_tags
      self.tag_list = self.tag_list.map(&:parameterize)
    end

  end
end