thumbs_up 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ pkg/*
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -30,21 +30,40 @@ module ThumbsUp
30
30
  # :at_most - Item may not have more than X votes
31
31
  def tally(*args)
32
32
  options = args.extract_options!
33
+
34
+ # Use the explicit SQL statement throughout for Postgresql compatibility.
35
+ vote_count = "COUNT(#{Vote.table_name}.voteable_id)"
36
+
33
37
  t = self.where("#{Vote.table_name}.voteable_type = '#{self.name}'")
38
+
34
39
  # We join so that you can order by columns on the voteable model.
35
40
  t = t.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.#{self.primary_key} = #{Vote.table_name}.voteable_id")
36
- t = t.having("vote_count > 0")
37
- t = t.group("#{Vote.table_name}.voteable_id")
41
+
42
+ t = t.group("#{Vote.table_name}.voteable_id, #{column_names_for_tally}")
38
43
  t = t.limit(options[:limit]) if options[:limit]
39
44
  t = t.where("#{Vote.table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
40
45
  t = t.where("#{Vote.table_name}.created_at <= ?", options[:end_at]) if options[:end_at]
41
46
  t = t.where(options[:conditions]) if options[:conditions]
42
- t = options[:order] ? t.order(options[:order]) : t.order("vote_count DESC")
43
- t = t.having(["vote_count >= ?", options[:at_least]]) if options[:at_least]
44
- t = t.having(["vote_count <= ?", options[:at_most]]) if options[:at_most]
47
+ t = options[:order] ? t.order(options[:order]) : t.order("#{vote_count} DESC")
48
+
49
+ # I haven't been able to confirm this bug yet, but Arel (2.0.7) currently blows up
50
+ # with multiple 'having' clauses. So we hack them all into one for now.
51
+ # If you have a more elegant solution, a pull request on Github would be greatly appreciated.
52
+ t = t.having([
53
+ "#{vote_count} > 0",
54
+ (options[:at_least] ? "#{vote_count} >= #{sanitize(options[:at_least])}" : nil),
55
+ (options[:at_most] ? "#{vote_count} <= #{sanitize(options[:at_most])}" : nil)
56
+ ].compact.join(' AND '))
57
+ # t = t.having("#{vote_count} > 0")
58
+ # t = t.having(["#{vote_count} >= ?", options[:at_least]]) if options[:at_least]
59
+ # t = t.having(["#{vote_count} <= ?", options[:at_most]]) if options[:at_most]
45
60
  t.select("#{self.table_name}.*, COUNT(#{Vote.table_name}.voteable_id) AS vote_count")
46
61
  end
47
62
 
63
+ def column_names_for_tally
64
+ column_names.map { |column| "#{self.table_name}.#{column}" }.join(', ')
65
+ end
66
+
48
67
  end
49
68
 
50
69
  module InstanceMethods
@@ -82,4 +101,4 @@ module ThumbsUp
82
101
 
83
102
  end
84
103
  end
85
- end
104
+ end
data/lib/acts_as_voter.rb CHANGED
@@ -79,7 +79,7 @@ module ThumbsUp #:nodoc:
79
79
  if options[:exclusive]
80
80
  self.clear_votes(voteable)
81
81
  end
82
- direction = (options[:direction].to_sym == :up ? true : false)
82
+ direction = (options[:direction].to_sym == :up)
83
83
  Vote.create!(:vote => direction, :voteable => voteable, :voter => self)
84
84
  end
85
85
 
@@ -1,21 +1,23 @@
1
1
  class ThumbsUpMigration < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :votes, :force => true do |t|
4
- t.boolean :vote, :default => false
4
+
5
+ t.boolean :vote, :default => false
5
6
  t.references :voteable, :polymorphic => true, :null => false
6
7
  t.references :voter, :polymorphic => true
7
8
  t.timestamps
9
+
8
10
  end
9
11
 
10
- add_index :votes, ["voter_id", "voter_type"], :name => "fk_voters"
11
- add_index :votes, ["voteable_id", "voteable_type"], :name => "fk_voteables"
12
+ add_index :votes, [:voter_id, :voter_type]
13
+ add_index :votes, [:voteable_id, :voteable_type]
12
14
 
13
- # If you don't want to enforce "One Person, One Vote" rules in the database, comment out the index below.
14
- add_index :votes, ["voter_id", "voter_type", "voteable_id", "voteable_type"], :unique => true, :name => "uniq_one_vote_only"
15
+ # Comment out the line below to allow multiple votes per voter on a single entity.
16
+ add_index :votes, [:voter_id, :voter_type, :voteable_id, :voteable_type], :unique => true, :name => 'fk_one_vote_per_user_per_entity'
15
17
  end
16
18
 
17
19
  def self.down
18
20
  drop_table :votes
19
21
  end
20
22
 
21
- end
23
+ end
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.name.foreign_key), (options[:weight] ? options[:weight] : 1).to_f ]
16
+ self.karmic_objects[voteable_type.to_s.classify.constantize] = [ (options[:as] ? options[:as].to_s.foreign_key : self.name.foreign_key), (options[:weight] || 1).to_f ]
17
17
  end
18
18
  end
19
19
 
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.3.1"
8
+ s.version = IO.read('./VERSION')
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-09-06}
12
+ s.date = Date.today.to_s
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
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
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-09-06 00:00:00 +10:00
22
+ date: 2010-12-16 00:00:00 -07:00
23
23
  default_executable:
24
24
  dependencies: []
25
25