parole 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +26 -0
- data/lib/parole/comment.rb +5 -6
- data/lib/parole/commentable.rb +1 -2
- data/lib/parole/version.rb +1 -1
- data/lib/parole.rb +7 -3
- data/spec/parole/comment_spec.rb +57 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: def12cc4e8ebdfb3f39c397a95eef9443af3822f
|
4
|
+
data.tar.gz: a7addb2a86084f9267e34de818f1f9f20d59745c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d9e9c53c499ace305e53f61f1e2c2f0f6deebeff566bd1b4f7849e774f1ba8373e29ed3e3c52fe0e958863b6eeeb3b4c51c96874ff3e6e3b24a6d1378d32bfc
|
7
|
+
data.tar.gz: cf0aea744c44063f9f4c15a35f9805e243071dc93433174026bd9d1bed125e106830c9bc3c3d695adde127b56198cd8af6730b9b07e3c5a74312f22902b033dd
|
data/LICENSE.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2013, Mirego
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
- Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
- Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
- Neither the name of the Mirego nor the names of its contributors may
|
13
|
+
be used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
21
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/lib/parole/comment.rb
CHANGED
@@ -12,11 +12,11 @@ module Parole
|
|
12
12
|
after_destroy :update_cache_counters
|
13
13
|
|
14
14
|
# Validations
|
15
|
-
validate :ensure_valid_role_for_commentable
|
15
|
+
validate :ensure_valid_role_for_commentable, if: lambda { commentable.present? && commentable.commentable? }
|
16
16
|
validate :ensure_valid_commentable
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
validates :commenter, presence: true
|
18
|
+
validates :commentable, presence: true
|
19
|
+
validates :comment, presence: true
|
20
20
|
end
|
21
21
|
|
22
22
|
protected
|
@@ -57,8 +57,7 @@ module Parole
|
|
57
57
|
# Make sure that the record we're commenting on is an instance
|
58
58
|
# of a commentable model.
|
59
59
|
def ensure_valid_commentable
|
60
|
-
|
61
|
-
errors.add(:commentable, :invalid) unless klass.respond_to?(:acts_as_commentable?) && klass.acts_as_commentable?
|
60
|
+
errors.add(:commentable, :invalid) unless commentable.respond_to?(:commentable?) && commentable.commentable?
|
62
61
|
end
|
63
62
|
end
|
64
63
|
end
|
data/lib/parole/commentable.rb
CHANGED
@@ -11,8 +11,7 @@ module Parole
|
|
11
11
|
|
12
12
|
# Role-specific comments for the record
|
13
13
|
commentable_options.fetch(:roles).each do |role|
|
14
|
-
|
15
|
-
has_many :"#{role}_comments", lambda { where(role: role) }, options
|
14
|
+
has_many :"#{role}_comments", lambda { where(role: role) }, association_options
|
16
15
|
end
|
17
16
|
end
|
18
17
|
end
|
data/lib/parole/version.rb
CHANGED
data/lib/parole.rb
CHANGED
@@ -10,8 +10,8 @@ class ActiveRecord::Base
|
|
10
10
|
def self.acts_as_commentable(options = {})
|
11
11
|
Parole.commentable_classes << self
|
12
12
|
|
13
|
-
class_attribute :commentable_options, :
|
14
|
-
self.
|
13
|
+
class_attribute :commentable_options, :actually_acts_as_commentable
|
14
|
+
self.actually_acts_as_commentable = true
|
15
15
|
self.commentable_options = options.reverse_merge(roles: [])
|
16
16
|
self.commentable_options[:roles] = commentable_options[:roles].map(&:to_s)
|
17
17
|
|
@@ -19,12 +19,16 @@ class ActiveRecord::Base
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.acts_as_commentable?
|
22
|
-
|
22
|
+
self.respond_to?(:actually_acts_as_commentable) && self.actually_acts_as_commentable
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.acts_as_comment(*args)
|
26
26
|
include Parole::Comment
|
27
27
|
end
|
28
|
+
|
29
|
+
def commentable?
|
30
|
+
self.class.acts_as_commentable?
|
31
|
+
end
|
28
32
|
end
|
29
33
|
|
30
34
|
module Parole
|
data/spec/parole/comment_spec.rb
CHANGED
@@ -1,6 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Parole::Comment do
|
4
|
+
describe :Validations do
|
5
|
+
before do
|
6
|
+
spawn_comment_model
|
7
|
+
spawn_commenter_model 'User'
|
8
|
+
spawn_commentable_model 'Article'
|
9
|
+
|
10
|
+
run_migration do
|
11
|
+
create_table(:users, force: true)
|
12
|
+
create_table(:articles, force: true)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:errors) do
|
17
|
+
subject.valid?
|
18
|
+
subject.errors.full_messages
|
19
|
+
end
|
20
|
+
|
21
|
+
describe :validates_comment do
|
22
|
+
subject { Comment.new }
|
23
|
+
it { expect(errors).to include("Comment can't be blank") }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe :validates_commenter_presence do
|
27
|
+
subject { Comment.new }
|
28
|
+
it { expect(errors).to include("Commenter can't be blank") }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe :validates_commentable_presence do
|
32
|
+
subject { Comment.new }
|
33
|
+
it { expect(errors).to include("Commentable can't be blank") }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe :validates_commentable do
|
37
|
+
subject { Comment.new(commentable: User.new) }
|
38
|
+
it { expect(errors).to include("Commentable is invalid") }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
4
42
|
describe :ClassMethods do
|
5
43
|
describe :create do
|
6
44
|
context 'through general `comments` association' do
|
@@ -110,4 +148,23 @@ describe Parole::Comment do
|
|
110
148
|
it { expect { create_comment! }.to change { commentable.reload.comments_count }.from(0).to(1) }
|
111
149
|
end
|
112
150
|
end
|
151
|
+
|
152
|
+
describe :ensure_valid_role_for_commentable do
|
153
|
+
before do
|
154
|
+
spawn_comment_model
|
155
|
+
spawn_commenter_model 'User'
|
156
|
+
|
157
|
+
run_migration do
|
158
|
+
create_table(:users, force: true)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
let(:commenter) { User.create }
|
163
|
+
|
164
|
+
context 'without associated commentable' do
|
165
|
+
let(:comment) { Comment.new(commenter: commenter, comment: 'Booya') }
|
166
|
+
|
167
|
+
it { expect { comment.valid? }.to_not raise_error }
|
168
|
+
end
|
169
|
+
end
|
113
170
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rémi Prévost
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- .rspec
|
106
106
|
- .travis.yml
|
107
107
|
- Gemfile
|
108
|
+
- LICENSE.md
|
108
109
|
- README.md
|
109
110
|
- Rakefile
|
110
111
|
- gemfiles/Gemfile.activerecord-4.0
|