Module: Taggable::Model::ClassMethods

Defined in:
lib/taggable/model.rb

Instance Method Summary collapse

Instance Method Details

#has_tagsObject



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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/taggable/model.rb', line 21

def has_tags
  return if has_tags?

  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_tag, lambda { |tag|
    tag = Tag.find_by_title(tag) unless tag.is_a? Tag
    {
      :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 = ?", tag.id],
      :readonly => false
    }
  }

  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

  # creates eg. tag.pages, tag.assets
  # (returning the from_tag scope defined above)
  Tag.define_retrieval_methods(self.to_s)

  class_eval {
    extend Taggable::Model::TaggableClassMethods
    include Taggable::Model::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

#has_tags?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/taggable/model.rb', line 13

def has_tags?
  false
end

#is_taggableObject



77
78
79
80
# File 'lib/taggable/model.rb', line 77

def is_taggable
  ActiveSupport::Deprecation.warn("The is_taggable syntax has been replaced with has_tags() and has_tags? for consistency with other extensions.", caller)
  has_tags
end

#is_taggable?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/taggable/model.rb', line 16

def is_taggable?
  ActiveSupport::Deprecation.warn("The is_taggable syntax has been replaced with has_tags for consistency with other extensions.", caller)
  has_tags?
end