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
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20131027185334) do
15
+
16
+ create_table "comments", force: true do |t|
17
+ t.integer "user_id"
18
+ t.integer "holder_id"
19
+ t.integer "commentable_id"
20
+ t.string "commentable_type"
21
+ t.string "commentable_url"
22
+ t.string "commentable_title"
23
+ t.string "commentable_state"
24
+ t.string "anchor"
25
+ t.string "title"
26
+ t.string "contacts"
27
+ t.text "raw_content"
28
+ t.text "content"
29
+ t.string "view_token"
30
+ t.string "state", default: "draft"
31
+ t.string "ip", default: "undefined"
32
+ t.string "referer", default: "undefined"
33
+ t.string "user_agent", default: "undefined"
34
+ t.integer "tolerance_time"
35
+ t.boolean "spam", default: false
36
+ t.integer "parent_id"
37
+ t.integer "lft"
38
+ t.integer "rgt"
39
+ t.integer "depth", default: 0
40
+ t.datetime "created_at"
41
+ t.datetime "updated_at"
42
+ end
43
+
44
+ create_table "posts", force: true do |t|
45
+ t.integer "user_id"
46
+ t.string "title"
47
+ t.text "content"
48
+ t.datetime "created_at"
49
+ t.datetime "updated_at"
50
+ t.integer "draft_comments_count", default: 0
51
+ t.integer "published_comments_count", default: 0
52
+ t.integer "deleted_comments_count", default: 0
53
+ end
54
+
55
+ create_table "users", force: true do |t|
56
+ t.string "username", null: false
57
+ t.string "email"
58
+ t.string "crypted_password"
59
+ t.string "salt"
60
+ t.datetime "created_at"
61
+ t.datetime "updated_at"
62
+ t.integer "my_draft_comments_count", default: 0
63
+ t.integer "my_published_comments_count", default: 0
64
+ t.integer "my_comments_count", default: 0
65
+ t.integer "draft_comcoms_count", default: 0
66
+ t.integer "published_comcoms_count", default: 0
67
+ t.integer "deleted_comcoms_count", default: 0
68
+ t.integer "spam_comcoms_count", default: 0
69
+ t.integer "draft_comments_count", default: 0
70
+ t.integer "published_comments_count", default: 0
71
+ t.integer "deleted_comments_count", default: 0
72
+ end
73
+
74
+ end
@@ -0,0 +1,42 @@
1
+ puts 'Users'
2
+
3
+ 3.times do
4
+ User.create!(username: Faker::Name.name)
5
+ print '.'
6
+ end
7
+
8
+ puts
9
+ puts 'Posts'
10
+
11
+ User.all.each do |user|
12
+ 3.times do
13
+ user.posts.create!(
14
+ title: Faker::Lorem.sentence,
15
+ content: Faker::Lorem.paragraphs(3).join
16
+ )
17
+ print '.'
18
+ end
19
+ end
20
+
21
+ puts
22
+ puts 'Comments'
23
+
24
+ users = User.all
25
+ posts = Post.all
26
+
27
+ 20.times do
28
+ user = users.sample
29
+ post = posts.sample
30
+
31
+ Comment.create!(
32
+ user: user,
33
+ commentable: post,
34
+ title: Faker::Lorem.sentence,
35
+ raw_content: Faker::Lorem.paragraphs(3).join,
36
+ state: :published
37
+ )
38
+
39
+ print '.'
40
+ end
41
+
42
+ puts
File without changes
File without changes
@@ -0,0 +1,15 @@
1
+ namespace :db do
2
+ desc "DB: drop, create, migrate"
3
+ task bootstrap: :environment do
4
+ Rake::Task["db:drop"].invoke
5
+ Rake::Task["db:create"].invoke
6
+ Rake::Task["db:migrate"].invoke
7
+ end
8
+
9
+ # rake db:bootstrap_and_seed
10
+ desc "Reset DB and seed"
11
+ task bootstrap_and_seed: :environment do
12
+ Rake::Task["db:bootstrap"].invoke
13
+ Rake::Task["db:seed"].invoke
14
+ end
15
+ end
File without changes
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/404.html -->
52
+ <div class="dialog">
53
+ <h1>The page you were looking for doesn't exist.</h1>
54
+ <p>You may have mistyped the address or the page may have moved.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/422.html -->
52
+ <div class="dialog">
53
+ <h1>The change you wanted was rejected.</h1>
54
+ <p>Maybe you tried to change something you didn't have access to.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/500.html -->
52
+ <div class="dialog">
53
+ <h1>We're sorry, but something went wrong.</h1>
54
+ </div>
55
+ <p>If you are the application owner check the logs for more information.</p>
56
+ </body>
57
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-agent: *
5
+ # Disallow: /
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :post, class: Post do
3
+ sequence(:title) { Faker::Lorem.sentence }
4
+ sequence(:content) { Faker::Lorem.sentence }
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :user, class: User do
3
+ sequence(:username) { Faker::Name.name }
4
+ sequence(:email) { Faker::Internet.email }
5
+ end
6
+ end
@@ -0,0 +1,339 @@
1
+ require 'spec_helper'
2
+
3
+ def destroy_all
4
+ User.destroy_all
5
+ 3.times{ begin; Comment.destroy_all; rescue; end; }
6
+ end
7
+
8
+ # --------------------------------------
9
+ # Helpers
10
+ # --------------------------------------
11
+ def my_comments_count_assert user, count
12
+ user.my_comments.active.count.should eq count
13
+ user.my_comments_count.should eq count
14
+ end
15
+
16
+ def comments_count_assert(obj, values)
17
+ obj.comments.with_state(:draft).count.should eq values[0]
18
+ obj.comments.with_state(:published).count.should eq values[1]
19
+ obj.comments.with_state(:deleted).count.should eq values[2]
20
+ end
21
+
22
+ def comments_counters_assert(obj, values)
23
+ obj.draft_comments_count.should eq values[0]
24
+ obj.published_comments_count.should eq values[1]
25
+ obj.deleted_comments_count.should eq values[2]
26
+ end
27
+
28
+ def comcoms_count_assert(obj, values)
29
+ obj.comcoms.with_state(:draft).count.should eq values[0]
30
+ obj.comcoms.with_state(:published).count.should eq values[1]
31
+ obj.comcoms.with_state(:deleted).count.should eq values[2]
32
+ end
33
+
34
+ def comcoms_counters_assert(obj, values)
35
+ obj.draft_comcoms_count.should eq values[0]
36
+ obj.published_comcoms_count.should eq values[1]
37
+ obj.deleted_comcoms_count.should eq values[2]
38
+ end
39
+
40
+ # --------------------------------------
41
+ # init functions
42
+ # --------------------------------------
43
+ def create_users_and_post
44
+ @user = FactoryGirl.create(:user)
45
+ @post_holder = FactoryGirl.create(:user)
46
+ @post = FactoryGirl.create(:post, user: @post_holder)
47
+ end
48
+
49
+ def base_test_situation
50
+ create_users_and_post
51
+
52
+ 3.times do
53
+ @comment = Comment.create!(
54
+ user: @user,
55
+ commentable: @post,
56
+ title: Faker::Lorem.sentence,
57
+ raw_content: Faker::Lorem.paragraphs(3).join
58
+ )
59
+ end
60
+
61
+ @user.reload
62
+ @post_holder.reload
63
+ end
64
+
65
+ def tree_test_situation
66
+ create_users_and_post
67
+
68
+ # LEVEL 1
69
+ 3.times do
70
+ Comment.create!(
71
+ user: @user,
72
+ commentable: @post,
73
+ title: Faker::Lorem.sentence,
74
+ raw_content: Faker::Lorem.paragraphs(3).join,
75
+ state: :published
76
+ )
77
+ end
78
+ # LEVEL 2
79
+ parent_comment = Comment.first
80
+ 3.times do
81
+ Comment.create!(
82
+ user: @user,
83
+ commentable: @post,
84
+ parent_id: parent_comment.id,
85
+ title: Faker::Lorem.sentence,
86
+ raw_content: Faker::Lorem.paragraphs(3).join,
87
+ state: :published
88
+ )
89
+ end
90
+ # LEVEL 3
91
+ parent_comment = Comment.first.children.first
92
+ 3.times do
93
+ Comment.create!(
94
+ user: @user,
95
+ commentable: @post,
96
+ parent_id: parent_comment.id,
97
+ title: Faker::Lorem.sentence,
98
+ raw_content: Faker::Lorem.paragraphs(3).join,
99
+ state: :published
100
+ )
101
+ end
102
+
103
+ @user.reload
104
+ @post_holder.reload
105
+ end
106
+ # --------------------------------------
107
+ # ~ init functions
108
+ # --------------------------------------
109
+
110
+ describe User do
111
+ context 'User leave comment to the post' do
112
+ after(:all){ destroy_all }
113
+
114
+ before(:all){ create_users_and_post }
115
+
116
+ it 'should be User' do
117
+ @user.should be_instance_of User
118
+ @post_holder.should be_instance_of User
119
+ end
120
+
121
+ it 'Post should has owner' do
122
+ @post.user.should eq @post_holder
123
+ end
124
+
125
+ it 'should be Post' do
126
+ @post.should be_instance_of Post
127
+ end
128
+
129
+ it 'Create new comment' do
130
+ @comment = Comment.create!(
131
+ user: @user,
132
+ commentable: @post,
133
+ title: Faker::Lorem.sentence,
134
+ raw_content: Faker::Lorem.paragraphs(3).join
135
+ )
136
+
137
+ @comment.user.should eq @user
138
+ @comment.holder.should eq @post_holder
139
+ end
140
+ end
141
+
142
+ context "Written by me counters" do
143
+ after(:all) { destroy_all }
144
+ before(:all){ create_users_and_post }
145
+ it 'should has correct My counters values' do
146
+ @comment = Comment.create!(
147
+ user: @user,
148
+ commentable: @post,
149
+ title: Faker::Lorem.sentence,
150
+ raw_content: Faker::Lorem.paragraphs(3).join
151
+ )
152
+ @user.reload
153
+ @user.my_comments_count.should eq 1
154
+ @user.my_draft_comments_count.should eq 1
155
+ @user.my_published_comments_count.should eq 0
156
+ end
157
+ end
158
+
159
+ context "Commentable Denormalization" do
160
+ after(:all) { destroy_all }
161
+ before(:all) do
162
+ base_test_situation
163
+ end
164
+
165
+ it 'should have denormalized fields' do
166
+ title = "New Title!"
167
+ @post.update_attribute(:title, title)
168
+ @post.title.should eq title
169
+ @comment = @post.comments.first
170
+ @comment.commentable_title.should eq title
171
+ end
172
+ end
173
+
174
+ context 'User leave 3 comments and Instances has expectable counter values' do
175
+ after(:all) { destroy_all }
176
+
177
+ before(:all){ base_test_situation }
178
+
179
+ describe 'User counters' do
180
+ it 'my_comments counter' do
181
+ my_comments_count_assert(@user, 3)
182
+ end
183
+
184
+ it 'Comcoms counters' do
185
+ @user.comcoms.count.should eq 0
186
+
187
+ comcoms_counters_assert(@user, [0,0,0])
188
+ end
189
+ end
190
+
191
+ describe 'Holder counters' do
192
+ it 'my_comments counter' do
193
+ my_comments_count_assert(@post_holder, 0)
194
+ end
195
+
196
+ it 'Comcoms counters' do
197
+ @post_holder.comcoms.count.should eq 3
198
+ comcoms_counters_assert(@post_holder, [3,0,0])
199
+ end
200
+ end
201
+ end
202
+
203
+ context 'User leave 3 comments, 1 Comment DRAFT => PUBLISHED' do
204
+ after(:all) { destroy_all }
205
+
206
+ before(:all) do
207
+ base_test_situation
208
+ @comment = Comment.first
209
+ @comment.to_published
210
+
211
+ @user.reload
212
+ @post.reload
213
+ @comment.reload
214
+ @post_holder.reload
215
+ end
216
+
217
+ describe 'User counters' do
218
+ it 'has expectable values' do
219
+ comments_counters_assert @user, [0,0,0]
220
+ comments_count_assert @user, [0,0,0]
221
+ comcoms_counters_assert @user, [0,0,0]
222
+ comcoms_count_assert @user, [0,0,0]
223
+ my_comments_count_assert @user, 3
224
+ end
225
+ end
226
+
227
+ describe 'Holder counters' do
228
+ it 'has expectable values' do
229
+ @post_holder.comcoms.count.should eq 3
230
+
231
+ my_comments_count_assert(@post_holder, 0)
232
+ comcoms_count_assert @post_holder, [2,1,0]
233
+ comcoms_counters_assert @post_holder, [2,1,0]
234
+ end
235
+ end
236
+ end
237
+
238
+ context '3 comments, 1 Comment DRAFT => DELETE' do
239
+ after(:all) { destroy_all }
240
+
241
+ before(:all) do
242
+ base_test_situation
243
+ @comment = Comment.first
244
+ @comment.to_published
245
+ @comment.to_deleted
246
+
247
+ @user.reload
248
+ @post.reload
249
+ @comment.reload
250
+ @post_holder.reload
251
+ end
252
+
253
+ it 'has correct counters values' do
254
+ comments_counters_assert @user, [0,0,0]
255
+ comments_count_assert @user, [0,0,0]
256
+ comcoms_count_assert @user, [0,0,0]
257
+ my_comments_count_assert @user, 2
258
+ @user.my_comments.count.should eq 3
259
+
260
+ comments_counters_assert @post_holder, [0,0,0]
261
+ comments_count_assert @post_holder, [0,0,0]
262
+ comcoms_count_assert @post_holder, [2,0,1]
263
+ my_comments_count_assert @post_holder, 0
264
+ @post_holder.my_comments.count.should eq 0
265
+
266
+ comments_count_assert @post, [2,0,1]
267
+ comments_counters_assert @post, [2,0,1]
268
+ end
269
+ end
270
+
271
+ context 'Comments tree, 1 branch to deleted' do
272
+ after(:each) { destroy_all }
273
+ before(:each){ tree_test_situation }
274
+
275
+ it 'has correct counters values before deleting' do
276
+ Comment.count.should eq 9
277
+
278
+ my_comments_count_assert @user, 9
279
+ comments_count_assert @user, [0,0,0]
280
+
281
+ my_comments_count_assert @post_holder, 0
282
+ comments_count_assert @post_holder, [0,0,0]
283
+
284
+ comcoms_count_assert @post_holder, [0,9,0]
285
+ comcoms_counters_assert @post_holder, [0,9,0]
286
+
287
+ comments_count_assert @post, [0,9,0]
288
+ comments_counters_assert @post, [0,9,0]
289
+ end
290
+
291
+ it 'has correct counters values after branch deleting' do
292
+ @comment = Comment.first.children.first
293
+ @comment.to_deleted
294
+
295
+ @user.reload
296
+ @post.reload
297
+ @post_holder.reload
298
+
299
+ Comment.with_state(:published).count.should eq 5
300
+ Comment.with_state(:deleted).count.should eq 4
301
+
302
+ my_comments_count_assert @user, 5
303
+ comments_count_assert @user, [0,0,0]
304
+
305
+ my_comments_count_assert @post_holder, 0
306
+ comments_count_assert @post_holder, [0,0,0]
307
+
308
+ comcoms_count_assert @post_holder, [0,5,4]
309
+ comcoms_counters_assert @post_holder, [0,5,4]
310
+
311
+ comments_count_assert @post, [0,5,4]
312
+ comments_counters_assert @post, [0,5,4]
313
+ end
314
+
315
+ # This code helps to me to catch logic bugs
316
+ # there is no any asserts
317
+ # context "callbacks catch" do
318
+ # after(:all) { destroy_all }
319
+ # it "before validation callback with increment! must be call manually" do
320
+ # @user = FactoryGirl.create(:user)
321
+ # @post_holder = FactoryGirl.create(:user)
322
+ # @post = FactoryGirl.create(:post, user: @post_holder)
323
+
324
+ # Comment.create!(
325
+ # user: @user,
326
+ # commentable: @post,
327
+ # title: Faker::Lorem.sentence,
328
+ # raw_content: Faker::Lorem.paragraphs(3).join,
329
+ # state: :deleted
330
+ # )
331
+ # Comment.first.to_published
332
+ # end
333
+ # end
334
+
335
+ # it 'has correct counters after comment destroy' do
336
+ # pending("has correct counters after comment destroy")
337
+ # end
338
+ end
339
+ end