likes_tracker 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/README.md +20 -10
- data/lib/likes_tracker.rb +9 -3
- data/lib/likes_tracker/version.rb +1 -1
- data/spec/likes_tracker_spec.rb +39 -8
- data/spec/spec_helper.rb +5 -0
- metadata +2 -2
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -23,8 +23,14 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
First of all, you need a ```$redis``` in your app, you might achieve this using an initializer:
|
27
|
+
|
28
|
+
```
|
29
|
+
# config/initializers/redis.rb
|
30
|
+
$redis = Redis.new(host: 'localhost', port: '6379', db: '1')
|
31
|
+
```
|
32
|
+
|
33
|
+
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*:
|
28
34
|
|
29
35
|
```
|
30
36
|
# app/models/post.rb
|
@@ -63,8 +69,7 @@ Now your models will have some methods to manage the likes a model *gives* to an
|
|
63
69
|
=> true
|
64
70
|
```
|
65
71
|
|
66
|
-
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
|
67
|
-
several others, for example User might like another model called Photo or Comment.
|
72
|
+
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.
|
68
73
|
|
69
74
|
How to find Posts liked by a User? There's a method for this, of course ;-)
|
70
75
|
|
@@ -72,10 +77,7 @@ How to find Posts liked by a User? There's a method for this, of course ;-)
|
|
72
77
|
> user.liked_posts
|
73
78
|
=> [#<Post id: 1, ...>]
|
74
79
|
```
|
75
|
-
It returns a *relation*, such as ```ActiveRecord::Relation```. Even if I
|
76
|
-
haven't tested it yet, this *should* work with other ORMs like Mongoid.
|
77
|
-
|
78
|
-
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.
|
80
|
+
It returns a *relation*, such as ```ActiveRecord::Relation```. Even if I haven't tested it yet, this *should* work with other ORMs like Mongoid.
|
79
81
|
|
80
82
|
```
|
81
83
|
# a silly example to show how it works
|
@@ -90,10 +92,18 @@ However, this method has an experimental feature: it accepts a block to operate
|
|
90
92
|
Last but not least, here there're the remaining methods and examples:
|
91
93
|
|
92
94
|
```
|
93
|
-
# you
|
95
|
+
# you can provide a *limit* parameter, if omitted it defaults to 5
|
94
96
|
> Post.most_liked(5)
|
95
97
|
=> [#<Post id: 1, ...>]
|
96
98
|
|
99
|
+
# and it also accepts an *offset* parameter, if omitted it defaults to 0
|
100
|
+
> Post.most_liked(5, 0)
|
101
|
+
=> [#<Post id: 1, ...>]
|
102
|
+
|
103
|
+
# last but not least, it accepts a block, like you've already seen in above examples
|
104
|
+
> Post.most_liked(5, 0) {|model, ids| p [model, ids] }
|
105
|
+
=> [Post(id: integer, ...), ["1"]]
|
106
|
+
|
97
107
|
> post.likes_users_count
|
98
108
|
=> 1
|
99
109
|
|
@@ -126,4 +136,4 @@ Last but not least, here there're the remaining methods and examples:
|
|
126
136
|
|
127
137
|
|
128
138
|
## License
|
129
|
-
Copyright (c) 2012 Andrea Pavoni http://andreapavoni.com
|
139
|
+
Copyright (c) 2012 Andrea Pavoni http://andreapavoni.com
|
data/lib/likes_tracker.rb
CHANGED
@@ -65,11 +65,17 @@ module LikesTracker
|
|
65
65
|
end
|
66
66
|
|
67
67
|
# find the first <limit> <object>s with more likes
|
68
|
-
define_singleton_method :most_liked do |limit|
|
68
|
+
define_singleton_method :most_liked do |limit=5, offset=0, &block|
|
69
69
|
limit -= 1 if (limit > 0)
|
70
70
|
most_liked_key = "#{self.name.downcase.pluralize}:like_scores"
|
71
|
-
most_liked_ids = $redis.zrevrange(most_liked_key,
|
72
|
-
|
71
|
+
most_liked_ids = $redis.zrevrange(most_liked_key, offset, limit)
|
72
|
+
|
73
|
+
if block
|
74
|
+
blk = ->(klass, params) { block.call(klass, params) }
|
75
|
+
blk.call(self, most_liked_ids)
|
76
|
+
else
|
77
|
+
self.where(id: most_liked_ids)
|
78
|
+
end
|
73
79
|
end
|
74
80
|
|
75
81
|
end # acts_as_liked_by
|
data/spec/likes_tracker_spec.rb
CHANGED
@@ -61,15 +61,25 @@ describe LikesTracker do
|
|
61
61
|
end # likes_post?
|
62
62
|
|
63
63
|
describe "#liked_posts" do
|
64
|
+
before(:each) do
|
65
|
+
user.like_post! post
|
66
|
+
end
|
67
|
+
|
64
68
|
it "returns an ActiveRecord::Relation" do
|
65
69
|
user.liked_posts.should be_a(ActiveRecord::Relation)
|
66
70
|
end
|
67
71
|
|
68
72
|
it "return posts liked by user" do
|
69
|
-
user.like_post! post
|
70
73
|
user.liked_posts.should include(post)
|
71
74
|
end
|
72
75
|
|
76
|
+
it "accepts a block to make custom queries" do
|
77
|
+
klass, id_list = user.liked_posts {|model, ids| [model, ids]}
|
78
|
+
|
79
|
+
klass.should == Post
|
80
|
+
id_list.should == [post.id.to_s]
|
81
|
+
end
|
82
|
+
|
73
83
|
end # liked_posts
|
74
84
|
|
75
85
|
end # .acts_as_liker_for
|
@@ -90,25 +100,46 @@ describe LikesTracker do
|
|
90
100
|
end # likes_users_count
|
91
101
|
|
92
102
|
describe ".most_liked" do
|
93
|
-
|
94
|
-
|
95
|
-
end
|
103
|
+
let(:user2) { FactoryGirl.create :user }
|
104
|
+
let(:post2) { FactoryGirl.create :post }
|
96
105
|
|
97
|
-
|
106
|
+
before(:each) do
|
98
107
|
user.like_post! post
|
99
108
|
|
100
|
-
user2 = FactoryGirl.create :user
|
101
|
-
post2 = FactoryGirl.create :post
|
102
|
-
|
103
109
|
user.like_post! post2
|
104
110
|
user2.like_post! post
|
105
111
|
|
106
112
|
post.likes_users_count.should == 2
|
107
113
|
post2.likes_users_count.should == 1
|
114
|
+
end
|
108
115
|
|
116
|
+
it "returns an ActiveRecord::Relation" do
|
117
|
+
Post.most_liked(5).should be_a(ActiveRecord::Relation)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "accepts a limit parameter" do
|
109
121
|
Post.most_liked(5).should == [post, post2]
|
110
122
|
end
|
111
123
|
|
124
|
+
it "accepts an offset parameter" do
|
125
|
+
Post.most_liked(5, 1).should == [post2]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "defaults limit to 5" do
|
129
|
+
Post.most_liked.should == [post, post2]
|
130
|
+
end
|
131
|
+
|
132
|
+
it "defaults offset to 0" do
|
133
|
+
Post.most_liked.should == [post, post2]
|
134
|
+
end
|
135
|
+
|
136
|
+
it "accepts a block to make custom queries" do
|
137
|
+
klass, id_list = Post.most_liked {|model, ids| [model, ids]}
|
138
|
+
|
139
|
+
klass.should == Post
|
140
|
+
id_list.should == [post.id.to_s, post2.id.to_s]
|
141
|
+
end
|
142
|
+
|
112
143
|
end # .most_liked
|
113
144
|
|
114
145
|
end # .acts_as_liked_by
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: likes_tracker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|