Permalink
Browse files

Ratable top method now accepts a start argument to be able to paginat…

…e top elements
  • Loading branch information...
1 parent 59c0016 commit 6351d7a4d6c743e0ee6a1dc9e7fecdad94a851a5 @romaind romaind committed Jan 8, 2014
Showing with 19 additions and 3 deletions.
  1. +3 −3 lib/recommendable/ratable.rb
  2. +16 −0 test/recommendable/ratable_test.rb
@@ -14,7 +14,7 @@ def make_recommendable!
class_eval do
include Likable
include Dislikable
-
+
case
when defined?(Sequel::Model) && ancestors.include?(Sequel::Model)
def before_destroy() super and remove_from_recommendable! end
@@ -45,9 +45,9 @@ def rated?
#
# @param [Fixnum] count the number of items to fetch (defaults to 1)
# @return [Array] the top items belonging to this class, sorted by score
- def self.top(count = 1)
+ def self.top(count = 1, start = 0)
score_set = Recommendable::Helpers::RedisKeyMapper.score_set_for(self)
- ids = Recommendable.redis.zrevrange(score_set, 0, count - 1)
+ ids = Recommendable.redis.zrevrange(score_set, start, start + count - 1)
Recommendable.query(self, ids).sort_by { |item| ids.index(item.id.to_s) }
end
@@ -67,6 +67,22 @@ def test_top_scope_returns_best_things_for_ratable_base_class
assert_equal top[2], @movie
end
+ def test_top_scope_returns_best_things_with_start
+ @movie2 = Factory(:movie)
+ @doc = Factory(:documentary)
+ @user = Factory(:user)
+ @friend = Factory(:user)
+
+ @user.like(@doc)
+ @friend.like(@doc)
+ @user.like(@movie2)
+ @user.dislike(@movie)
+
+ top = Movie.top(2, 1)
+ assert_equal top[0], @movie2
+ assert_equal top[1], @movie
+ end
+
def test_removed_from_recommendable_upon_destruction
@user = Factory(:user)
@friend = Factory(:user)

0 comments on commit 6351d7a

Please sign in to comment.