Skip to content

Commit

Permalink
Added missing :limit option to #plusminus_tally.
Browse files Browse the repository at this point in the history
  • Loading branch information
bouchard committed Nov 27, 2011
1 parent 1210385 commit 04cdace
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/acts_as_voteable.rb
Expand Up @@ -51,8 +51,9 @@ def plusminus_tally(*args)
AS joined_#{Vote.table_name} ON #{self.table_name}.#{self.primary_key} =
joined_#{Vote.table_name}.voteable_id")

t = t.where("joined_#{Vote.table_name}.voteable_type = '#{self.name}'")
t = t.group("joined_#{Vote.table_name}.voteable_id, joined_#{Vote.table_name}.vote_total, #{column_names_for_tally}")
t = t.limit(options[:limit]) if options[:limit]
t = t.where("joined_#{Vote.table_name}.voteable_type = '#{self.name}'")
t = t.where("joined_#{Vote.table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
t = t.where("joined_#{Vote.table_name}.created_at <= ?", options[:end_at]) if options[:end_at]
t = options[:ascending] ? t.order("joined_#{Vote.table_name}.vote_total") : t.order("joined_#{Vote.table_name}.vote_total DESC")
Expand Down
16 changes: 10 additions & 6 deletions test/test_helper.rb
Expand Up @@ -34,8 +34,9 @@
end

create_table :items, :force => true do |t|
t.string :name
t.string :description
t.integer :user_id
t.string :name
t.string :description
end
end

Expand All @@ -57,12 +58,15 @@ class Vote < ActiveRecord::Base
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
end

class User < ActiveRecord::Base
acts_as_voter
end

class Item < ActiveRecord::Base
acts_as_voteable
belongs_to :user
end

class User < ActiveRecord::Base
acts_as_voter
has_many :items
has_karma(:items)
end

class Test::Unit::TestCase
Expand Down
57 changes: 37 additions & 20 deletions test/test_thumbs_up.rb
Expand Up @@ -145,37 +145,37 @@ def test_tally_between_start_at_end_at
assert_equal 2, Item.tally(:start_at => 3.days.ago, :end_at => 4.days.from_now).length
end

def test_rank_tally_empty
def test_plusminus_tally_empty
item = Item.create(:name => 'XBOX', :description => 'XBOX console')

assert_equal 0, Item.rank_tally.length
assert_equal 0, Item.plusminus_tally.length
end

def test_rank_tally_starts_at
def test_plusminus_tally_starts_at
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
user = User.create(:name => 'david')

vote = user.vote_for(item)
vote.created_at = 3.days.ago
vote.save

assert_equal 0, Item.rank_tally(:start_at => 2.days.ago).length
assert_equal 1, Item.rank_tally(:start_at => 4.days.ago).length
assert_equal 0, Item.plusminus_tally(:start_at => 2.days.ago).length
assert_equal 1, Item.plusminus_tally(:start_at => 4.days.ago).length
end

def test_rank_tally_end_at
def test_plusminus_tally_end_at
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
user = User.create(:name => 'david')

vote = user.vote_for(item)
vote.created_at = 3.days.from_now
vote.save

assert_equal 0, Item.rank_tally(:end_at => 2.days.from_now).length
assert_equal 1, Item.rank_tally(:end_at => 4.days.from_now).length
assert_equal 0, Item.plusminus_tally(:end_at => 2.days.from_now).length
assert_equal 1, Item.plusminus_tally(:end_at => 4.days.from_now).length
end

def test_rank_tally_between_start_at_end_at
def test_plusminus_tally_between_start_at_end_at
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
another_item = Item.create(:name => 'XBOX', :description => 'XBOX console')
user = User.create(:name => 'david')
Expand All @@ -188,42 +188,59 @@ def test_rank_tally_between_start_at_end_at
vote.created_at = 3.days.from_now
vote.save

assert_equal 1, Item.rank_tally(:start_at => 3.days.ago, :end_at => 2.days.from_now).length
assert_equal 2, Item.rank_tally(:start_at => 3.days.ago, :end_at => 4.days.from_now).length
assert_equal 1, Item.plusminus_tally(:start_at => 3.days.ago, :end_at => 2.days.from_now).length
assert_equal 2, Item.plusminus_tally(:start_at => 3.days.ago, :end_at => 4.days.from_now).length
end

def test_rank_tally_inclusion
def test_plusminus_tally_inclusion
user = User.create(:name => 'david')
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
item_not_included = Item.create(:name => 'Playstation', :description => 'Playstation console')

assert_not_nil user.vote_for(item)

assert (Item.rank_tally.include? item)
assert (not Item.rank_tally.include? item_not_included)
assert (Item.plusminus_tally.include? item)
assert (not Item.plusminus_tally.include? item_not_included)
end

def test_rank_tally_default_ordering
def test_plusminus_tally_default_ordering
user = User.create(:name => 'david')
item_for = Item.create(:name => 'XBOX', :description => 'XBOX console')
item_against = Item.create(:name => 'Playstation', :description => 'Playstation console')

assert_not_nil user.vote_for(item_for)
assert_not_nil user.vote_against(item_against)

assert_equal item_for, Item.rank_tally[0]
assert_equal item_against, Item.rank_tally[1]
assert_equal item_for, Item.plusminus_tally[0]
assert_equal item_against, Item.plusminus_tally[1]
end

def test_plusminus_tally_limit
users = (0..9).map{ |u| User.create(:name => "User #{u}") }
items = (0..9).map{ |u| Item.create(:name => "Item #{u}", :description => "Item #{u}") }
users.each{ |u| items.each { |i| u.vote_for(i) } }
assert_equal 10, Item.plusminus_tally.length
assert_equal 2, Item.plusminus_tally(:limit => 2).length
end

def test_rank_tally_ascending_ordering
def test_plusminus_tally_ascending_ordering
user = User.create(:name => 'david')
item_for = Item.create(:name => 'XBOX', :description => 'XBOX console')
item_against = Item.create(:name => 'Playstation', :description => 'Playstation console')

assert_not_nil user.vote_for(item_for)
assert_not_nil user.vote_against(item_against)

assert_equal item_for, Item.rank_tally(:ascending => true)[1]
assert_equal item_against, Item.rank_tally(:ascending => true)[0]
assert_equal item_for, Item.plusminus_tally(:ascending => true)[1]
assert_equal item_against, Item.plusminus_tally(:ascending => true)[0]
end

def test_karma
users = (0..1).map{ |u| User.create(:name => "User #{u}") }
items = (0..1).map{ |u| users[0].items.create(:name => "Item #{u}", :description => "Item #{u}") }
users.each{ |u| items.each { |i| u.vote_for(i) } }

assert_equal 4, users[0].karma
assert_equal 0, users[1].karma
end
end

0 comments on commit 04cdace

Please sign in to comment.