the_comments 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. data/.rvmrc.example +1 -0
  2. data/.travis.yml +5 -0
  3. data/README.md +105 -425
  4. data/app/assets/javascripts/the_comments.js.coffee +12 -9
  5. data/app/assets/javascripts/the_comments_manage.js.coffee +19 -49
  6. data/app/assets/stylesheets/the_comments.css.scss +20 -29
  7. data/app/controllers/_templates_/comments_controller.rb +44 -0
  8. data/app/controllers/concerns/controller.rb +216 -0
  9. data/app/helpers/render_comments_tree_helper.rb +4 -7
  10. data/app/models/_templates_/comment.rb +38 -0
  11. data/app/models/concerns/comment.rb +103 -0
  12. data/app/models/concerns/comment_states.rb +80 -0
  13. data/app/models/concerns/commentable.rb +69 -0
  14. data/app/models/concerns/user.rb +52 -0
  15. data/app/views/the_comments/_tree.html.erb +3 -0
  16. data/app/views/the_comments/haml/_additional_info.html.haml +13 -0
  17. data/app/views/the_comments/{_comment.html.haml → haml/_comment.html.haml} +0 -0
  18. data/app/views/the_comments/haml/_comment_body.html.haml +20 -0
  19. data/app/views/the_comments/haml/_comment_edit.html.haml +26 -0
  20. data/app/views/the_comments/{_form.html.haml → haml/_form.html.haml} +8 -6
  21. data/app/views/the_comments/haml/_manage_controls.html.haml +27 -0
  22. data/app/views/the_comments/haml/_sidebar.html.haml +28 -0
  23. data/app/views/the_comments/haml/_tree.html.haml +4 -0
  24. data/app/views/the_comments/haml/index.html.haml +18 -0
  25. data/app/views/the_comments/haml/manage.html.haml +25 -0
  26. data/app/views/the_comments/haml/my_comments.html.haml +28 -0
  27. data/app/views/the_comments/slim/_additional_info.html.slim +13 -0
  28. data/app/views/the_comments/slim/_comment.html.slim +1 -0
  29. data/app/views/the_comments/slim/_comment_body.html.slim +20 -0
  30. data/app/views/the_comments/slim/_comment_edit.html.slim +26 -0
  31. data/app/views/the_comments/slim/_form.html.slim +27 -0
  32. data/app/views/the_comments/slim/_manage_controls.html.slim +27 -0
  33. data/app/views/the_comments/slim/_sidebar.html.slim +28 -0
  34. data/app/views/the_comments/slim/_tree.html.slim +4 -0
  35. data/app/views/the_comments/slim/index.html.slim +18 -0
  36. data/app/views/the_comments/slim/manage.html.slim +25 -0
  37. data/app/views/the_comments/slim/my_comments.html.slim +28 -0
  38. data/{lib/generators/the_comments/templates → config/initializers}/the_comments.rb +3 -0
  39. data/config/locales/en.yml +39 -14
  40. data/config/locales/ru.yml +67 -0
  41. data/config/routes.rb +17 -13
  42. data/db/migrate/20130101010101_change_user.rb +18 -0
  43. data/db/migrate/20130101010102_create_comments.rb +50 -0
  44. data/db/migrate/20130101010103_change_commentable.rb +13 -0
  45. data/docs/admin_ui_installation.md +145 -0
  46. data/docs/advanced_installation.md +182 -0
  47. data/docs/comment_api.md +58 -0
  48. data/docs/commentable_api.md +59 -0
  49. data/docs/config_file.md +27 -0
  50. data/docs/content_preprocessors.md +73 -0
  51. data/docs/customazation_of_views.md +30 -0
  52. data/docs/denormalization_and_recent_comments.md +40 -0
  53. data/docs/documentation.md +28 -0
  54. data/docs/mountable_routes.md +80 -0
  55. data/docs/pagination.md +123 -0
  56. data/docs/screencast.jpg +0 -0
  57. data/docs/user_api.md +75 -0
  58. data/docs/what_is_comcoms.md +63 -0
  59. data/docs/whats_wrong_with_other_gems.md +18 -0
  60. data/docs/where_is_example_application.md +37 -0
  61. data/gem_version.rb +3 -0
  62. data/lib/generators/the_comments/USAGE +31 -20
  63. data/lib/generators/the_comments/the_comments_generator.rb +35 -18
  64. data/lib/generators/the_comments/views_generator.rb +52 -16
  65. data/lib/the_comments/config.rb +14 -1
  66. data/lib/the_comments/version.rb +1 -3
  67. data/lib/the_comments.rb +10 -0
  68. data/spec/dummy_app/.gitignore +17 -0
  69. data/spec/dummy_app/.rspec +1 -0
  70. data/spec/dummy_app/.ruby-gemset +1 -0
  71. data/spec/dummy_app/.ruby-version +1 -0
  72. data/spec/dummy_app/Gemfile +43 -0
  73. data/spec/dummy_app/README.md +50 -0
  74. data/spec/dummy_app/Rakefile +6 -0
  75. data/spec/dummy_app/app/assets/images/.keep +0 -0
  76. data/spec/dummy_app/app/assets/javascripts/admin_panel.js +5 -0
  77. data/spec/dummy_app/app/assets/javascripts/application.js +16 -0
  78. data/spec/dummy_app/app/assets/stylesheets/admin_panel.css +3 -0
  79. data/spec/dummy_app/app/assets/stylesheets/app.css.scss +4 -0
  80. data/spec/dummy_app/app/assets/stylesheets/application.css +16 -0
  81. data/spec/dummy_app/app/controllers/application_controller.rb +7 -0
  82. data/{lib/generators/the_comments/templates → spec/dummy_app/app/controllers}/comments_controller.rb +3 -1
  83. data/spec/dummy_app/app/controllers/concerns/.keep +0 -0
  84. data/spec/dummy_app/app/controllers/posts_controller.rb +13 -0
  85. data/spec/dummy_app/app/controllers/users_controller.rb +7 -0
  86. data/spec/dummy_app/app/helpers/application_helper.rb +2 -0
  87. data/spec/dummy_app/app/mailers/.keep +0 -0
  88. data/spec/dummy_app/app/models/.keep +0 -0
  89. data/spec/dummy_app/app/models/comment.rb +32 -0
  90. data/spec/dummy_app/app/models/concerns/.keep +0 -0
  91. data/spec/dummy_app/app/models/post.rb +17 -0
  92. data/spec/dummy_app/app/models/user.rb +21 -0
  93. data/spec/dummy_app/app/views/layouts/admin.html.haml +25 -0
  94. data/spec/dummy_app/app/views/layouts/application.html.haml +20 -0
  95. data/spec/dummy_app/app/views/posts/index.html.haml +22 -0
  96. data/spec/dummy_app/app/views/posts/show.html.haml +7 -0
  97. data/spec/dummy_app/bin/bundle +3 -0
  98. data/spec/dummy_app/bin/rails +4 -0
  99. data/spec/dummy_app/bin/rake +4 -0
  100. data/spec/dummy_app/config/application.rb +23 -0
  101. data/spec/dummy_app/config/boot.rb +4 -0
  102. data/spec/dummy_app/config/database.yml +11 -0
  103. data/spec/dummy_app/config/environment.rb +5 -0
  104. data/spec/dummy_app/config/environments/development.rb +29 -0
  105. data/spec/dummy_app/config/environments/production.rb +80 -0
  106. data/spec/dummy_app/config/environments/test.rb +36 -0
  107. data/spec/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
  108. data/spec/dummy_app/config/initializers/filter_parameter_logging.rb +4 -0
  109. data/spec/dummy_app/config/initializers/inflections.rb +16 -0
  110. data/spec/dummy_app/config/initializers/mime_types.rb +5 -0
  111. data/spec/dummy_app/config/initializers/secret_token.rb +12 -0
  112. data/spec/dummy_app/config/initializers/session_store.rb +3 -0
  113. data/spec/dummy_app/config/initializers/sorcery.rb +437 -0
  114. data/spec/dummy_app/config/initializers/the_comments.rb +13 -0
  115. data/spec/dummy_app/config/initializers/wrap_parameters.rb +14 -0
  116. data/spec/dummy_app/config/locales/en.yml +23 -0
  117. data/spec/dummy_app/config/routes.rb +15 -0
  118. data/spec/dummy_app/config.ru +4 -0
  119. data/spec/dummy_app/db/migrate/20130712061503_sorcery_core.rb +16 -0
  120. data/spec/dummy_app/db/migrate/20130712065951_create_posts.rb +11 -0
  121. data/spec/dummy_app/db/migrate/20131027185332_change_user.the_comments_engine.rb +19 -0
  122. data/spec/dummy_app/db/migrate/20131027185333_create_comments.the_comments_engine.rb +51 -0
  123. data/spec/dummy_app/db/migrate/20131027185334_change_commentable.the_comments_engine.rb +14 -0
  124. data/spec/dummy_app/db/schema.rb +74 -0
  125. data/spec/dummy_app/db/seeds.rb +42 -0
  126. data/spec/dummy_app/lib/assets/.keep +0 -0
  127. data/spec/dummy_app/lib/tasks/.keep +0 -0
  128. data/spec/dummy_app/lib/tasks/app_bootstrap.rake +15 -0
  129. data/spec/dummy_app/log/.keep +0 -0
  130. data/spec/dummy_app/public/404.html +58 -0
  131. data/spec/dummy_app/public/422.html +58 -0
  132. data/spec/dummy_app/public/500.html +57 -0
  133. data/spec/dummy_app/public/favicon.ico +0 -0
  134. data/spec/dummy_app/public/robots.txt +5 -0
  135. data/spec/dummy_app/spec/factories/post.rb +6 -0
  136. data/spec/dummy_app/spec/factories/user.rb +6 -0
  137. data/spec/dummy_app/spec/models/user_counters_spec.rb +339 -0
  138. data/spec/dummy_app/spec/spec_helper.rb +29 -0
  139. data/spec/dummy_app/test/controllers/.keep +0 -0
  140. data/spec/dummy_app/test/fixtures/.keep +0 -0
  141. data/spec/dummy_app/test/helpers/.keep +0 -0
  142. data/spec/dummy_app/test/integration/.keep +0 -0
  143. data/spec/dummy_app/test/mailers/.keep +0 -0
  144. data/spec/dummy_app/test/models/.keep +0 -0
  145. data/spec/dummy_app/test/test_helper.rb +15 -0
  146. data/spec/dummy_app/vendor/assets/javascripts/.keep +0 -0
  147. data/spec/dummy_app/vendor/assets/stylesheets/.keep +0 -0
  148. data/views_converter.rb +16 -0
  149. metadata +223 -45
  150. data/app/controllers/concerns/the_comments_controller.rb +0 -229
  151. data/app/controllers/concerns/the_comments_ip_controller.rb +0 -17
  152. data/app/controllers/concerns/the_comments_user_agent_controller.rb +0 -15
  153. data/app/models/concerns/the_comments_base.rb +0 -69
  154. data/app/models/concerns/the_comments_black_ip.rb +0 -9
  155. data/app/models/concerns/the_comments_black_user_agent.rb +0 -9
  156. data/app/models/concerns/the_comments_commentable.rb +0 -66
  157. data/app/models/concerns/the_comments_states.rb +0 -65
  158. data/app/models/concerns/the_comments_user.rb +0 -32
  159. data/app/views/ip_black_lists/index.html.haml +0 -17
  160. data/app/views/the_comments/_comment_body.html.haml +0 -30
  161. data/app/views/the_comments/_manage_controls.html.haml +0 -4
  162. data/app/views/the_comments/_tree.html.haml +0 -4
  163. data/app/views/the_comments/index.html.haml +0 -19
  164. data/app/views/the_comments/manage.html.haml +0 -29
  165. data/app/views/user_agent_black_lists/index.html.haml +0 -17
  166. data/db/migrate/20130101010101_create_comments.rb +0 -90
  167. data/lib/generators/the_comments/templates/ip_black_list.rb +0 -3
  168. data/lib/generators/the_comments/templates/ip_black_lists_controller.rb +0 -10
  169. data/lib/generators/the_comments/templates/the_comments_black_ip.rb +0 -9
  170. data/lib/generators/the_comments/templates/the_comments_black_user_agent.rb +0 -9
  171. data/lib/generators/the_comments/templates/user_agent_black_list.rb +0 -3
  172. 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
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ gemfile: spec/dummy_app/Gemfile
5
+ script: "cd spec/dummy_app && rake db:bootstrap RAILS_ENV=test && rspec --format documentation"
data/README.md CHANGED
@@ -1,522 +1,202 @@
1
- # TheComments 1.0.0
1
+ ### This Repo under development!
2
2
 
