commentable_on 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/Gemfile.lock +5 -3
- data/README.md +34 -5
- data/commentable_on.gemspec +3 -0
- data/lib/commentable_on/comment.rb +4 -0
- data/lib/commentable_on/commentable.rb +20 -0
- data/lib/commentable_on/commenter.rb +6 -2
- data/lib/commentable_on/version.rb +1 -1
- data/lib/generators/commentable_on/migration/migration_generator.rb +55 -0
- data/lib/generators/commentable_on/migration/templates/active_record/migration.erb +20 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 114f29fe05a7af6a7ab42c9a193244d31d8f2dc0df038dd9de3005fbe79009fd
|
4
|
+
data.tar.gz: 4516516f21e525daf05cd836aac4d728a046cb947452e92c113fb585a2e937e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ed87344389a6ebdea787203963b43802ce0bd0ec646bfbe85eb58b13e327af761f3f3d72f1b3302830586089ce7b2da581edef50b8c3da3dbd6876b83e83148
|
7
|
+
data.tar.gz: 58d8ce724fd9e0b3c5e846a650569fc466c5120250a696d37fc7359fdee779a8babd8c287d5cdf58d67e1e24d8a3dbad2e320306ca881046d3b520cb7b4065ab
|
data/Gemfile
CHANGED
@@ -7,7 +7,6 @@ gem "rake", "~> 12.0"
|
|
7
7
|
gem "rspec", "~> 3.6"
|
8
8
|
gem "sqlite3", "~> 1.5"
|
9
9
|
gem "factory_bot", "~> 6.2"
|
10
|
-
gem "activerecord", "~> 7.0", require: false
|
11
10
|
gem "rubocop", "= 1.35.1", require: false
|
12
11
|
gem "standard", "~> 1.16", ">= 1.16.1", require: false
|
13
12
|
gem "rubocop-rspec", require: false
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
commentable_on (
|
4
|
+
commentable_on (1.0.0)
|
5
|
+
ancestry (~> 4.2)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
@@ -16,6 +17,8 @@ GEM
|
|
16
17
|
i18n (>= 1.6, < 2)
|
17
18
|
minitest (>= 5.1)
|
18
19
|
tzinfo (~> 2.0)
|
20
|
+
ancestry (4.2.0)
|
21
|
+
activerecord (>= 5.2.6)
|
19
22
|
ast (2.4.2)
|
20
23
|
concurrent-ruby (1.1.10)
|
21
24
|
diff-lcs (1.5.0)
|
@@ -31,7 +34,7 @@ GEM
|
|
31
34
|
ast (~> 2.4.1)
|
32
35
|
rainbow (3.1.1)
|
33
36
|
rake (12.3.3)
|
34
|
-
regexp_parser (2.6.
|
37
|
+
regexp_parser (2.6.1)
|
35
38
|
rexml (3.2.5)
|
36
39
|
rspec (3.12.0)
|
37
40
|
rspec-core (~> 3.12.0)
|
@@ -79,7 +82,6 @@ PLATFORMS
|
|
79
82
|
ruby
|
80
83
|
|
81
84
|
DEPENDENCIES
|
82
|
-
activerecord (~> 7.0)
|
83
85
|
commentable_on!
|
84
86
|
factory_bot (~> 6.2)
|
85
87
|
rake (~> 12.0)
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# _**Under active development**_
|
2
|
-
|
3
1
|
# Commentable On Steroids
|
4
2
|
|
5
|
-
Adds comments functionality to Rails/ActiveRecord
|
3
|
+
Adds comments functionality to Rails/ActiveRecord models. It uses the [ancestry gem](https://github.com/stefankroes/ancestry) to add thread/reply support to comments.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -45,7 +43,31 @@ Add a comment to model instance:
|
|
45
43
|
@post.add_comment(commenter: current_user, body: "Awesome")
|
46
44
|
```
|
47
45
|
|
48
|
-
|
46
|
+
Create reply
|
47
|
+
```ruby
|
48
|
+
comment = commenter.first
|
49
|
+
@post.create_reply(comment: comment, commenter: current_user, body: "awesome reply")
|
50
|
+
```
|
51
|
+
|
52
|
+
Get all root comments
|
53
|
+
```ruby
|
54
|
+
@post = Post.find(params[:post_id])
|
55
|
+
@post.root_comments
|
56
|
+
```
|
57
|
+
|
58
|
+
Get all replies for a comment
|
59
|
+
```ruby
|
60
|
+
comment = @post.comments.first
|
61
|
+
@post.replies_for(comment)
|
62
|
+
```
|
63
|
+
|
64
|
+
Get thread for a comment
|
65
|
+
```ruby
|
66
|
+
comment = @post.comments.first
|
67
|
+
@post.thread_for(comment)
|
68
|
+
```
|
69
|
+
|
70
|
+
The commenter, add `acts_as_commenter` to commenter models
|
49
71
|
```ruby
|
50
72
|
class User < ActiveRecord::Base
|
51
73
|
acts_as_commenter
|
@@ -57,6 +79,12 @@ Add comment as a commenter
|
|
57
79
|
@post = Post.find(params[:post_id])
|
58
80
|
current_user.comment(commentable: @post, body: "awesome")
|
59
81
|
```
|
82
|
+
|
83
|
+
Reply to comment as a commenter
|
84
|
+
```ruby
|
85
|
+
comment = commenter.comments.first
|
86
|
+
current_user.reply_to(comment: comment, body: "awesome reply")
|
87
|
+
```
|
60
88
|
|
61
89
|
## Development
|
62
90
|
|
@@ -81,4 +109,5 @@ Everyone interacting in the CommentableOn project's codebases, issue trackers, c
|
|
81
109
|
|
82
110
|
- [ ] Add comment threading
|
83
111
|
- [ ] Add migration generators
|
84
|
-
- [ ] Automate gem release
|
112
|
+
- [ ] Automate gem release
|
113
|
+
- [ ] Add contribution guideline
|
data/commentable_on.gemspec
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
+
require "ancestry"
|
2
|
+
|
1
3
|
module CommentableOn
|
2
4
|
class Comment < ::ActiveRecord::Base
|
5
|
+
has_ancestry(primary_key_format: /\A[\w\-]+(\/[\w\-]+)*\z/, ancestry_column: :thread)
|
6
|
+
|
3
7
|
if defined?(ProtectedAttributes)
|
4
8
|
attr_accessible :commentable_id, :commentable_type, :commenter_id, :commenter_type, :commentable, :commenter, :body
|
5
9
|
end
|
@@ -14,5 +14,25 @@ module CommentableOn
|
|
14
14
|
comment = CommentableOn::Comment.new(commentable: self, commenter: commenter, body: body)
|
15
15
|
comment.save
|
16
16
|
end
|
17
|
+
|
18
|
+
def create_reply(comment:, commenter:, body:)
|
19
|
+
unless comment.instance_of?(CommentableOn::Comment)
|
20
|
+
return CommentableOn::Comment.create(commentable: self, commenter: commenter, body: body, parent_id: comment)
|
21
|
+
end
|
22
|
+
|
23
|
+
comment.children.create(commentable: self, commenter: commenter, body: body)
|
24
|
+
end
|
25
|
+
|
26
|
+
def root_comments
|
27
|
+
comments.roots
|
28
|
+
end
|
29
|
+
|
30
|
+
def replies_for(comment)
|
31
|
+
comments.children_of(comment)
|
32
|
+
end
|
33
|
+
|
34
|
+
def thread_for(comment)
|
35
|
+
comments.descendents_of(comment)
|
36
|
+
end
|
17
37
|
end
|
18
38
|
end
|
@@ -10,8 +10,12 @@ module CommentableOn
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
commentable.add_comment
|
13
|
+
def comment_on(commentable:, body:)
|
14
|
+
commentable.add_comment commenter: self, body: body
|
15
|
+
end
|
16
|
+
|
17
|
+
def reply_to(comment:, body:)
|
18
|
+
comment.commentable.create_reply comment: comment, commenter: self, body: body
|
15
19
|
end
|
16
20
|
end
|
17
21
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "rails/generators/migration"
|
2
|
+
|
3
|
+
module CommentableOn
|
4
|
+
class MigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
desc "Generates migration for commentable (comments table)"
|
8
|
+
|
9
|
+
def self.orm
|
10
|
+
Rails::Generators.options[:rails][:orm]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.source_root
|
14
|
+
File.join(File.dirname(__FILE__), "templates", (orm.to_s unless orm.instance_of?(String)))
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.orm_has_migration?
|
18
|
+
[:active_record].include? orm
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.next_migration_number(_path)
|
22
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_migration_file
|
26
|
+
if self.class.orm_has_migration?
|
27
|
+
migration_template "migration.erb", "db/migrate/commentable_on_migration.rb", migration_version: migration_version
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def migration_version
|
34
|
+
if rails5?
|
35
|
+
"[4.2]"
|
36
|
+
elsif rails6?
|
37
|
+
"[6.0]"
|
38
|
+
elsif rails7?
|
39
|
+
"[7.0]"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def rails5?
|
44
|
+
Rails.version.start_with? "5"
|
45
|
+
end
|
46
|
+
|
47
|
+
def rails6?
|
48
|
+
Rails.version.start_with? "6"
|
49
|
+
end
|
50
|
+
|
51
|
+
def rails7?
|
52
|
+
Rails.version.start_with? "7"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class CommentableOnMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def self.up
|
3
|
+
create_table :comments do |t|
|
4
|
+
t.references :commentable, polymorphic: true
|
5
|
+
t.references :commenter, polymorphic: true
|
6
|
+
t.text :body
|
7
|
+
t.string :thread
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :comments, :thread
|
13
|
+
add_index :comments, [:commenter_id, :commenter_type]
|
14
|
+
add_index :comments, [:commentable_id, :commentable_type]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :comments
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: commentable_on
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Ralak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
12
|
-
dependencies:
|
11
|
+
date: 2022-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ancestry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
13
27
|
description: Placeholder description text explaining how commentable on steroids will
|
14
28
|
save the world in 5 words or less
|
15
29
|
email:
|
@@ -39,6 +53,8 @@ files:
|
|
39
53
|
- lib/commentable_on/extenders/commentable.rb
|
40
54
|
- lib/commentable_on/extenders/commenter.rb
|
41
55
|
- lib/commentable_on/version.rb
|
56
|
+
- lib/generators/commentable_on/migration/migration_generator.rb
|
57
|
+
- lib/generators/commentable_on/migration/templates/active_record/migration.erb
|
42
58
|
homepage: https://github.com/samuelralak/commentable-on
|
43
59
|
licenses:
|
44
60
|
- MIT
|