thumbs_up 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -48,8 +48,9 @@ Usage
48
48
  acts_as_voter
49
49
  # The following line is optional, and tracks karma (up votes) for questions this user has submitted.
50
50
  # Each question has a submitter_id column that tracks the user who submitted it.
51
+ # The option :weight value will be multiplied to any karma from that voteable model (defaults to 1).
51
52
  # You can track any voteable model.
52
- has_karma(:questions, :as => :submitter)
53
+ has_karma(:questions, :as => :submitter, :weight => 0.5)
53
54
  end
54
55
 
55
56
  class Robot < ActiveRecord::Base
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
data/lib/acts_as_voter.rb CHANGED
@@ -75,17 +75,21 @@ module ThumbsUp #:nodoc:
75
75
  def vote(voteable, options = {})
76
76
  raise ArgumentError "you must specify :up or :down in order to vote" unless options[:direction] && [:up, :down].include?(options[:direction].to_sym)
77
77
  if options[:exclusive]
78
- Vote.where(
79
- :voter_id => self.id,
80
- :voter_type => self.class.name,
81
- :voteable_id => voteable.id,
82
- :voteable_type => voteable.class.name
83
- ).map(&:destroy)
78
+ self.clear_votes(voteable)
84
79
  end
85
80
  direction = (options[:direction].to_sym == :up ? true : false)
86
81
  Vote.create!(:vote => direction, :voteable => voteable, :voter => self)
87
82
  end
88
83
 
84
+ def clear_votes(voteable)
85
+ Vote.where(
86
+ :voter_id => self.id,
87
+ :voter_type => self.class.name,
88
+ :voteable_id => voteable.id,
89
+ :voteable_type => voteable.class.name
90
+ ).map(&:destroy)
91
+ end
92
+
89
93
  def voted_which_way?(voteable, direction)
90
94
  raise ArgumentError, "expected :up or :down" unless [:up, :down].include?(direction)
91
95
  0 < Vote.where(
data/lib/has_karma.rb CHANGED
@@ -13,7 +13,7 @@ module ThumbsUp #:nodoc:
13
13
  include ThumbsUp::Karma::InstanceMethods
14
14
  extend ThumbsUp::Karma::SingletonMethods
15
15
  self.karmic_objects ||= {}
16
- self.karmic_objects[voteable_type.to_s.classify.constantize] = (options[:as] ? options[:as].to_s.foreign_key : self.class.name.foreign_key)
16
+ 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 ]
17
17
  end
18
18
  end
19
19
 
@@ -29,11 +29,11 @@ module ThumbsUp #:nodoc:
29
29
 
30
30
  module InstanceMethods
31
31
  def karma(options = {})
32
- self.class.karmic_objects.collect do |object, fk|
32
+ self.class.karmic_objects.collect do |object, attr|
33
33
  v = object.where(["#{Vote.table_name}.vote = ?", true]).where(["#{self.class.table_name}.#{self.class.primary_key} = ?", self.id])
34
34
  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}")
35
- v = v.joins("INNER JOIN #{self.class.table_name} ON #{self.class.table_name}.#{self.class.primary_key} = #{object.table_name}.#{fk}")
36
- v.count
35
+ v = v.joins("INNER JOIN #{self.class.table_name} ON #{self.class.table_name}.#{self.class.primary_key} = #{object.table_name}.#{attr[0]}")
36
+ (v.count.to_f * attr[1]).round
37
37
  end.sum
38
38
  end
39
39
  end
data/thumbs_up.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{thumbs_up}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brady Bouchard", "Peter Jackson", "Cosmin Radoi", "Bence Nagy", "Rob Maddox", "Wojciech Wnętrzak"]
12
- s.date = %q{2010-08-06}
12
+ s.date = %q{2010-08-28}
13
13
  s.description = %q{ThumbsUp provides dead-simple voting capabilities to ActiveRecord models with karma calculation, a la stackoverflow.com.}
14
14
  s.email = %q{brady@ldawn.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brady Bouchard
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2010-08-06 00:00:00 +10:00
22
+ date: 2010-08-28 00:00:00 +10:00
23
23
  default_executable:
24
24
  dependencies: []
25
25