acts_as_liked 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +26 -16
- data/acts_as_liked.gemspec +1 -0
- data/converalls.yml +1 -0
- data/lib/acts_as_liked/likeable.rb +11 -0
- data/lib/acts_as_liked/version.rb +1 -1
- data/spec/likeable_spec.rb +54 -0
- data/spec/spec_helper.rb +2 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3831b85d4ba7a642824e1e80a843c803103eda70
|
4
|
+
data.tar.gz: da424cb1348e49bc1739fed23479e59242440ca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2c88e184b7339d8544a975d514a53c876aeaed5c2dabdc4995ec3cb965f22563795baa7524e0ac7c160e66fb840db655cee832db207d6adb14f95cdf36e9007
|
7
|
+
data.tar.gz: bb10d80195e58f8277686add9c7c8ec059cb0ea6c9d947d1ad4a82e06e685e7086a2c8d1b1981c72b4820fa1597077a3c989be941ec5bbe9d0264c8a7ad8d13e
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# ActsAsLiked [](https://travis-ci.org/sungwoncho/acts_as_liked)
|
1
|
+
# ActsAsLiked [](https://travis-ci.org/sungwoncho/acts_as_liked) [](https://coveralls.io/r/sungwoncho/acts_as_liked?branch=master)
|
2
2
|
|
3
|
-
Add like feature to any Active Record models through polymorphic association. Designate any models to act as a
|
3
|
+
Add like feature to any Active Record models through polymorphic association. Designate any models to act as a *Liker* or *Likeable*.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -29,7 +29,7 @@ And don't forget to migrate your database
|
|
29
29
|
Add `acts_as_likeable` to any models, and its instances can be liked by other models.
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
class
|
32
|
+
class Post < ActiveRecord::Base
|
33
33
|
acts_as_likeable
|
34
34
|
end
|
35
35
|
```
|
@@ -39,34 +39,44 @@ end
|
|
39
39
|
Add `acts_as_liker` to any models, and it can like instances of other models.
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
class
|
42
|
+
class User < ActiveRecord::Base
|
43
43
|
acts_as_liker
|
44
44
|
end
|
45
45
|
```
|
46
46
|
|
47
|
+
It is not necessary to use both `acts_as_liker` and `acts_as_likeable`. You can simply drop in one of them into your model and you will be good to go.
|
48
|
+
|
47
49
|
### API
|
48
50
|
|
49
|
-
Liker
|
51
|
+
Following APIs will be provided to *Likeable* and *Liker*.
|
52
|
+
|
53
|
+
*Likeable*
|
50
54
|
|
51
55
|
```ruby
|
52
|
-
#
|
53
|
-
@
|
56
|
+
# Count the number of likes of @post
|
57
|
+
@post.like_count
|
54
58
|
|
55
|
-
#
|
56
|
-
@
|
59
|
+
# Check if @post is liked by @user
|
60
|
+
@post.liked_by?(@user)
|
57
61
|
|
58
|
-
#
|
59
|
-
@
|
62
|
+
# Create a new Like record for @user, and @post
|
63
|
+
@post.liked_by(@user)
|
64
|
+
|
65
|
+
# Destroy the Like record
|
66
|
+
@post.unliked_by(@user)
|
60
67
|
```
|
61
68
|
|
62
|
-
|
69
|
+
*Liker*
|
63
70
|
|
64
71
|
```ruby
|
65
|
-
#
|
66
|
-
@post
|
72
|
+
# Create a new Like record for @user, and @post
|
73
|
+
@user.like(@post)
|
67
74
|
|
68
|
-
#
|
69
|
-
@
|
75
|
+
# Destroy the Like record
|
76
|
+
@user.unlike(@post)
|
77
|
+
|
78
|
+
# Check if @user has liked @post
|
79
|
+
@user.liked?(@post)
|
70
80
|
```
|
71
81
|
|
72
82
|
## Contributing
|
data/acts_as_liked.gemspec
CHANGED
data/converalls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
@@ -24,6 +24,17 @@ module ActsAsLiked
|
|
24
24
|
def liked_by?(liker)
|
25
25
|
likes.find_by(liker_id: liker.id, liker_type: liker.class.base_class.name).present?
|
26
26
|
end
|
27
|
+
|
28
|
+
def liked_by(liker)
|
29
|
+
unless liker.liked?(self)
|
30
|
+
likes.create(liker_id: liker.id, liker_type: liker.class.base_class.name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def unliked_by(liker)
|
35
|
+
like_record = likes.find_by(liker_id: liker.id, liker_type: liker.class.base_class.name)
|
36
|
+
like_record.try(:destroy)
|
37
|
+
end
|
27
38
|
end
|
28
39
|
|
29
40
|
end
|
data/spec/likeable_spec.rb
CHANGED
@@ -42,4 +42,58 @@ describe Food, focus: true do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
describe "#liked_by" do
|
47
|
+
context "when liker has not liked likabled" do
|
48
|
+
it "should create Like" do
|
49
|
+
expect{
|
50
|
+
cheese.liked_by(charlie)
|
51
|
+
}.to change(Like, :count).by(1)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set Like to belong to liker" do
|
55
|
+
cheese.liked_by(charlie)
|
56
|
+
like = Like.find_by(likeable_id: cheese.id, likeable_type: cheese.class.base_class.name, liker_id: charlie.id, liker_type: charlie.class.base_class.name)
|
57
|
+
|
58
|
+
expect(like.liker).to eq charlie
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should set Like to belong to likeable" do
|
62
|
+
cheese.liked_by(charlie)
|
63
|
+
like = Like.find_by(likeable_id: cheese.id, likeable_type: cheese.class.base_class.name, liker_id: charlie.id, liker_type: charlie.class.base_class.name)
|
64
|
+
|
65
|
+
expect(like.likeable).to eq cheese
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when liker has already liked likeable" do
|
70
|
+
it "should not create Like" do
|
71
|
+
create_like(charlie, cheese)
|
72
|
+
|
73
|
+
expect {
|
74
|
+
cheese.liked_by(charlie)
|
75
|
+
}.to change(Like, :count).by(0)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#unliked_by" do
|
81
|
+
context "when liker has liked likedable" do
|
82
|
+
it "should destroy the like record" do
|
83
|
+
create_like(charlie, cheese)
|
84
|
+
|
85
|
+
expect {
|
86
|
+
cheese.unliked_by(charlie)
|
87
|
+
}.to change(Like, :count).by(-1)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when liker has not liked likeable" do
|
92
|
+
it "should not destroy any Likes" do
|
93
|
+
expect {
|
94
|
+
cheese.unliked_by(charlie)
|
95
|
+
}.to change(Like, :count).by(0)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
45
99
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_liked
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sung Won Cho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11
|
11
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: coveralls
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description:
|
126
140
|
email:
|
127
141
|
- mikeswcho@gmail.com
|
@@ -138,6 +152,7 @@ files:
|
|
138
152
|
- README.md
|
139
153
|
- Rakefile
|
140
154
|
- acts_as_liked.gemspec
|
155
|
+
- converalls.yml
|
141
156
|
- lib/acts_as_liked.rb
|
142
157
|
- lib/acts_as_liked/likeable.rb
|
143
158
|
- lib/acts_as_liked/liker.rb
|
@@ -176,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
191
|
version: '0'
|
177
192
|
requirements: []
|
178
193
|
rubyforge_project:
|
179
|
-
rubygems_version: 2.4.
|
194
|
+
rubygems_version: 2.4.4
|
180
195
|
signing_key:
|
181
196
|
specification_version: 4
|
182
197
|
summary: Add like feature to any Active Record models
|