Module: PeteOnRails::VoteFu::Karma::InstanceMethods

Defined in:
lib/has_karma.rb

Overview

This module contains instance methods

Instance Method Summary collapse

Instance Method Details

#karma(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/has_karma.rb', line 44

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

#options_for_karma(object, options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/has_karma.rb', line 57

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