acts_as_liked 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89b280555a74b3018003f37dc97b717ade203e5e
4
- data.tar.gz: 5de17db8bf816939f79248faaca154593e3db576
3
+ metadata.gz: 3831b85d4ba7a642824e1e80a843c803103eda70
4
+ data.tar.gz: da424cb1348e49bc1739fed23479e59242440ca8
5
5
  SHA512:
6
- metadata.gz: 1217fe282fc125bdc937b28812229d0b1fc1e62c1c81a1cc6cc0ccfc06e1a4d8099ac7e591c755133c5f3df25dd6fbdcfe297bb3bcf88d239623a17eabecb94b
7
- data.tar.gz: 161324021f14f557a7c5a552fe89b92cc3e516f4d986fadec33f9792a6a8fdf1cafeb19ca2ddd70e859c5f13a6ceb9c5ec937af81e3a1c21879f4dba9f45b408
6
+ metadata.gz: a2c88e184b7339d8544a975d514a53c876aeaed5c2dabdc4995ec3cb965f22563795baa7524e0ac7c160e66fb840db655cee832db207d6adb14f95cdf36e9007
7
+ data.tar.gz: bb10d80195e58f8277686add9c7c8ec059cb0ea6c9d947d1ad4a82e06e685e7086a2c8d1b1981c72b4820fa1597077a3c989be941ec5bbe9d0264c8a7ad8d13e
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # ActsAsLiked [![Build Status](https://travis-ci.org/sungwoncho/acts_as_liked.svg?branch=master)](https://travis-ci.org/sungwoncho/acts_as_liked)
1
+ # ActsAsLiked [![Build Status](https://travis-ci.org/sungwoncho/acts_as_liked.svg?branch=master)](https://travis-ci.org/sungwoncho/acts_as_liked) [![Coverage Status](https://coveralls.io/repos/sungwoncho/acts_as_liked/badge.png?branch=master)](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 `Liker` or `Likeable`.
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 Food < ActiveRecord::Base
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 Person < ActiveRecord::Base
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
- # Creates a new like record for @user, and @post
53
- @user.like(@post)
56
+ # Count the number of likes of @post
57
+ @post.like_count
54
58
 
55
- # Destroys the like record
56
- @user.unlike(@post)
59
+ # Check if @post is liked by @user
60
+ @post.liked_by?(@user)
57
61
 
58
- # Check if @user has liked @post
59
- @user.liked?(@post) # Check if the user has liked the post
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
- Likeable:
69
+ *Liker*
63
70
 
64
71
  ```ruby
65
- # Count the number of likes of @post
66
- @post.like_count
72
+ # Create a new Like record for @user, and @post
73
+ @user.like(@post)
67
74
 
68
- # Check if @post is liked by @user
69
- @post.liked_by?(@user)
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
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "sqlite3"
26
26
  spec.add_development_dependency "activerecord"
27
27
  spec.add_development_dependency "generator_spec"
28
+ spec.add_development_dependency "coveralls"
28
29
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ActsAsLiked
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
@@ -1,6 +1,8 @@
1
1
  require 'acts_as_liked'
2
2
  require 'active_record'
3
3
  require 'support/models'
4
+ require 'coveralls'
5
+ Coveralls.wear!
4
6
 
5
7
  require 'support/like_macros.rb'
6
8
 
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
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-30 00:00:00.000000000 Z
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.1
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