commentable_on 0.1.0 → 1.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
  SHA256:
3
- metadata.gz: 977c9733dc850169abece4df1029b7d41777b97138b2a2abbd9df4224118e81c
4
- data.tar.gz: b0cbcf3f8ede3685d03dc4bf706bace452cbe8c95ee1ff58e141b66d4c451875
3
+ metadata.gz: 7eaced595b0d8d8c0bc04edf5096cfbed04a70051e25d151535ab4dafbdb5063
4
+ data.tar.gz: 783c1c906f4e8cc5e66b4f8863db720ffacc4a1daefa283519ebe525c3968e63
5
5
  SHA512:
6
- metadata.gz: 2c1c4b801b7a65535a9e8595c15d94745d0cad32506264ba33647c5515d98b5bdee4fa81bea0c73861eaeeb6ca0b41f620b6facd26f2b8a033da36965c3a099e
7
- data.tar.gz: 60dea80bd2cf24badd245fe637d3345837a26d7a9b7c8e0caadfbc9188a8396be82fd41ac9f51b6d32cf5a8a3a5d550df2aab5d23137d2e12467b9dd5d74c979
6
+ metadata.gz: 20b46a4ae92df395d359ff83a93e8b79e700dc84c14cb7b2bc6a27e6df11892e88385ea9db30e34fd01179289e8e71f42a32b2b273951416156a85ed0bf8c1d3
7
+ data.tar.gz: 627f1fa8c90f8952fba07ee25572b4c62cd966a0ec46d98baccdded88f2eb97c51c5a0b4ddcd0dd6566a1928d16012df5011425af88b2166bd490727282ae7cb
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 (0.1.0)
4
+ commentable_on (1.1.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.0)
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 modules
3
+ Adds comments functionality to Rails/ActiveRecord models. It uses the [ancestry gem](https://github.com/stefankroes/ancestry) to add thread/reply.
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
- The commenter, add `acts_as_commenter` to commenter models for reserve functionality
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
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.email = ["thesamuelralak@gmail.com"]
8
8
 
9
9
  spec.summary = "Provides comment functionality with replies"
10
- spec.description = "Placeholder description text explaining how commentable on steroids will save the world in 5 words or less"
10
+ spec.description = "Adds comments functionality to Rails/ActiveRecord models. It uses the [ancestry gem](https://github.com/stefankroes/ancestry) to add thread/reply"
11
11
  spec.homepage = "https://github.com/samuelralak/commentable-on"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
@@ -26,4 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.bindir = "exe"
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
+
30
+ # Gem dependencies
31
+ spec.add_dependency "ancestry", "~> 4.2"
29
32
  end
@@ -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 comment(commentable:, body:)
14
- commentable.add_comment args.merge(commenter: self, body: body)
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
@@ -1,3 +1,3 @@
1
1
  module CommentableOn
2
- VERSION = "0.1.0"
2
+ VERSION = "1.1.0"
3
3
  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,17 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commentable_on
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.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-16 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Placeholder description text explaining how commentable on steroids will
14
- save the world in 5 words or less
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'
27
+ description: Adds comments functionality to Rails/ActiveRecord models. It uses the
28
+ [ancestry gem](https://github.com/stefankroes/ancestry) to add thread/reply
15
29
  email:
16
30
  - thesamuelralak@gmail.com
17
31
  executables: []
@@ -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