README.md in likes_tracker-0.0.1 vs README.md in likes_tracker-0.0.3

- old
+ new

@@ -21,14 +21,20 @@ * [redis](http://redis.io) * ruby 1.9+ (it uses some 1.9's syntax) ## Usage -Given you have two models, say User and Post, and you want to track the *likes* a given Post receives by User(s). Include the LikesTracker -module and use the methods it offers to setup models as *liker* and *liked*: +First of all, you need a ```$redis``` in your app, you might achieve this using an initializer: ``` +# config/initializers/redis.rb +$redis = Redis.new(host: 'localhost', port: '6379', db: '1') +``` + +Given you have two models, say User and Post, and you want to track the *likes* a given Post receives by User(s). Include the LikesTracker module and use the methods it offers to setup models as *liker* and *liked*: + +``` # app/models/post.rb class Post < ActiveRecord::Base include LikesTracker acts_as_liked_by :users @@ -61,24 +67,20 @@ > user.likes_post? post => true ``` -As you can see, the methods' names reflect model names (and they'll be properly namespaced on Redis). This means, that the same models can like -several others, for example User might like another model called Photo or Comment. +As you can see, the methods' names reflect model names (and they'll be properly namespaced on Redis). This means, that the same models can like several others, for example User might like another model called Photo or Comment, so you'll have methods like ```#like_comment!``` or ```#likes_photo?``` and so on. How to find Posts liked by a User? There's a method for this, of course ;-) ``` > user.liked_posts => [#<Post id: 1, ...>] ``` -It returns a *relation*, such as ```ActiveRecord::Relation```. Even if I -haven't tested it yet, this *should* work with other ORMs like Mongoid. +It returns a *relation*, such as ```ActiveRecord::Relation```. Even if I haven't tested it yet, this *should* work with other ORMs like Mongoid. -However, this method has an experimental feature: it accepts a block to operate custom queries. I'm still not sure I will expand it to other methods. - ``` # a silly example to show how it works > user.liked_posts {|model, ids| p [model, ids] } => [Post(id: integer, ...), ["1"]] @@ -88,14 +90,22 @@ ``` Last but not least, here there're the remaining methods and examples: ``` -# you should provide a *limit* parameter +# you can provide a *limit* parameter, if omitted it defaults to 5 > Post.most_liked(5) => [#<Post id: 1, ...>] +# and it also accepts an *offset* parameter, if omitted it defaults to 0 +> Post.most_liked(5, 0) + => [#<Post id: 1, ...>] + +# last but not least, it accepts a block, like you've already seen in above examples +> Post.most_liked(5, 0) {|model, ids| p [model, ids] } + => [Post(id: integer, ...), ["1"]] + > post.likes_users_count => 1 > user.unlike_post! post => [true, true, 0.0] @@ -124,6 +134,6 @@ * run `bundle install` * run `rspec spec` ## License -Copyright (c) 2012 Andrea Pavoni http://andreapavoni.com \ No newline at end of file +Copyright (c) 2012 Andrea Pavoni http://andreapavoni.com