Module: TaggableModel::ClassMethods

Defined in:
lib/taggable_model.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) is_taggable



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
57
58
59
60
61
62
63
# File 'lib/taggable_model.rb', line 16

def is_taggable
  return if is_taggable?

  has_many :taggings, :as => :tagged
  has_many :attached_tags, :through => :taggings, :source => :tag    # can't be just has_many :tags because that stomps on the radius tags in Page.
  
  named_scope :from_tags, lambda { |tags| 
    {
      :joins => "INNER JOIN taggings as tt on tt.tagged_id = #{self.table_name}.id AND tt.tagged_type = '#{self.to_s}'", 
      :conditions => ["tt.tag_id in(#{tags.map{ '?' }.join(',')})"] + tags.map(&:id),
      :group => column_names.map { |n| table_name + '.' + n }.join(','),    # postgres is strict and requires that we group by all selected (but not aggregated) columns
      :order => "count(tt.id) DESC",
      :readonly => false
    }
  }

  named_scope :from_all_tags, lambda { |tags| 
    {
      :joins => "INNER JOIN taggings as tt on tt.tagged_id = #{self.table_name}.id AND tt.tagged_type = '#{self.to_s}'", 
      :conditions => ["tt.tag_id in(#{tags.map{ '?' }.join(',')})"] + tags.map(&:id),
      :group => column_names.map { |n| table_name + '.' + n }.join(','),    # postgres is strict and requires that we group by all selected (but not aggregated) columns
      :having => "count(tt.id) >= #{tags.length}",
      :readonly => false
    }
  } do
    # count is badly sugared here: it omits the group and having clauses.
    # length performs the query and looks at the array: less neat, but more right
    # this gives us back any? and empty? as well.
    def count
      length
    end
  end
  
  # this sets up eg Taggings.of_model
  # and then uses that to define instance methods in Tag:
  # tag.models
  # tag.models_count
  Tag.define_class_retrieval_methods(self.to_s)
  
  class_eval {
    extend TaggableModel::TaggableClassMethods
    include TaggableModel::TaggableInstanceMethods
    alias_method "related_#{self.to_s.underscore.pluralize}".intern, :related
    alias_method "closely_related_#{self.to_s.underscore.pluralize}".intern, :closely_related
  }

  ActiveRecord::Base.taggable_models.push(self.to_s.intern)
end

- (Boolean) is_taggable?



12
13
14
# File 'lib/taggable_model.rb', line 12

def is_taggable?
  false
end