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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 17
def acts_as_taggable_on(*args)
args.flatten! if args
args.compact! if args
for tag_type in args
tag_type = tag_type.to_s
self.class_eval do
has_many "#{tag_type.singularize}_taggings".to_sym, :as => :taggable, :dependent => :destroy,
:include => :tag, :conditions => ['#{aliased_join_table_name rescue "taggings"}.context = ?',tag_type], :class_name => "Tagging"
has_many "#{tag_type}".to_sym, :through => "#{tag_type.singularize}_taggings".to_sym, :source => :tag, :order => "`tags`.name"
end
self.class_eval <<-RUBY
def self.taggable?
true
end
def self.caching_#{tag_type.singularize}_list?
caching_tag_list_on?("#{tag_type}")
end
def self.#{tag_type.singularize}_counts(options={})
tag_counts_on('#{tag_type}',options)
end
def #{tag_type.singularize}_list
tag_list_on('#{tag_type}')
end
def #{tag_type.singularize}_list=(new_tags)
set_tag_list_on('#{tag_type}',new_tags)
end
def #{tag_type.singularize}_counts(options = {})
tag_counts_on('#{tag_type}',options)
end
def #{tag_type}_from(owner)
tag_list_on('#{tag_type}', owner)
end
def find_related_#{tag_type}(options = {})
related_tags_for('#{tag_type}', self.class, options)
end
alias_method :find_related_on_#{tag_type}, :find_related_#{tag_type}
def find_related_#{tag_type}_for(klass, options = {})
related_tags_for('#{tag_type}', klass, options)
end
def top_#{tag_type}(limit = 10)
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
end
def self.top_#{tag_type}(limit = 10)
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
end
RUBY
end
if respond_to?(:tag_types)
write_inheritable_attribute( :tag_types, (tag_types + args).uniq )
else
self.class_eval do
write_inheritable_attribute(:tag_types, args.uniq)
class_inheritable_reader :tag_types
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag
has_many :base_tags, :class_name => "Tag", :through => :taggings, :source => :tag
attr_writer :custom_contexts
before_save :save_cached_tag_list
after_save :save_tags
if respond_to?(:named_scope)
named_scope :tagged_with, lambda{ |*args|
find_options_for_find_tagged_with(*args)
}
end
end
include ActiveRecord::Acts::TaggableOn::InstanceMethods
extend ActiveRecord::Acts::TaggableOn::SingletonMethods
alias_method_chain :reload, :tag_list
end
end
|