Permalink
Browse files

ratable top method options is now a Hash

  • Loading branch information...
1 parent 6351d7a commit 2469c932e310873a215e1d261604b0689978695e @romaind romaind committed Jan 9, 2014
Showing with 27 additions and 5 deletions.
  1. +7 −2 lib/recommendable/ratable.rb
  2. +20 −3 test/recommendable/ratable_test.rb
@@ -45,9 +45,14 @@ 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, start = 0)
+ def self.top(options = {})
+ if options.is_a?(Integer)
+ options = { count: options}
+ warn "[DEPRECATION] Recommenable::Ratable.top now takes an options hash. Please call `.top(count: #{options[:count]})` instead of just `.top(#{options[:count]})`"
+ end
+ options.reverse_merge!(count: 1, offset: 0)
score_set = Recommendable::Helpers::RedisKeyMapper.score_set_for(self)
- ids = Recommendable.redis.zrevrange(score_set, start, start + count - 1)
+ ids = Recommendable.redis.zrevrange(score_set, options[:offset], options[:offset] + options[:count] - 1)
Recommendable.query(self, ids).sort_by { |item| ids.index(item.id.to_s) }
end
@@ -33,7 +33,7 @@ def test_rated_predicate_works
assert @movie.rated?
end
- def test_top_scope_returns_best_things
+ def test_top_scope_deprecated_syntax_returns_best_things
@book2 = Factory(:book)
@book3 = Factory(:book)
@user = Factory(:user)
@@ -50,6 +50,23 @@ def test_top_scope_returns_best_things
assert_equal top[2], @book
end
+ def test_top_scope_returns_best_things
+ @book2 = Factory(:book)
+ @book3 = Factory(:book)
+ @user = Factory(:user)
+ @friend = Factory(:user)
+
+ @user.like(@book2)
+ @friend.like(@book2)
+ @user.like(@book3)
+ @user.dislike(@book)
+
+ top = Book.top(count: 3)
+ assert_equal top[0], @book2
+ assert_equal top[1], @book3
+ assert_equal top[2], @book
+ end
+
def test_top_scope_returns_best_things_for_ratable_base_class
@movie2 = Factory(:movie)
@doc = Factory(:documentary)
@@ -61,7 +78,7 @@ def test_top_scope_returns_best_things_for_ratable_base_class
@user.like(@movie2)
@user.dislike(@movie)
- top = Movie.top(3)
+ top = Movie.top(count: 3)
assert_equal top[0], @doc
assert_equal top[1], @movie2
assert_equal top[2], @movie
@@ -78,7 +95,7 @@ def test_top_scope_returns_best_things_with_start
@user.like(@movie2)
@user.dislike(@movie)
- top = Movie.top(2, 1)
+ top = Movie.top(count: 2, offset: 1)
assert_equal top[0], @movie2
assert_equal top[1], @movie
end

0 comments on commit 2469c93

Please sign in to comment.