comment_me 0.1.0alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f5501f6b8825f7338a5427d4eb7f236d1b6ebee7ffd30fcec3da8ee44260f14
4
+ data.tar.gz: b36b31c3fb04311c0f43fbbf0f425b0f85d9a3379a3446424c002f838b6bbfc2
5
+ SHA512:
6
+ metadata.gz: f007142bb33a78053df1c3e4dec214a1712f0c349e8109337c374568722bd1da00c7738322c978b0f50ae1ffb5f4ffdd82d5c2986fb504794a9d78b2591a1426
7
+ data.tar.gz: 4186378b2a18462b2e6a789a5d5dda512f26276cef300d2fbea9689ecc3bd4d9524dc72882135fecf6b605b2abe461b39ab29608d891d03a298c297b7f608f2d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # CommentMe
2
+
3
+ Attached comments for ActiveRecord models
4
+
5
+ [![Build Status](https://travis-ci.org/Kinedu/comment_me.svg?branch=master)](https://travis-ci.org/Kinedu/comment_me)
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'comment_me'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```bash
17
+ $ bundle install
18
+ ```
19
+
20
+ In the root directory:
21
+
22
+ ```bash
23
+ $ rails generate comment_me
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ The plugin generates a comment structure mounted on ActiveRecord models. Comments can be associated with any model (any model can have the functionality).
29
+
30
+ It also contains a control that can be used, or not, and that makes data persistence by inserting into the database.
31
+
32
+
33
+ ### Models
34
+
35
+ Make a model commentable.
36
+
37
+ ```ruby
38
+ class Article < ActiveRecord::Base
39
+ comment_me
40
+ end
41
+ ```
42
+
43
+ Then.
44
+
45
+ ```ruby
46
+ # Create new record
47
+ article = Article.create!
48
+
49
+ # Create a comment for article
50
+ article.comments.create emitter: "Armando Alejandre", message: "¡Hola mundo!"
51
+
52
+ # => #<Comment id: 29, emitter: "Armando Alejandre", message: "¡Hola mundo!", entity_type: "Article", entity_id: 2, comment_id: nil, created_at: "2019-10-11 06:37:13", updated_at: "2019-10-11 06:37:13">
53
+ # 2.6.3 :004 >
54
+
55
+ # See all comments
56
+ article.comments
57
+
58
+ # => #<ActiveRecord::Associations::CollectionProxy [#<Comment id: 32, emitter: "Armando Alejandre", message: "¡Hola mundo!", entity_type: "Article", entity_id: 2, comment_id: nil, created_at: "2019-10-11 06:40:58", updated_at: "2019-10-11 06:40:58">, #<Comment id: 31, emitter: "Armando Alejandre", message: "¡Hola mundo!", entity_type: "Article", entity_id: 2, comment_id: nil, created_at: "2019-10-11 06:40:57", updated_at: "2019-10-11 06:40:57">, #<Comment id: 30, emitter: "Armando Alejandre", message: "¡Hola mundo!", entity_type: "Article", entity_id: 2, comment_id: nil, created_at: "2019-10-11 06:40:56", updated_at: "2019-10-11 06:40:56">, #<Comment id: 29, emitter: "Armando Alejandre", message: "¡Hola mundo!", entity_type: "Article", entity_id: 2, comment_id: nil, created_at: "2019-10-11 06:37:13", updated_at: "2019-10-11 06:37:13">]>
59
+ ```
60
+
61
+ ### Controllers
62
+
63
+ A control is available to record comments and works with any model to which you want to add a comment.
64
+
65
+ To access the routes, call the #comment_me method into the route file.
66
+
67
+ ```ruby
68
+ # call method #comment_me:
69
+ # config/routes.rb
70
+
71
+ Rails.application.routes.draw do
72
+
73
+ comment_me
74
+ end
75
+
76
+ ```
77
+
78
+ Check the routes.
79
+
80
+ ```bach
81
+ rails routes
82
+ ```
83
+
84
+ More information about the routes visit the [Postman](https://documenter.getpostman.com/view/2691667/SVtVU7zQ) documentation.
85
+
86
+ ## Contributing
87
+
88
+ Bug report or pull request are welcome.
89
+
90
+ Make a pull request:
91
+
92
+ - Clone the repo
93
+ - Create your feature branch
94
+ - Commit your changes
95
+ - Push the branch
96
+ - Create new Pull-Request
97
+
98
+ Please write tests if necessary.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CommentMe'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+ require "rspec/core/rake_task"
19
+
20
+ RSpec::Core::RakeTask.new(:spec)
21
+ task default: :spec
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommentMe
4
+ module ActsAsCommentable
5
+ extend ActiveSupport::Concern
6
+
7
+ ModelCommentable = Module.new do
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ has_many :comments, -> { includes :comments }, as: :entity, dependent: :destroy
12
+ end
13
+ end
14
+
15
+ class_methods do
16
+ def comment_me
17
+ include ModelCommentable
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ # => including the method in ActiveRecord
24
+ ActiveRecord::Base.include CommentMe::ActsAsCommentable
@@ -0,0 +1,3 @@
1
+ class CommentMeBaseController < ActionController::Base
2
+ protect_from_forgery with: :null_session
3
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'comment_me_base_controller'
2
+
3
+ class CommentsController < CommentMeBaseController
4
+ before_action :set_commet, only: %i(show update destroy)
5
+
6
+ def show
7
+ render json: @comment
8
+ end
9
+
10
+ def create
11
+ @comment = Comment.new(comment_params)
12
+
13
+ if @comment.valid?
14
+ @comment.save
15
+
16
+ render json: @comment
17
+ else
18
+ render_comment_errors
19
+ end
20
+ end
21
+
22
+ def update
23
+ if @comment.update(comment_params)
24
+ render json: @comment
25
+ else
26
+ render_comment_errors
27
+ end
28
+ end
29
+
30
+ def destroy
31
+ @comment.destroy
32
+ end
33
+
34
+ private
35
+
36
+ def set_commet
37
+ @comment = Comment.find(params[:id])
38
+ rescue ActiveRecord::RecordNotFound
39
+ head :not_found
40
+ end
41
+
42
+ def comment_params
43
+ params.require(:comment).permit(
44
+ :entity_type,
45
+ :entity_id,
46
+ :emitter,
47
+ :message,
48
+ comments_attributes: [:emitter, :message]
49
+ )
50
+ end
51
+
52
+ def render_comment_errors
53
+ render json: @comment.errors, status: :unprocessable_entity
54
+ end
55
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'models/comment'
2
+ require_relative 'controllers/comments_controller'
@@ -0,0 +1,33 @@
1
+ class Comment < ActiveRecord::Base
2
+
3
+ # => associations
4
+ belongs_to :entity, polymorphic: true, optional: false
5
+ belongs_to :comment, optional: true
6
+ has_many :comments, dependent: :destroy
7
+
8
+ # => Validations
9
+ validates_presence_of :emitter, :message
10
+ validate :entity_existence, if: :entity_type?
11
+
12
+ # => *
13
+ def entity_exist?
14
+ entity_instance&.respond_to?(self.class.name.tableize)
15
+ end
16
+
17
+ # => *
18
+ def entity_instance
19
+ "#{entity_type}".constantize.find_by(id: entity_id)
20
+ end
21
+
22
+ private
23
+
24
+ # => *
25
+ def entity_existence
26
+ unless entity_exist?
27
+ errors.add(
28
+ :entity,
29
+ "#{entity_type} doesn't have association with Comment or there's no record in #{entity_type} witn id=#{entity_id}"
30
+ )
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module CommentMe
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+
4
+ # => Inserting routes for comments_controller
5
+ #
6
+ # Example:
7
+ #
8
+ # Rails.application.routes.draw do
9
+ #
10
+ # comment_me
11
+ # end
12
+ #
13
+ def comment_me
14
+ scope :operations, defaults: { format: :json } do
15
+ scope :comment_me do
16
+ scope :comments do
17
+ resources :comments, path: :/
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module CommentMe
2
+ VERSION = '0.1.0alpha'
3
+ end
data/lib/comment_me.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'comment_me/initializer'
2
+ require 'comment_me/routes'
3
+ require 'comment_me/acts_as_commentable'
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate access_token_migration Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,25 @@
1
+ class CommentMeGenerator < Rails::Generators::Base
2
+ include Rails::Generators::Migration
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ def migration
6
+ migration_template(
7
+ 'migration.rb',
8
+ 'db/migrate/create_comments.rb'
9
+ )
10
+ end
11
+
12
+ def rails5_and_up?
13
+ Rails::VERSION::MAJOR >= 5
14
+ end
15
+
16
+ def migration_version
17
+ if rails5_and_up?
18
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
19
+ end
20
+ end
21
+
22
+ def self.next_migration_number(dir)
23
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ class CreateComments < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :comments do |t|
4
+ t.string :emitter
5
+ t.text :message
6
+ t.references :entity, polymorphic: true, index: true
7
+ t.references :comment, foreign_key: true, index: true
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :comment_me do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comment_me
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0alpha
5
+ platform: ruby
6
+ authors:
7
+ - Armando Alejandre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.2.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.2.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.2.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: pg
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: jbuilder
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: shoulda-matchers
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: shoulda-callback-matchers
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: coveralls
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: pry
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: Attached comments for ActiveRecord models
146
+ email:
147
+ - armando1339@gmail.com
148
+ executables: []
149
+ extensions: []
150
+ extra_rdoc_files: []
151
+ files:
152
+ - MIT-LICENSE
153
+ - README.md
154
+ - Rakefile
155
+ - lib/comment_me.rb
156
+ - lib/comment_me/acts_as_commentable.rb
157
+ - lib/comment_me/controllers/comment_me_base_controller.rb
158
+ - lib/comment_me/controllers/comments_controller.rb
159
+ - lib/comment_me/initializer.rb
160
+ - lib/comment_me/models/comment.rb
161
+ - lib/comment_me/railtie.rb
162
+ - lib/comment_me/routes.rb
163
+ - lib/comment_me/version.rb
164
+ - lib/generators/comment_me/USAGE
165
+ - lib/generators/comment_me/comment_me_generator.rb
166
+ - lib/generators/comment_me/templates/migration.rb
167
+ - lib/tasks/comment_me_tasks.rake
168
+ homepage: https://www.kinedu.com/
169
+ licenses:
170
+ - MIT
171
+ metadata:
172
+ source_code_uri: https://github.com/Kinedu/comment_me
173
+ allowed_push_host: https://rubygems.org
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">"
186
+ - !ruby/object:Gem::Version
187
+ version: 1.3.1
188
+ requirements: []
189
+ rubygems_version: 3.0.6
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Attached comments for ActiveRecord models
193
+ test_files: []