12
13
14
15
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
|
# File 'lib/rating_for.rb', line 12
def rating_for(element, options = {})
has_one "rating_for_#{element}".to_sym,
:dependent => :destroy,
:as => :element,
:class_name => 'RateableElement',
:conditions => {:element_attribute => element.to_s}
define_method "rating_for_#{element}" do
rateable_element = instance_variable_get("@_rating_for_#{element}")
if rateable_element.nil?
rateable_element = RateableElement.find_by_element_id(self.id, :conditions => {:element_type => self.class.name, :element_attribute => element.to_s})
rateable_element = RateableElement.new(:element_id => self.id, :element_type => self.class.name, :element_attribute => element.to_s) if rateable_element.nil?
instance_variable_set("@_rating_for_#{element}", rateable_element)
end
rateable_element
end
(class << self; self; end).module_eval do
define_method "find_where_#{element}_has_average_rating_of" do |value|
self.find_with_average_rating_of(element.to_s, value)
end
end
(class << self; self; end).module_eval do
define_method "find_where_#{element}_has_total_rating_of" do |value|
self.find_with_average_rating_of(element.to_s, value)
end
end
(class << self; self; end).module_eval do
define_method "find_where_#{element}_has_ratings_count_of" do |value|
self.find_with_average_rating_of(element.to_s, value)
end
end
(class << self; self; end).module_eval do
define_method "find_where_#{element}_was_rated_by" do |rater|
self.find_rated_by(element.to_s, rater)
end
end
extend RatingFor::SingletonMethods
end
|