lib/has_karma.rb in thumbs_up-0.2.3 vs lib/has_karma.rb in thumbs_up-0.3.0
- old
+ new
@@ -11,11 +11,11 @@
module ClassMethods
def has_karma(voteable_type, options = {})
include ThumbsUp::Karma::InstanceMethods
extend ThumbsUp::Karma::SingletonMethods
self.karmic_objects ||= {}
- self.karmic_objects[voteable_type.to_s.classify.constantize] = (options[:as] ? options[:as].to_s.foreign_key : self.class.name.foreign_key)
+ self.karmic_objects[voteable_type.to_s.classify.constantize] = [ (options[:as] ? options[:as].to_s.foreign_key : self.name.foreign_key), (options[:weight] ? options[:weight] : 1).to_f ]
end
end
module SingletonMethods
@@ -27,14 +27,14 @@
end
module InstanceMethods
def karma(options = {})
- self.class.karmic_objects.collect do |object, fk|
+ self.class.karmic_objects.collect do |object, attr|
v = object.where(["#{Vote.table_name}.vote = ?", true]).where(["#{self.class.table_name}.#{self.class.primary_key} = ?", self.id])
v = v.joins("INNER JOIN #{Vote.table_name} ON #{Vote.table_name}.voteable_type = '#{object.to_s}' AND #{Vote.table_name}.voteable_id = #{object.table_name}.#{object.primary_key}")
- v = v.joins("INNER JOIN #{self.class.table_name} ON #{self.class.table_name}.#{self.class.primary_key} = #{object.table_name}.#{fk}")
- v.count
+ v = v.joins("INNER JOIN #{self.class.table_name} ON #{self.class.table_name}.#{self.class.primary_key} = #{object.table_name}.#{attr[0]}")
+ (v.count.to_f * attr[1]).round
end.sum
end
end
end