has_karma.rb
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
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
57
58
59
60
61
62
63
64
65
66
67
68
# Has Karma
module PeteOnRails
module VoteFu #:nodoc:
module Karma #:nodoc:
def self.included(base)
base.extend ClassMethods
class << base
attr_accessor :karmatic_objects
end
end
module ClassMethods
def has_karma(voteable_type)
self.class_eval <<-RUBY
def karma_voteable
#{voteable_type.to_s.classify}
end
RUBY
include PeteOnRails::VoteFu::Karma::InstanceMethods
extend PeteOnRails::VoteFu::Karma::SingletonMethods
if self.karmatic_objects.nil?
self.karmatic_objects = [eval(voteable_type.to_s.classify)]
else
self.karmatic_objects.push(eval(voteable_type.to_s.classify))
end
end
end
# This module contains class methods
module SingletonMethods
## Not yet implemented. Don't use it!
# Find the most popular users
def find_most_karmic
find(:all)
end
end
# This module contains instance methods
module InstanceMethods
def karma(options = {})
#FIXME cannot have 2 models imapcting the karma simultaneously
# count the total number of votes on all of the voteable objects that are related to this object
#2009-01-30 GuillaumeNM The following line is not SQLite3 compatible, because boolean are stored as 'f' or 't', not '1', or '0'
#self.karma_voteable.sum(:vote, options_for_karma(options))
#self.karma_voteable.find(:all, options_for_karma(options)).length
karma_value = 0
self.class.karmatic_objects.each do |object|
karma_value += object.find(:all, options_for_karma(object, options)).length
end
return karma_value
end
def options_for_karma (object, options = {})
#GuillaumeNM : 2009-01-30 Adding condition for SQLite3
conditions = ["u.id = ? AND vote = ?" , self[:id] , true]
joins = ["inner join votes v on #{object.table_name}.id = v.voteable_id", "inner join #{self.class.table_name} u on u.id = #{object.name.tableize}.#{self.class.name.foreign_key}"]
{ :joins => joins.join(" "), :conditions => conditions }.update(options)
end
end
end
end
end