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
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_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
|