3
- TheComments - just comment system for my Ruby on Rails 4 projects. [(rubygems)](http://rubygems.org/gems/the_comments)
3
+ # TheComments
4
4
 
5
- ## Keywords
5
+ [![Gem Version](https://badge.fury.io/rb/the_comments.png)](http://badge.fury.io/rb/the_comments) | [![Build Status](https://travis-ci.org/the-teacher/the_comments.png?branch=master)](https://travis-ci.org/the-teacher/the_comments) | [![Code Climate](https://codeclimate.com/github/the-teacher/the_comments.png)](https://codeclimate.com/github/the-teacher/the_comments) | [(rubygems)](http://rubygems.org/gems/the_comments)
6
6
 
7
- Comments for Rails 4, Comments with threading, Nested Comments, Polymorphic comments, Acts as commentable, Comment functionality, Comments, Threading, Rails 4, Comments with moderation, I hate captcha!
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
- * Tree rendering via [TheSortableTree](https://github.com/the-teacher/the_sortable_tree)
34
- * [Denormalization](#denormalization) for Recent comments
35
- * Usefull cache counters
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
- ### Intro (understanding)
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: &nbsp; [Documentation](docs/documentation.md)
54
21
 
55
- ## Installation
22
+ ## How to start dummy app (screencast)
56
23
 
57
- This gem has many steps to install. Be careful when installing and keep calm.
24
+ [![Foo](https://raw.github.com/the-teacher/the_comments/master/docs/screencast.jpg)](http://vk.com/video_ext.php?oid=49225742&id=166578209&hash=10be1dba625149bb&hd=3)
58
25
 
59
- Just follow an installation instruction step by step and everything will be fine!
26
+ ## Quick Start Installation
60
27
 
61
- **Installation process consist of 4 main steps:**
28
+ ### 1. Gems install
62
29
 
63
- * [Gem Installation](#gem-installation)
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
- # I use *awesome_nested_set* gem to provide threading for comments
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
- **3)** Copy migration file into application, <b>OPEN FILE AND FOLLOW AN INSTRUCTION</b>.
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
- **4)** run migration
39
+ **Bundle**
96
40
 
97
- ```ruby
98
- bundle exec rake db:migrate
41
+ ```
42
+ bundle
99
43
  ```
100
44
 
101
- ## Code Installation
102
-
103
- **1)** Assets
45
+ ### 2. Migrations install
104
46
 
105
- *app/assets/javascripts/application.js*
47
+ **Copy migrations**
106
48
 
107
- ```js
108
- //= require the_comments
109
- //= require the_comments_manage
49
+ ```
50
+ rake the_comments_engine:install:migrations
110
51
  ```
111
52
 
112
- *app/assets/stylesheets/application.css*
53
+ Will create:
113
54
 
114
- ```css
115
- *= require the_comments
116
- ```
55
+ * xxxxx_change_user.rb
56
+ * xxxxx_create_comments.rb
57
+ * xxxxx_change_commentable.rb
117
58
 
118
- **2)** Change your ApplicationController
59
+ :warning: &nbsp; **Open and change xxxxx_change_commentable.rb migration**
119
60
 
120
61
  ```ruby
121
- class ApplicationController < ActionController::Base
122
- include TheCommentsController::ViewToken
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
- **3)** Run generator, <b>OPEN EACH OF CREATED FILES AND FOLLOW INSTRUCTIONS</b>.
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
- **4)** Copy view files into your application
85
+ ### 3. Code install
146
86
 
147
87
  ```ruby
148
- bundle exec rails g the_comments:views views
88
+ rails g the_comments install
149
89
  ```
150
90
 
151
- *List of created directories with view files:*
91
+ Will create:
152
92
 
153
- ```ruby
154
- app/views/the_comments/*.haml
155
- app/views/ip_black_lists/*.haml
156
- app/views/user_agent_black_lists/*.haml
157
- ```
93
+ * config/initializers/the_comments.rb
94
+ * app/controllers/comments_controller.rb
95
+ * app/models/comment.rb
96
+
97
+ :warning: &nbsp; **Open each file and follow an instructions**
158
98
 
159
- ## Tuning
99
+ ### 4. Models modifictions
160
100
 
161
- ### User Model
101
+ **app/models/user.rb**
162
102
 
163
103
  ```ruby
164
104
  class User < ActiveRecord::Base
165
- include TheCommentsUser
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
- # Comments moderator checking (simple example)
173
- # Usually comment's holder should be moderator
174
- def comment_moderator? comment
175
- admin? || id == comment.holder_id
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
- ### Any Commentable Model (Page, Blog, Article, User(!) ...)
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 Blog < ActiveRecord::Base
186
- include TheCommentsCommentable
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
- # by default: try(:title) || TheComments.config.default_title
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
- # by default: ['', self.class.to_s.tableize, self.to_param].join('/')
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
- # by default: try(:state)
202
- # for example: "draft"
203
- self.ban_flag == true ? :banned : :published
142
+ "published"
204
143
  end
205
144
  end
206
145
  ```
207
146
 
208
- ### Comment Model
147
+ ### 5. Mount Engine routes
209
148
 
149
+ **config/routes.rb**
210
150
 
211
151
  ```ruby
212
- class Comment < ActiveRecord::Base
213
- include TheCommentsBase
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
- # Define your filters for content
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
- ### Commentable controller
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
- ### View
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
- ![TheComments](https://raw.github.com/the-teacher/the_comments/master/docs/the_comments.jpg)
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
- * **Comment#commentable_title** - for example: "My first post about Ruby On Rails"
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
- That is why any **Commentable Model should have few methods** to provide denormalization for Comments.
166
+ **app/assets/stylesheets/application.css**
364
167
 
365
- ## Recent comments building
366
-
367
- Denormalization makes building of Recent comments (for polymorphic relationship) very easy!
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
- ### AntiSpam system
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
- _Usually spambots works faster, than human. We can try to use this feature of behavior_
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
- * **the_sortable_tree** - render helper for nested set
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
- Generators list:
182
+ **app/controllers/posts_controllers.rb**
429
183
 
430
184
  ```ruby
431
- bundle exec rails g the_comments --help
432
- ```
433
-
434
- Copy View files for customization:
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
- Copy Helper file for tree customization:
191
+ ### 8. View code example
442
192
 
443
- For more info read [TheSortableTree doc](https://github.com/the-teacher/the_sortable_tree)
193
+ **app/views/posts/show.html.haml**
444
194
 
445
- ```ruby
446
- bundle exec rails g the_comments:views helper
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
- 1. Fork it
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.