Permalink
Browse files

Allow pagination of recommendations and users

A request made in #79 to paginate likes made me think this would be nice
for users.

```ruby
@user.recommended_movies(10, 20)
```

Signed-off-by: David Celis <me@davidcel.is>
  • Loading branch information...
1 parent c0fa615 commit f99fa21a5cef3a2b3dc659b07b14f6568ab366ef @davidcelis committed Jun 14, 2013
Showing with 4 additions and 8 deletions.
  1. +4 −4 lib/recommendable/rater/recommender.rb
  2. +0 −4 test/recommendable/rater/recommender_test.rb
View
8 lib/recommendable/rater/recommender.rb
@@ -6,8 +6,8 @@ module Recommender
#
# @param [Fixnum] count the number of users to return (defaults to 10)
# @return [Array] An array of instances of your user class
- def similar_raters(count = 10)
- ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), 0, count - 1)
+ def similar_raters(count = 10, offset = 0)
+ ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), offset, count - 1)
Recommendable.query(self.class, ids).sort_by { |user| ids.index(user.id.to_s) }
end
@@ -18,11 +18,11 @@ def similar_raters(count = 10)
# @param [String, Symbol, Class] klass the class from which to get recommendations
# @param [Fixnum] count the number of recommendations to fetch (defaults to 10)
# @return [Array] a list of things this person's gonna love
- def recommended_for(klass, count = 10)
+ def recommended_for(klass, count = 10, offset = 0)
recommended_set = Recommendable::Helpers::RedisKeyMapper.recommended_set_for(klass, self.id)
return Recommendable.query(klass, []) unless rated_anything? && Recommendable.redis.zcard(recommended_set) > 0
- ids = Recommendable.redis.zrevrange(recommended_set, 0, count - 1)
+ ids = Recommendable.redis.zrevrange(recommended_set, offset, count - 1)
Recommendable.query(klass, ids)
end
View
4 test/recommendable/rater/recommender_test.rb
@@ -50,10 +50,6 @@ def test_recommended_for_only_returns_relevant_recommendations
[@movie7, @movie8, @movie9, @movie10].each { |movie| refute_includes @user.recommended_books, movie }
end
- def test_unrecommend_removes_items_from_recommendations
-
- end
-
def test_that_it_is_removed_from_recommendable_after_destroy
Recommendable::Helpers::Calculations.update_similarities_for(@user2.id)

0 comments on commit f99fa21

Please sign in to comment.