the_comments 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc.example +1 -0
- data/.travis.yml +5 -0
- data/README.md +105 -425
- data/app/assets/javascripts/the_comments.js.coffee +12 -9
- data/app/assets/javascripts/the_comments_manage.js.coffee +19 -49
- data/app/assets/stylesheets/the_comments.css.scss +20 -29
- data/app/controllers/_templates_/comments_controller.rb +44 -0
- data/app/controllers/concerns/controller.rb +216 -0
- data/app/helpers/render_comments_tree_helper.rb +4 -7
- data/app/models/_templates_/comment.rb +38 -0
- data/app/models/concerns/comment.rb +103 -0
- data/app/models/concerns/comment_states.rb +80 -0
- data/app/models/concerns/commentable.rb +69 -0
- data/app/models/concerns/user.rb +52 -0
- data/app/views/the_comments/_tree.html.erb +3 -0
- data/app/views/the_comments/haml/_additional_info.html.haml +13 -0
- data/app/views/the_comments/{_comment.html.haml → haml/_comment.html.haml} +0 -0
- data/app/views/the_comments/haml/_comment_body.html.haml +20 -0
- data/app/views/the_comments/haml/_comment_edit.html.haml +26 -0
- data/app/views/the_comments/{_form.html.haml → haml/_form.html.haml} +8 -6
- data/app/views/the_comments/haml/_manage_controls.html.haml +27 -0
- data/app/views/the_comments/haml/_sidebar.html.haml +28 -0
- data/app/views/the_comments/haml/_tree.html.haml +4 -0
- data/app/views/the_comments/haml/index.html.haml +18 -0
- data/app/views/the_comments/haml/manage.html.haml +25 -0
- data/app/views/the_comments/haml/my_comments.html.haml +28 -0
- data/app/views/the_comments/slim/_additional_info.html.slim +13 -0
- data/app/views/the_comments/slim/_comment.html.slim +1 -0
- data/app/views/the_comments/slim/_comment_body.html.slim +20 -0
- data/app/views/the_comments/slim/_comment_edit.html.slim +26 -0
- data/app/views/the_comments/slim/_form.html.slim +27 -0
- data/app/views/the_comments/slim/_manage_controls.html.slim +27 -0
- data/app/views/the_comments/slim/_sidebar.html.slim +28 -0
- data/app/views/the_comments/slim/_tree.html.slim +4 -0
- data/app/views/the_comments/slim/index.html.slim +18 -0
- data/app/views/the_comments/slim/manage.html.slim +25 -0
- data/app/views/the_comments/slim/my_comments.html.slim +28 -0
- data/{lib/generators/the_comments/templates → config/initializers}/the_comments.rb +3 -0
- data/config/locales/en.yml +39 -14
- data/config/locales/ru.yml +67 -0
- data/config/routes.rb +17 -13
- data/db/migrate/20130101010101_change_user.rb +18 -0
- data/db/migrate/20130101010102_create_comments.rb +50 -0
- data/db/migrate/20130101010103_change_commentable.rb +13 -0
- data/docs/admin_ui_installation.md +145 -0
- data/docs/advanced_installation.md +182 -0
- data/docs/comment_api.md +58 -0
- data/docs/commentable_api.md +59 -0
- data/docs/config_file.md +27 -0
- data/docs/content_preprocessors.md +73 -0
- data/docs/customazation_of_views.md +30 -0
- data/docs/denormalization_and_recent_comments.md +40 -0
- data/docs/documentation.md +28 -0
- data/docs/mountable_routes.md +80 -0
- data/docs/pagination.md +123 -0
- data/docs/screencast.jpg +0 -0
- data/docs/user_api.md +75 -0
- data/docs/what_is_comcoms.md +63 -0
- data/docs/whats_wrong_with_other_gems.md +18 -0
- data/docs/where_is_example_application.md +37 -0
- data/gem_version.rb +3 -0
- data/lib/generators/the_comments/USAGE +31 -20
- data/lib/generators/the_comments/the_comments_generator.rb +35 -18
- data/lib/generators/the_comments/views_generator.rb +52 -16
- data/lib/the_comments/config.rb +14 -1
- data/lib/the_comments/version.rb +1 -3
- data/lib/the_comments.rb +10 -0
- data/spec/dummy_app/.gitignore +17 -0
- data/spec/dummy_app/.rspec +1 -0
- data/spec/dummy_app/.ruby-gemset +1 -0
- data/spec/dummy_app/.ruby-version +1 -0
- data/spec/dummy_app/Gemfile +43 -0
- data/spec/dummy_app/README.md +50 -0
- data/spec/dummy_app/Rakefile +6 -0
- data/spec/dummy_app/app/assets/images/.keep +0 -0
- data/spec/dummy_app/app/assets/javascripts/admin_panel.js +5 -0
- data/spec/dummy_app/app/assets/javascripts/application.js +16 -0
- data/spec/dummy_app/app/assets/stylesheets/admin_panel.css +3 -0
- data/spec/dummy_app/app/assets/stylesheets/app.css.scss +4 -0
- data/spec/dummy_app/app/assets/stylesheets/application.css +16 -0
- data/spec/dummy_app/app/controllers/application_controller.rb +7 -0
- data/{lib/generators/the_comments/templates → spec/dummy_app/app/controllers}/comments_controller.rb +3 -1
- data/spec/dummy_app/app/controllers/concerns/.keep +0 -0
- data/spec/dummy_app/app/controllers/posts_controller.rb +13 -0
- data/spec/dummy_app/app/controllers/users_controller.rb +7 -0
- data/spec/dummy_app/app/helpers/application_helper.rb +2 -0
- data/spec/dummy_app/app/mailers/.keep +0 -0
- data/spec/dummy_app/app/models/.keep +0 -0
- data/spec/dummy_app/app/models/comment.rb +32 -0
- data/spec/dummy_app/app/models/concerns/.keep +0 -0
- data/spec/dummy_app/app/models/post.rb +17 -0
- data/spec/dummy_app/app/models/user.rb +21 -0
- data/spec/dummy_app/app/views/layouts/admin.html.haml +25 -0
- data/spec/dummy_app/app/views/layouts/application.html.haml +20 -0
- data/spec/dummy_app/app/views/posts/index.html.haml +22 -0
- data/spec/dummy_app/app/views/posts/show.html.haml +7 -0
- data/spec/dummy_app/bin/bundle +3 -0
- data/spec/dummy_app/bin/rails +4 -0
- data/spec/dummy_app/bin/rake +4 -0
- data/spec/dummy_app/config/application.rb +23 -0
- data/spec/dummy_app/config/boot.rb +4 -0
- data/spec/dummy_app/config/database.yml +11 -0
- data/spec/dummy_app/config/environment.rb +5 -0
- data/spec/dummy_app/config/environments/development.rb +29 -0
- data/spec/dummy_app/config/environments/production.rb +80 -0
- data/spec/dummy_app/config/environments/test.rb +36 -0
- data/spec/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy_app/config/initializers/inflections.rb +16 -0
- data/spec/dummy_app/config/initializers/mime_types.rb +5 -0
- data/spec/dummy_app/config/initializers/secret_token.rb +12 -0
- data/spec/dummy_app/config/initializers/session_store.rb +3 -0
- data/spec/dummy_app/config/initializers/sorcery.rb +437 -0
- data/spec/dummy_app/config/initializers/the_comments.rb +13 -0
- data/spec/dummy_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy_app/config/locales/en.yml +23 -0
- data/spec/dummy_app/config/routes.rb +15 -0
- data/spec/dummy_app/config.ru +4 -0
- data/spec/dummy_app/db/migrate/20130712061503_sorcery_core.rb +16 -0
- data/spec/dummy_app/db/migrate/20130712065951_create_posts.rb +11 -0
- data/spec/dummy_app/db/migrate/20131027185332_change_user.the_comments_engine.rb +19 -0
- data/spec/dummy_app/db/migrate/20131027185333_create_comments.the_comments_engine.rb +51 -0
- data/spec/dummy_app/db/migrate/20131027185334_change_commentable.the_comments_engine.rb +14 -0
- data/spec/dummy_app/db/schema.rb +74 -0
- data/spec/dummy_app/db/seeds.rb +42 -0
- data/spec/dummy_app/lib/assets/.keep +0 -0
- data/spec/dummy_app/lib/tasks/.keep +0 -0
- data/spec/dummy_app/lib/tasks/app_bootstrap.rake +15 -0
- data/spec/dummy_app/log/.keep +0 -0
- data/spec/dummy_app/public/404.html +58 -0
- data/spec/dummy_app/public/422.html +58 -0
- data/spec/dummy_app/public/500.html +57 -0
- data/spec/dummy_app/public/favicon.ico +0 -0
- data/spec/dummy_app/public/robots.txt +5 -0
- data/spec/dummy_app/spec/factories/post.rb +6 -0
- data/spec/dummy_app/spec/factories/user.rb +6 -0
- data/spec/dummy_app/spec/models/user_counters_spec.rb +339 -0
- data/spec/dummy_app/spec/spec_helper.rb +29 -0
- data/spec/dummy_app/test/controllers/.keep +0 -0
- data/spec/dummy_app/test/fixtures/.keep +0 -0
- data/spec/dummy_app/test/helpers/.keep +0 -0
- data/spec/dummy_app/test/integration/.keep +0 -0
- data/spec/dummy_app/test/mailers/.keep +0 -0
- data/spec/dummy_app/test/models/.keep +0 -0
- data/spec/dummy_app/test/test_helper.rb +15 -0
- data/spec/dummy_app/vendor/assets/javascripts/.keep +0 -0
- data/spec/dummy_app/vendor/assets/stylesheets/.keep +0 -0
- data/views_converter.rb +16 -0
- metadata +223 -45
- data/app/controllers/concerns/the_comments_controller.rb +0 -229
- data/app/controllers/concerns/the_comments_ip_controller.rb +0 -17
- data/app/controllers/concerns/the_comments_user_agent_controller.rb +0 -15
- data/app/models/concerns/the_comments_base.rb +0 -69
- data/app/models/concerns/the_comments_black_ip.rb +0 -9
- data/app/models/concerns/the_comments_black_user_agent.rb +0 -9
- data/app/models/concerns/the_comments_commentable.rb +0 -66
- data/app/models/concerns/the_comments_states.rb +0 -65
- data/app/models/concerns/the_comments_user.rb +0 -32
- data/app/views/ip_black_lists/index.html.haml +0 -17
- data/app/views/the_comments/_comment_body.html.haml +0 -30
- data/app/views/the_comments/_manage_controls.html.haml +0 -4
- data/app/views/the_comments/_tree.html.haml +0 -4
- data/app/views/the_comments/index.html.haml +0 -19
- data/app/views/the_comments/manage.html.haml +0 -29
- data/app/views/user_agent_black_lists/index.html.haml +0 -17
- data/db/migrate/20130101010101_create_comments.rb +0 -90
- data/lib/generators/the_comments/templates/ip_black_list.rb +0 -3
- data/lib/generators/the_comments/templates/ip_black_lists_controller.rb +0 -10
- data/lib/generators/the_comments/templates/the_comments_black_ip.rb +0 -9
- data/lib/generators/the_comments/templates/the_comments_black_user_agent.rb +0 -9
- data/lib/generators/the_comments/templates/user_agent_black_list.rb +0 -3
- data/lib/generators/the_comments/templates/user_agent_black_lists_controller.rb +0 -10
data/.rvmrc.example
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rvm use ruby-1.9.3-p392@the_comments --create
|
data/.travis.yml
ADDED
data/README.md
CHANGED
|
@@ -1,522 +1,202 @@
|
|
|
1
|
-
|
|
1
|
+
### This Repo under development!
|
|
2
2
|
|
|
3
|
-
TheComments
|
|
3
|
+
# TheComments
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](http://badge.fury.io/rb/the_comments) | [](https://travis-ci.org/the-teacher/the_comments) | [](https://codeclimate.com/github/the-teacher/the_comments) | [(rubygems)](http://rubygems.org/gems/the_comments)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Screenshots
|
|
10
|
-
|
|
11
|
-
**click to zoom**
|
|
12
|
-
|
|
13
|
-
<table>
|
|
14
|
-
<tr>
|
|
15
|
-
<td width="20%">Guest view</td>
|
|
16
|
-
<td width="20%">Admin view</td>
|
|
17
|
-
<td width="20%">Edit</td>
|
|
18
|
-
<td width="20%">Cache counters & User Cabinet</td>
|
|
19
|
-
<td width="20%">Recent comments & Denormalization</td>
|
|
20
|
-
</tr>
|
|
21
|
-
<tr>
|
|
22
|
-
<td width="20%"><img width="100%" height="100%" src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_2.gif" alt="the_comments"></td>
|
|
23
|
-
<td width="20%"><img width="100%" height="100%" src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_1.gif" alt="the_comments"></td>
|
|
24
|
-
<td width="20%"><img width="100%" height="100%" src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_4.gif" alt="the_comments"></td>
|
|
25
|
-
<td width="20%"><img width="100%" height="100%" src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_3.gif" alt="the_comments"></td>
|
|
26
|
-
<td width="20%"><img width="100%" height="100%" src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_5.gif" alt="the_comments"></td>
|
|
27
|
-
</tr>
|
|
28
|
-
</table>
|
|
7
|
+
TheComments - probably, best commenting system for Rails
|
|
29
8
|
|
|
30
9
|
### Main features
|
|
31
10
|
|
|
32
11
|
* Threaded comments
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* Basic AntiSpam system
|
|
12
|
+
* Useful cache counters
|
|
13
|
+
* Admin UI for moderation
|
|
14
|
+
* Mountable Engine.routes
|
|
37
15
|
* Online Support via skype: **ilya.killich**
|
|
16
|
+
* [Denormalization](docs/denormalization_and_recent_comments.md) for Recent comments
|
|
17
|
+
* Production-ready commenting system for Rails 4+
|
|
18
|
+
* Designed for preprocessors Sanitize, Textile, Markdawn etc.
|
|
38
19
|
|
|
39
|
-
###
|
|
40
|
-
|
|
41
|
-
* [My hopes about comments system](#my-hopes-about-comments-system)
|
|
42
|
-
* [What's wrong with other gems?](#whats-wrong-with-other-gems)
|
|
43
|
-
* [Comments, Posted comments & ComComs](#comments-posted-comments-comcoms)
|
|
44
|
-
* [Denormalization and Recent comments](#denormalization)
|
|
45
|
-
* [Recent comments building](#recent-comments-building)
|
|
46
|
-
* [AntiSpam system](#antispam-system)
|
|
47
|
-
* [Customization](#customization)
|
|
48
|
-
* [User methods](#user-methods)
|
|
49
|
-
* [Commentable methods](#commentable-methods)
|
|
50
|
-
* [Online Support](#online-support)
|
|
51
|
-
* [About author](#about-author)
|
|
52
|
-
* [What about specs?](#what-about-specs)
|
|
53
|
-
|
|
20
|
+
### :books: [Documentation](docs/documentation.md)
|
|
54
21
|
|
|
55
|
-
##
|
|
22
|
+
## How to start dummy app (screencast)
|
|
56
23
|
|
|
57
|
-
|
|
24
|
+
[](http://vk.com/video_ext.php?oid=49225742&id=166578209&hash=10be1dba625149bb&hd=3)
|
|
58
25
|
|
|
59
|
-
|
|
26
|
+
## Quick Start Installation
|
|
60
27
|
|
|
61
|
-
|
|
28
|
+
### 1. Gems install
|
|
62
29
|
|
|
63
|
-
|
|
64
|
-
* [Code Installation](#code-installation)
|
|
65
|
-
* [Tuning](#tuning)
|
|
66
|
-
* [Using](#using)
|
|
67
|
-
|
|
68
|
-
## Gem Installation
|
|
69
|
-
|
|
70
|
-
**1)** change your Gemfile
|
|
30
|
+
**Gemfile**
|
|
71
31
|
|
|
72
32
|
```ruby
|
|
73
|
-
|
|
74
|
-
# But you can use other nested set gem, for example:
|
|
75
|
-
# gem 'nested_set' (github.com/skyeagle/nested_set)
|
|
76
|
-
gem 'awesome_nested_set'
|
|
77
|
-
|
|
78
|
-
# I use haml for default views
|
|
79
|
-
# You can remove this dependancy,
|
|
80
|
-
# but you will should rewrite default views with your template engine
|
|
81
|
-
gem 'haml'
|
|
82
|
-
|
|
83
|
-
# finally, this gem
|
|
84
|
-
gem 'the_comments'
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
**2)** bundle
|
|
33
|
+
gem 'the_comments', "~> 2.0"
|
|
88
34
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
```ruby
|
|
92
|
-
bundle exec rake the_comments_engine:install:migrations
|
|
35
|
+
gem 'haml' # or gem 'slim'
|
|
36
|
+
gem 'awesome_nested_set' # or same gem
|
|
93
37
|
```
|
|
94
38
|
|
|
95
|
-
**
|
|
39
|
+
**Bundle**
|
|
96
40
|
|
|
97
|
-
```
|
|
98
|
-
bundle
|
|
41
|
+
```
|
|
42
|
+
bundle
|
|
99
43
|
```
|
|
100
44
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
**1)** Assets
|
|
45
|
+
### 2. Migrations install
|
|
104
46
|
|
|
105
|
-
|
|
47
|
+
**Copy migrations**
|
|
106
48
|
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
//= require the_comments_manage
|
|
49
|
+
```
|
|
50
|
+
rake the_comments_engine:install:migrations
|
|
110
51
|
```
|
|
111
52
|
|
|
112
|
-
|
|
53
|
+
Will create:
|
|
113
54
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
55
|
+
* xxxxx_change_user.rb
|
|
56
|
+
* xxxxx_create_comments.rb
|
|
57
|
+
* xxxxx_change_commentable.rb
|
|
117
58
|
|
|
118
|
-
**
|
|
59
|
+
:warning: **Open and change xxxxx_change_commentable.rb migration**
|
|
119
60
|
|
|
120
61
|
```ruby
|
|
121
|
-
class
|
|
122
|
-
|
|
62
|
+
class ChangeCommentable < ActiveRecord::Migration
|
|
63
|
+
def change
|
|
64
|
+
# Additional fields to Commentable Models
|
|
65
|
+
# [:posts, :articles, ... ]
|
|
66
|
+
|
|
67
|
+
# There is only Post model is commentable
|
|
68
|
+
[:posts].each do |table_name|
|
|
69
|
+
change_table table_name do |t|
|
|
70
|
+
t.integer :draft_comments_count, default: 0
|
|
71
|
+
t.integer :published_comments_count, default: 0
|
|
72
|
+
t.integer :deleted_comments_count, default: 0
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
123
76
|
end
|
|
124
77
|
```
|
|
125
78
|
|
|
126
|
-
**
|
|
79
|
+
**Invoke migrations**
|
|
127
80
|
|
|
128
|
-
```ruby
|
|
129
|
-
bundle exec rails g the_comments install
|
|
130
81
|
```
|
|
131
|
-
|
|
132
|
-
*List of created files:*
|
|
133
|
-
|
|
134
|
-
```ruby
|
|
135
|
-
config/initializers/the_comments.rb
|
|
136
|
-
|
|
137
|
-
app/models/ip_black_list.rb
|
|
138
|
-
app/models/user_agent_black_list.rb
|
|
139
|
-
|
|
140
|
-
app/controllers/comments_controller.rb
|
|
141
|
-
app/controllers/ip_black_lists_controller.rb
|
|
142
|
-
app/controllers/user_agent_black_lists_controller.rb
|
|
82
|
+
rake db:migrate
|
|
143
83
|
```
|
|
144
84
|
|
|
145
|
-
|
|
85
|
+
### 3. Code install
|
|
146
86
|
|
|
147
87
|
```ruby
|
|
148
|
-
|
|
88
|
+
rails g the_comments install
|
|
149
89
|
```
|
|
150
90
|
|
|
151
|
-
|
|
91
|
+
Will create:
|
|
152
92
|
|
|
153
|
-
|
|
154
|
-
app/
|
|
155
|
-
app/
|
|
156
|
-
|
|
157
|
-
|
|
93
|
+
* config/initializers/the_comments.rb
|
|
94
|
+
* app/controllers/comments_controller.rb
|
|
95
|
+
* app/models/comment.rb
|
|
96
|
+
|
|
97
|
+
:warning: **Open each file and follow an instructions**
|
|
158
98
|
|
|
159
|
-
|
|
99
|
+
### 4. Models modifictions
|
|
160
100
|
|
|
161
|
-
|
|
101
|
+
**app/models/user.rb**
|
|
162
102
|
|
|
163
103
|
```ruby
|
|
164
104
|
class User < ActiveRecord::Base
|
|
165
|
-
include
|
|
105
|
+
include TheComments::User
|
|
106
|
+
|
|
107
|
+
has_many :posts
|
|
166
108
|
|
|
167
|
-
# Your implementation of role policy
|
|
168
109
|
def admin?
|
|
169
110
|
self == User.first
|
|
170
111
|
end
|
|
171
112
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
113
|
+
def comments_admin?
|
|
114
|
+
admin?
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def comments_moderator? comment
|
|
118
|
+
id == comment.holder_id
|
|
176
119
|
end
|
|
177
120
|
end
|
|
178
121
|
```
|
|
179
122
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
Read more about **commentable_title**, **commentable_url** and **commentable_state** methods here: [Denormalization and Recent comments](#denormalization)
|
|
123
|
+
**app/models/post.rb**
|
|
183
124
|
|
|
184
125
|
```ruby
|
|
185
|
-
class
|
|
186
|
-
include
|
|
126
|
+
class Post < ActiveRecord::Base
|
|
127
|
+
include TheComments::Commentable
|
|
128
|
+
|
|
129
|
+
belongs_to :user
|
|
187
130
|
|
|
131
|
+
# Denormalization methods
|
|
132
|
+
# Please, read about advanced using
|
|
188
133
|
def commentable_title
|
|
189
|
-
|
|
190
|
-
# for example: "My first blog post"
|
|
191
|
-
blog_post_name
|
|
134
|
+
"Undefined Post Title"
|
|
192
135
|
end
|
|
193
136
|
|
|
194
137
|
def commentable_url
|
|
195
|
-
#
|
|
196
|
-
# for example: "/blogs/1-my-first-blog-post"
|
|
197
|
-
['', self.class.to_s.tableize, slug_id].join('/')
|
|
138
|
+
"#"
|
|
198
139
|
end
|
|
199
140
|
|
|
200
141
|
def commentable_state
|
|
201
|
-
|
|
202
|
-
# for example: "draft"
|
|
203
|
-
self.ban_flag == true ? :banned : :published
|
|
142
|
+
"published"
|
|
204
143
|
end
|
|
205
144
|
end
|
|
206
145
|
```
|
|
207
146
|
|
|
208
|
-
###
|
|
147
|
+
### 5. Mount Engine routes
|
|
209
148
|
|
|
149
|
+
**config/routes.rb**
|
|
210
150
|
|
|
211
151
|
```ruby
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
# Define comment's avatar url
|
|
216
|
-
# Usually we use Comment#user (owner of comment) to define avatar
|
|
217
|
-
# @blog.comments.includes(:user) <= use includes(:user) to decrease queries count
|
|
218
|
-
# comment#user.avatar_url
|
|
219
|
-
|
|
220
|
-
# Simple way to define avatar url
|
|
221
|
-
def avatar_url
|
|
222
|
-
hash = Digest::MD5.hexdigest self.id.to_s
|
|
223
|
-
"http://www.gravatar.com/avatar/#{hash}?s=30&d=identicon"
|
|
224
|
-
end
|
|
152
|
+
MyApp::Application.routes.draw do
|
|
153
|
+
root 'posts#index'
|
|
154
|
+
resources :posts
|
|
225
155
|
|
|
226
|
-
#
|
|
227
|
-
# Expample for: gem 'RedCloth', gem 'sanitize'
|
|
228
|
-
# your personal SmilesProcessor
|
|
229
|
-
def prepare_content
|
|
230
|
-
text = self.raw_content
|
|
231
|
-
text = RedCloth.new(text).to_html
|
|
232
|
-
text = SmilesProcessor.new(text)
|
|
233
|
-
text = Sanitize.clean(text, Sanitize::Config::RELAXED)
|
|
234
|
-
self.content = text
|
|
235
|
-
end
|
|
236
|
-
end
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
## Using
|
|
156
|
+
# ...
|
|
240
157
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
```ruby
|
|
244
|
-
class BlogsController < ApplicationController
|
|
245
|
-
def show
|
|
246
|
-
@blog = Blog.where(id: params[:id]).with_states(:published).first
|
|
247
|
-
@comments = @blog.comments.with_state([:draft, :published]).nested_set
|
|
248
|
-
end
|
|
158
|
+
mount TheComments::Engine => '/', as: :comments
|
|
249
159
|
end
|
|
250
160
|
```
|
|
251
161
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
```ruby
|
|
255
|
-
%h1= @blog.title
|
|
256
|
-
%p= @blog.content
|
|
257
|
-
|
|
258
|
-
= render partial: 'the_comments/tree', locals: { commentable: @blog, comments_tree: @comments }
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
## Understanding
|
|
262
|
-
|
|
263
|
-
### My hopes about comments system
|
|
264
|
-
|
|
265
|
-
* Open comments for everybody (by default). *I hate user registration*
|
|
266
|
-
* Polymorphic comments for any AR Model
|
|
267
|
-
* Threading for comments (can be plain comments list)
|
|
268
|
-
* Cache counters for commentable objects and User
|
|
269
|
-
* Moderation for comments and simple Admin UI
|
|
270
|
-
* Spam traps instead Captcha. *I hate Captcha*
|
|
271
|
-
* Blacklists for IP and UserAgent
|
|
272
|
-
* Denormalization for fast and Request-free Recent comments building
|
|
273
|
-
* Ready for external content filters (<b>sanitize</b>, <b>RedCloth</b>, <b>Markdown</b>, etc)
|
|
274
|
-
* Highlighting and Jumping to comment via anchor
|
|
275
|
-
* Ready for Rails4 (and Rails::Engine)
|
|
276
|
-
* Ready for JQuery 1.9+
|
|
277
|
-
* Delete without destroy
|
|
278
|
-
|
|
279
|
-
### What's wrong with other gems?
|
|
280
|
-
|
|
281
|
-
Just look at [Ruby-Toolbox](https://www.ruby-toolbox.com/categories/rails_comments). What we can see?
|
|
282
|
-
|
|
283
|
-
* [Acts as commentable with threading](https://github.com/elight/acts_as_commentable_with_threading) - so, guys, where is the render helper for the tree? There is no helper! Should I make render helper for tree by myself? Nooooo!!! I'm so sorry, but I can't use this gem.
|
|
284
|
-
* [acts_as_commentable](https://github.com/jackdempsey/acts_as_commentable) - so, I can see code for models. But I can't see code for controllers and views. Unfortunately, there is no threading. It's not enough for me.
|
|
285
|
-
* [opinio](https://github.com/Draiken/opinio) - looks better, but there is no threading. I want to have more!
|
|
286
|
-
* [has_threaded_comments](https://github.com/aarongough/has_threaded_comments) - Nice work! Nice gem! Models, controllers, views, view helper for tree rendering! **But**, last activity 2 years ago, I need few features, I think - I can make it better.
|
|
287
|
-
|
|
288
|
-

|
|
289
|
-
|
|
290
|
-
## Comments, Posted comments, ComComs
|
|
291
|
-
|
|
292
|
-
### Posted comments
|
|
293
|
-
|
|
294
|
-
**@user.posted_comments** (has_many)
|
|
295
|
-
|
|
296
|
-
Set of comments, where current user is owner (creator).
|
|
297
|
-
|
|
298
|
-
```ruby
|
|
299
|
-
@my_comments = @user.posted_comments # => [comment, comment, ...]
|
|
300
|
-
|
|
301
|
-
@comment = @my_comments.first
|
|
302
|
-
@user.id == @comment.user_id # => true
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
### Comments
|
|
306
|
-
|
|
307
|
-
**@commentable.comments** (has_many)
|
|
308
|
-
|
|
309
|
-
Set of comments for this commentable object
|
|
310
|
-
|
|
311
|
-
```ruby
|
|
312
|
-
@comments = @blog.comments # => [comment, comment, ...]
|
|
313
|
-
|
|
314
|
-
@comment = @comments.first
|
|
315
|
-
@comment.commentable_id == @blog.id # => true
|
|
316
|
-
@comment.commentable_type == 'Blog' # => true
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
<b>(!) Attention:</b> User Model can be commentable object too!
|
|
320
|
-
|
|
321
|
-
```ruby
|
|
322
|
-
@comments = @user.comments # => [comment, comment, ...]
|
|
323
|
-
|
|
324
|
-
@comment = @comments.first
|
|
325
|
-
@comment.commentable_id == @user.id # => true
|
|
326
|
-
@comment.commentable_type == 'User' # => true
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
### ComComs (COMments of COMmentable objects)
|
|
330
|
-
|
|
331
|
-
**@user.comcoms** (has_many)
|
|
332
|
-
|
|
333
|
-
Set of All <b>COM</b>ments of All <b>COM</b>mentable objects of this User
|
|
334
|
-
|
|
335
|
-
```ruby
|
|
336
|
-
@comcoms = @user.comcoms # => [comment, comment, ...]
|
|
337
|
-
|
|
338
|
-
@comcom = @comcoms.first
|
|
339
|
-
@user.id == @comcom.holder_id # => true
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
**Comment#holder_id** should not be empty. Because we should to know, who is moderator of this comment.
|
|
343
|
-
|
|
344
|
-
```ruby
|
|
345
|
-
@user.id == @comment.holder_id # => true
|
|
346
|
-
# => This user should be MODERATOR for this comment
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
## Denormalization
|
|
350
|
-
|
|
351
|
-
For building of Recent comments list (for polymorphic relationship) we need to have many additional requests to database. It's classic problem of polymorphic comments.
|
|
352
|
-
|
|
353
|
-
I use denormalization of commentable objects to solve this problem.
|
|
354
|
-
|
|
355
|
-
My practice shows - We need 3 denormalized fields into comment for (request-free) building of recent comments list:
|
|
356
|
-
|
|
357
|
-
<img src="https://raw.github.com/the-teacher/the_comments/master/docs/the_comments_view_5.gif" alt="the_comments">
|
|
162
|
+
Please, read [documentation](docs/documentation.md) to learn more
|
|
358
163
|
|
|
359
|
-
|
|
360
|
-
* **Comment#commentable_url** - for example: "/posts/1-my-first-post-about-ruby-on-rails"
|
|
361
|
-
* **Comment#commentable_state** - for example: "draft"
|
|
164
|
+
### 6. Assets install
|
|
362
165
|
|
|
363
|
-
|
|
166
|
+
**app/assets/stylesheets/application.css**
|
|
364
167
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
Controller:
|
|
370
|
-
|
|
371
|
-
```ruby
|
|
372
|
-
@comments = Comment.with_state(:published)
|
|
373
|
-
.where(commentable_state: [:published])
|
|
374
|
-
.order('created_at DESC')
|
|
375
|
-
.page(params[:page])
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
View:
|
|
379
|
-
|
|
380
|
-
```ruby
|
|
381
|
-
- @comments.each do |comment|
|
|
382
|
-
%div
|
|
383
|
-
%p= comment.commentable_title
|
|
384
|
-
%p= link_to comment.commentable_title, comment.commentable_url
|
|
385
|
-
%p= comment.content
|
|
168
|
+
```css
|
|
169
|
+
/*
|
|
170
|
+
*= require the_comments
|
|
171
|
+
*/
|
|
386
172
|
```
|
|
387
173
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
**1) Moderation**
|
|
391
|
-
|
|
392
|
-
**2) User agent must have:**
|
|
393
|
-
|
|
394
|
-
* Cookies support
|
|
395
|
-
* JavaScript and Ajax support
|
|
396
|
-
|
|
397
|
-
_Usually spambots not support Cookies and JavaScript_
|
|
398
|
-
|
|
399
|
-
**3) Comment form mast have:**
|
|
400
|
-
|
|
401
|
-
* fake (hidden via css) fields
|
|
402
|
-
|
|
403
|
-
_Usually spambots puts data in fake inputs_
|
|
404
|
-
|
|
405
|
-
**4) Trap via time:**
|
|
406
|
-
|
|
407
|
-
* User should be few seconds on page, before comment sending (by default 5 sec)
|
|
174
|
+
**app/assets/javascripts/application.js**
|
|
408
175
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
**5) IP and User Agent blacklists**
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
### Strong dependencies of gem
|
|
415
|
-
|
|
416
|
-
```ruby
|
|
417
|
-
gem 'the_sortable_tree'
|
|
418
|
-
gem 'state_machine'
|
|
176
|
+
```js
|
|
177
|
+
//= require the_comments
|
|
419
178
|
```
|
|
420
179
|
|
|
421
|
-
|
|
422
|
-
* **state_machine** - states for moderation and callbacks for recalculating of counters when state of comment was changed
|
|
423
|
-
|
|
424
|
-
## Customization
|
|
425
|
-
|
|
426
|
-
You can use **generators** for copy files into your Application. After that you can customize almost everything
|
|
180
|
+
### 7. Controller code example
|
|
427
181
|
|
|
428
|
-
|
|
182
|
+
**app/controllers/posts_controllers.rb**
|
|
429
183
|
|
|
430
184
|
```ruby
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
```ruby
|
|
437
|
-
bundle exec rails g the_comments:views assets
|
|
438
|
-
bundle exec rails g the_comments:views views
|
|
185
|
+
def show
|
|
186
|
+
@post = Post.find params[:id]
|
|
187
|
+
@comments = @post.comments.with_state([:draft, :published])
|
|
188
|
+
end
|
|
439
189
|
```
|
|
440
190
|
|
|
441
|
-
|
|
191
|
+
### 8. View code example
|
|
442
192
|
|
|
443
|
-
|
|
193
|
+
**app/views/posts/show.html.haml**
|
|
444
194
|
|
|
445
|
-
```
|
|
446
|
-
|
|
195
|
+
```haml
|
|
196
|
+
= render partial: 'the_comments/tree', locals: { commentable: @post, comments_tree: @comments }
|
|
447
197
|
```
|
|
448
|
-
### User methods
|
|
449
|
-
|
|
450
|
-
* @user.<b>posted_comments</b> (all states)
|
|
451
|
-
* @user.<b>my_comments</b> (draft, published)
|
|
452
|
-
* @user.<b>my_comments_count</b>
|
|
453
|
-
|
|
454
|
-
Cache counters methods
|
|
455
|
-
|
|
456
|
-
* @user.<b>recalculate_my_comments_counter!</b>
|
|
457
|
-
* @user.<b>recalculate_comcoms_counters!</b>
|
|
458
|
-
|
|
459
|
-
Comments methods
|
|
460
|
-
|
|
461
|
-
* @user.<b>comments</b>
|
|
462
|
-
* @user.<b>comments_sum</b>
|
|
463
|
-
* @user.<b>draft_comments_count</b>
|
|
464
|
-
* @user.<b>published_comments_count</b>
|
|
465
|
-
* @user.<b>deleted_comments_count</b>
|
|
466
|
-
|
|
467
|
-
Comcoms methods
|
|
468
|
-
|
|
469
|
-
* @user.<b>comcoms</b>
|
|
470
|
-
* @user.<b>comcoms_sum</b>
|
|
471
|
-
* @user.<b>draft_comcoms_count</b>
|
|
472
|
-
* @user.<b>published_comcoms_count</b>
|
|
473
|
-
* @user.<b>deleted_comcoms_count</b>
|
|
474
|
-
|
|
475
|
-
### Commentable methods
|
|
476
|
-
|
|
477
|
-
* @post.<b>comments</b>
|
|
478
|
-
* @post.<b>draft_comments_count</b>
|
|
479
|
-
* @post.<b>published_comments_count</b>
|
|
480
|
-
* @post.<b>deleted_comments_count</b>
|
|
481
|
-
* @post.<b>comments_sum</b> (draft + published)
|
|
482
|
-
* @post.<b>recalculate_comments_counters!</b>
|
|
483
|
-
|
|
484
|
-
Denornalization methods
|
|
485
|
-
|
|
486
|
-
* @post.<b>commentable_title</b>
|
|
487
|
-
* @post.<b>commentable_url</b>
|
|
488
|
-
* @post.<b>commentable_state</b>
|
|
489
|
-
|
|
490
|
-
## Online Support
|
|
491
|
-
|
|
492
|
-
I need your opinion, ideas, user experience - that is why you can ask me about this gem via skype: **ilya.killich**
|
|
493
|
-
|
|
494
|
-
## About author
|
|
495
|
-
|
|
496
|
-
Yes, It's true - I was a school teacher in the past.
|
|
497
|
-
That's why my login is the-teacher.
|
|
498
|
-
Now I'm ruby/frontend developer and [food-blogger](http://open-cook.ru).
|
|
499
|
-
I learn, I teach, I make a code. And sorry for my English.
|
|
500
|
-
|
|
501
|
-
## What about specs?
|
|
502
|
-
|
|
503
|
-
This gem - just first prototype of my ideas about comment system.
|
|
504
|
-
Unfortunatly, I have no time to write many tests for this gem.
|
|
505
|
-
Release 1.0.0 works for my pet projects - it's enough for me.
|
|
506
|
-
If you have a problem with gem and you can to create coverage tests for this problem - I will be happy to get your pull request.
|
|
507
|
-
|
|
508
|
-
### Where I can find example of application with the_comments?
|
|
509
|
-
|
|
510
|
-
This gem is part of new version of my food-blog about modern russian home cuisine. There is code of my tasty CMS [open-cook](https://github.com/open-cook/open-cook)
|
|
511
|
-
|
|
512
|
-
## Contributing
|
|
513
198
|
|
|
514
|
-
|
|
515
|
-
2. Clone it into local folder
|
|
516
|
-
3. Add to Gemfile via **gem 'the_comments', path: '/path/to/gem/the_comments'**
|
|
517
|
-
4. Change code
|
|
518
|
-
5. git push origin master
|
|
519
|
-
6. Create pull request via github
|
|
199
|
+
<hr>
|
|
520
200
|
|
|
521
201
|
### MIT License
|
|
522
202
|
|
|
@@ -539,4 +219,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
539
219
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
540
220
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
541
221
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
542
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
222
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|