Permalink
Browse files

Merge pull request #85 from StudioMelipone/ratable-top-start

Ratable top method now accepts a start argument
  • Loading branch information...
2 parents 59c0016 + 2469c93 commit dd9e9dcd65e71b83f766d38067b27378433af9df @davidcelis committed Jan 9, 2014
Showing with 43 additions and 5 deletions.
  1. +8 −3 lib/recommendable/ratable.rb
  2. +35 −2 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,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)
+ 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, 0, 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,12 +78,28 @@ 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
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(count: 2, offset: 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 dd9e9dc

Please sign in to comment.