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
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(','), :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(','), :having => "count(tt.id) >= #{tags.length}",
:readonly => false
}
} do
def count
length
end
end
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
|