parole 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -0
- data/README.md +13 -5
- data/gemfiles/Gemfile.activerecord-4.0 +5 -0
- data/lib/generators/parole/templates/migration.rb +1 -0
- data/lib/parole/comment.rb +11 -0
- data/lib/parole/version.rb +1 -1
- data/spec/parole/comment_spec.rb +109 -0
- data/spec/parole/commentable_spec.rb +26 -92
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6ee830289243ba04c8d108f5d7f5735be8306e9
|
4
|
+
data.tar.gz: 8faf15de5fceaaa33ce9098bfe68a4e8c32c6a99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57bba72cecc135f0aaa16e5830761cef234ba6b81b30d5a69bab063523f4043642e7f501c444b6031b00175d84c7b458d52dbf00b14105af5dc4e54cf00c2a45
|
7
|
+
data.tar.gz: 0fc0fc9d30fce89c01a0fbb04a961ce79e715c172a2b80b8b89a0f80102472623a665d5f6bb488796982ee61d400a6fbf8799e0f94512c631112d82f808daa49
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
<p align="center">
|
2
|
+
<a href="https://github.com/mirego/parole">
|
3
|
+
<img src="http://i.imgur.com/QQlNfGL.png" alt="Parole" />
|
4
|
+
</a>
|
5
|
+
<br />
|
6
|
+
Parole adds the ability to comment on ActiveRecord records.
|
7
|
+
<br /><br />
|
8
|
+
<a href="https://rubygems.org/gems/parole"><img src="https://badge.fury.io/rb/parole.png" /></a>
|
9
|
+
<a href="https://codeclimate.com/github/mirego/parole"><img src="https://codeclimate.com/github/mirego/parole.png" /></a>
|
10
|
+
<a href="https://travis-ci.org/mirego/parole"><img src="https://travis-ci.org/mirego/parole.png?branch=master" /></a>
|
11
|
+
</p>
|
12
|
+
|
13
|
+
---
|
6
14
|
|
7
15
|
## Installation
|
8
16
|
|
data/lib/parole/comment.rb
CHANGED
@@ -21,6 +21,10 @@ module Parole
|
|
21
21
|
|
22
22
|
protected
|
23
23
|
|
24
|
+
# Update the commentable cache counter columns
|
25
|
+
#
|
26
|
+
# Look for a `<role>_comments_count` and a `comments_count` column
|
27
|
+
# in the commentable model and update their value with the count.
|
24
28
|
def update_cache_counters
|
25
29
|
role_method = :"#{self.role}_comments_count="
|
26
30
|
if commentable.respond_to?(role_method)
|
@@ -35,6 +39,11 @@ module Parole
|
|
35
39
|
commentable.save(validate: false)
|
36
40
|
end
|
37
41
|
|
42
|
+
# Make sure that the value of the `role` attribute is a valid role
|
43
|
+
# for the commentable.
|
44
|
+
#
|
45
|
+
# If the commentable doesn't have any comment roles, we make sure
|
46
|
+
# that the value is blank.
|
38
47
|
def ensure_valid_role_for_commentable
|
39
48
|
allowed_roles = commentable.class.commentable_options[:roles]
|
40
49
|
|
@@ -45,6 +54,8 @@ module Parole
|
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
# Make sure that the record we're commenting on is an instance
|
58
|
+
# of a commentable model.
|
48
59
|
def ensure_valid_commentable
|
49
60
|
klass = commentable.class
|
50
61
|
errors.add(:commentable, :invalid) unless klass.respond_to?(:acts_as_commentable?) && klass.acts_as_commentable?
|
data/lib/parole/version.rb
CHANGED
data/spec/parole/comment_spec.rb
CHANGED
@@ -1,4 +1,113 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Parole::Comment do
|
4
|
+
describe :ClassMethods do
|
5
|
+
describe :create do
|
6
|
+
context 'through general `comments` association' do
|
7
|
+
before do
|
8
|
+
spawn_comment_model
|
9
|
+
spawn_commenter_model 'User'
|
10
|
+
spawn_commentable_model 'Article'
|
11
|
+
|
12
|
+
run_migration do
|
13
|
+
create_table(:articles, force: true)
|
14
|
+
create_table(:users, force: true)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:commenter) { User.create }
|
19
|
+
let(:commentable) { Article.create }
|
20
|
+
|
21
|
+
context 'without role attribute' do
|
22
|
+
let(:comment) { commentable.comments.create(commenter: commenter, comment: 'Booya') }
|
23
|
+
|
24
|
+
it { expect(comment).to be_persisted }
|
25
|
+
it { expect(comment.comment).to eql 'Booya' }
|
26
|
+
it { expect(comment.commenter).to eql commenter }
|
27
|
+
it { expect(comment.commentable).to eql commentable }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'with role attribute' do
|
31
|
+
let(:comment) { commentable.comments.create(role: 'YEP', commenter: commenter, comment: 'Booya') }
|
32
|
+
it { expect(comment).to_not be_persisted }
|
33
|
+
it { expect(comment.errors.full_messages).to eql ['Role is invalid'] }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'through a role-specific comments association' do
|
38
|
+
before do
|
39
|
+
spawn_comment_model
|
40
|
+
spawn_commenter_model 'User'
|
41
|
+
spawn_commentable_model 'Article' do
|
42
|
+
acts_as_commentable roles: [:photos, :videos]
|
43
|
+
end
|
44
|
+
|
45
|
+
run_migration do
|
46
|
+
create_table(:articles, force: true)
|
47
|
+
create_table(:users, force: true)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:commenter) { User.create }
|
52
|
+
let(:commentable) { Article.create }
|
53
|
+
|
54
|
+
context 'with commentable role association method' do
|
55
|
+
let(:comment) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') }
|
56
|
+
|
57
|
+
it { expect(comment).to be_persisted }
|
58
|
+
it { expect(comment.role).to eql 'photos' }
|
59
|
+
it { expect(comment.comment).to eql 'Booya' }
|
60
|
+
it { expect(comment.commenter).to eql commenter }
|
61
|
+
it { expect(comment.commentable).to eql commentable }
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'with commentable main association method' do
|
65
|
+
context 'with valid role' do
|
66
|
+
let(:comment) { commentable.comments.create(role: 'photos', commenter: commenter, comment: 'Booya') }
|
67
|
+
|
68
|
+
it { expect(comment).to be_persisted }
|
69
|
+
it { expect(comment.role).to eql 'photos' }
|
70
|
+
it { expect(comment.comment).to eql 'Booya' }
|
71
|
+
it { expect(comment.commenter).to eql commenter }
|
72
|
+
it { expect(comment.commentable).to eql commentable }
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'with invalid role' do
|
76
|
+
let(:comment) { commentable.comments.create(role: 'NOPE', commenter: commenter, comment: 'Booya') }
|
77
|
+
it { expect(comment).to_not be_persisted }
|
78
|
+
it { expect(comment.errors.full_messages).to eql ['Role is invalid'] }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe :InstanceMethods do
|
86
|
+
describe :update_cache_counters do
|
87
|
+
before do
|
88
|
+
spawn_comment_model
|
89
|
+
spawn_commenter_model 'User'
|
90
|
+
spawn_commentable_model 'Article' do
|
91
|
+
acts_as_commentable roles: [:photos, :videos]
|
92
|
+
end
|
93
|
+
|
94
|
+
run_migration do
|
95
|
+
create_table(:users, force: true)
|
96
|
+
create_table(:articles, force: true) do |t|
|
97
|
+
t.integer :photos_comments_count, default: 0
|
98
|
+
t.integer :videos_comments_count, default: 0
|
99
|
+
t.integer :comments_count, default: 0
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
let(:commenter) { User.create }
|
105
|
+
let(:commentable) { Article.create }
|
106
|
+
let(:create_comment!) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') }
|
107
|
+
|
108
|
+
it { expect { create_comment! }.to change { commentable.reload.photos_comments_count }.from(0).to(1) }
|
109
|
+
it { expect { create_comment! }.to_not change { commentable.reload.videos_comments_count } }
|
110
|
+
it { expect { create_comment! }.to change { commentable.reload.comments_count }.from(0).to(1) }
|
111
|
+
end
|
112
|
+
end
|
4
113
|
end
|
@@ -1,107 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Parole::Commentable do
|
4
|
-
describe
|
5
|
-
|
6
|
-
spawn_comment_model
|
7
|
-
spawn_commenter_model 'User'
|
8
|
-
spawn_commentable_model 'Article'
|
4
|
+
describe 'general `comments` relation' do
|
5
|
+
let(:relation_class) { ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Comment }
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
let(:commenter) { User.create }
|
17
|
-
let(:commentable) { Article.create }
|
18
|
-
|
19
|
-
context 'without role attribute' do
|
20
|
-
let(:comment) { commentable.comments.create(commenter: commenter, comment: 'Booya') }
|
21
|
-
|
22
|
-
it { expect(comment).to be_persisted }
|
23
|
-
it { expect(comment.comment).to eql 'Booya' }
|
24
|
-
it { expect(comment.commenter).to eql commenter }
|
25
|
-
it { expect(comment.commentable).to eql commentable }
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with role attribute' do
|
29
|
-
let(:comment) { commentable.comments.create(role: 'YEP', commenter: commenter, comment: 'Booya') }
|
30
|
-
it { expect(comment).to_not be_persisted }
|
31
|
-
it { expect(comment.errors.full_messages).to eql ['Role is invalid'] }
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe 'role comments' do
|
36
|
-
before do
|
37
|
-
spawn_comment_model
|
38
|
-
spawn_commenter_model 'User'
|
39
|
-
spawn_commentable_model 'Article' do
|
40
|
-
acts_as_commentable roles: [:photos, :videos]
|
41
|
-
end
|
42
|
-
|
43
|
-
run_migration do
|
44
|
-
create_table(:articles, force: true)
|
45
|
-
create_table(:users, force: true)
|
46
|
-
end
|
47
|
-
end
|
7
|
+
context 'without defined roles' do
|
8
|
+
before do
|
9
|
+
spawn_comment_model
|
10
|
+
spawn_commentable_model 'Article'
|
48
11
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
context 'with commentable role association method' do
|
53
|
-
let(:comment) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') }
|
54
|
-
|
55
|
-
it { expect(comment).to be_persisted }
|
56
|
-
it { expect(comment.role).to eql 'photos' }
|
57
|
-
it { expect(comment.comment).to eql 'Booya' }
|
58
|
-
it { expect(comment.commenter).to eql commenter }
|
59
|
-
it { expect(comment.commentable).to eql commentable }
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'with commentable main association method' do
|
63
|
-
context 'with valid role' do
|
64
|
-
let(:comment) { commentable.comments.create(role: 'photos', commenter: commenter, comment: 'Booya') }
|
65
|
-
|
66
|
-
it { expect(comment).to be_persisted }
|
67
|
-
it { expect(comment.role).to eql 'photos' }
|
68
|
-
it { expect(comment.comment).to eql 'Booya' }
|
69
|
-
it { expect(comment.commenter).to eql commenter }
|
70
|
-
it { expect(comment.commentable).to eql commentable }
|
12
|
+
run_migration do
|
13
|
+
create_table(:articles, force: true)
|
14
|
+
end
|
71
15
|
end
|
72
16
|
|
73
|
-
|
74
|
-
|
75
|
-
it { expect(comment).to_not be_persisted }
|
76
|
-
it { expect(comment.errors.full_messages).to eql ['Role is invalid'] }
|
77
|
-
end
|
17
|
+
it { expect(Article.create.comments).to be_instance_of(ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Comment) }
|
18
|
+
it { expect(Article.create.comments.new.role).to be_blank }
|
78
19
|
end
|
79
|
-
end
|
80
20
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
21
|
+
context 'with roles' do
|
22
|
+
before do
|
23
|
+
spawn_comment_model
|
24
|
+
spawn_commentable_model 'Article' do
|
25
|
+
acts_as_commentable roles: [:photos, :videos]
|
26
|
+
end
|
88
27
|
|
89
|
-
|
90
|
-
|
91
|
-
create_table(:articles, force: true) do |t|
|
92
|
-
t.integer :photos_comments_count, default: 0
|
93
|
-
t.integer :videos_comments_count, default: 0
|
94
|
-
t.integer :comments_count, default: 0
|
28
|
+
run_migration do
|
29
|
+
create_table(:articles, force: true)
|
95
30
|
end
|
96
31
|
end
|
97
|
-
end
|
98
|
-
|
99
|
-
let(:commenter) { User.create }
|
100
|
-
let(:commentable) { Article.create }
|
101
|
-
let(:create_comment!) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') }
|
102
32
|
|
103
|
-
|
104
|
-
|
105
|
-
|
33
|
+
it { expect(Article.create.comments).to be_instance_of(relation_class) }
|
34
|
+
it { expect(Article.create.photos_comments).to be_instance_of(relation_class) }
|
35
|
+
it { expect(Article.create.videos_comments).to be_instance_of(relation_class) }
|
36
|
+
it { expect(Article.create.comments.new.role).to be_blank }
|
37
|
+
it { expect(Article.create.photos_comments.new.role).to eql 'photos' }
|
38
|
+
it { expect(Article.create.videos_comments.new.role).to eql 'videos' }
|
39
|
+
end
|
106
40
|
end
|
107
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parole
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rémi Prévost
|
@@ -103,9 +103,11 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- .gitignore
|
105
105
|
- .rspec
|
106
|
+
- .travis.yml
|
106
107
|
- Gemfile
|
107
108
|
- README.md
|
108
109
|
- Rakefile
|
110
|
+
- gemfiles/Gemfile.activerecord-4.0
|
109
111
|
- lib/generators/parole/USAGE
|
110
112
|
- lib/generators/parole/install_generator.rb
|
111
113
|
- lib/generators/parole/templates/migration.rb
|