acts_as_commentable_more 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +48 -0
- data/.travis.yml +23 -0
- data/CHANGELOG.rdoc +5 -0
- data/Gemfile +40 -0
- data/Gemfile.lock +212 -0
- data/LICENSE +22 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +16 -0
- data/Rakefile +32 -0
- data/acts_as_commentable_more.gemspec +36 -0
- data/lib/acts_as_commentable_more/version.rb +3 -0
- data/lib/acts_as_commentable_more.rb +4 -0
- data/lib/comment_methods.rb +19 -0
- data/lib/commentable_methods.rb +68 -0
- data/lib/generators/commentable/USAGE +11 -0
- data/lib/generators/commentable/commentable_generator.rb +18 -0
- data/lib/generators/commentable/templates/comment.rb +17 -0
- data/lib/generators/commentable/templates/create_comments.rb +18 -0
- data/lib/tasks/acts_as_commentable_more_tasks.rake +4 -0
- data/test/acts_as_commentable_more_test.rb +7 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +17 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/admin.rb +2 -0
- data/test/dummy/app/models/comment.rb +17 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/models/custom_comment.rb +17 -0
- data/test/dummy/app/models/letter.rb +3 -0
- data/test/dummy/app/models/note.rb +3 -0
- data/test/dummy/app/models/post.rb +4 -0
- data/test/dummy/app/models/topic.rb +3 -0
- data/test/dummy/app/models/user.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +35 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.travis.yml +18 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20150113045806_create_posts.rb +9 -0
- data/test/dummy/db/migrate/20150113045817_create_admins.rb +9 -0
- data/test/dummy/db/migrate/20150113045831_create_users.rb +9 -0
- data/test/dummy/db/migrate/20150113065948_create_notes.rb +9 -0
- data/test/dummy/db/migrate/20150113074249_create_topics.rb +9 -0
- data/test/dummy/db/migrate/20150114052120_create_letters.rb +9 -0
- data/test/dummy/db/migrate/20150114100411_create_comments.rb +18 -0
- data/test/dummy/db/migrate/20150114100422_create_custom_comments.rb +18 -0
- data/test/dummy/db/schema.rb +86 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/spec/acts_as_commentable_more_spec.rb +205 -0
- data/test/dummy/spec/factories/admins.rb +7 -0
- data/test/dummy/spec/factories/comments.rb +7 -0
- data/test/dummy/spec/factories/custom_comments.rb +7 -0
- data/test/dummy/spec/factories/letters.rb +7 -0
- data/test/dummy/spec/factories/notes.rb +7 -0
- data/test/dummy/spec/factories/posts.rb +7 -0
- data/test/dummy/spec/factories/topics.rb +7 -0
- data/test/dummy/spec/factories/users.rb +7 -0
- data/test/dummy/spec/models/admin_spec.rb +5 -0
- data/test/dummy/spec/models/custom_comment_spec.rb +5 -0
- data/test/dummy/spec/models/letter_spec.rb +5 -0
- data/test/dummy/spec/models/note_spec.rb +5 -0
- data/test/dummy/spec/models/post_spec.rb +5 -0
- data/test/dummy/spec/models/topic_spec.rb +5 -0
- data/test/dummy/spec/models/user_spec.rb +5 -0
- data/test/dummy/spec/rails_helper.rb +61 -0
- data/test/dummy/spec/spec_helper.rb +85 -0
- data/test/test_helper.rb +16 -0
- metadata +197 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateComments < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
enable_extension 'hstore'
|
4
|
+
|
5
|
+
create_table :comments do |t|
|
6
|
+
# t.string :title, :limit => 50, :default => ""
|
7
|
+
t.text :message
|
8
|
+
t.references :commentable, polymorphic: true
|
9
|
+
t.references :user, polymorphic: true, index: true
|
10
|
+
t.string :role, default: nil
|
11
|
+
t.hstore :related_attributes
|
12
|
+
t.timestamps
|
13
|
+
end
|
14
|
+
|
15
|
+
add_index :comments, [:commentable_type, :commentable_id],
|
16
|
+
name: :index_comments_on_commentable_type_and_commentable_id
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateCustomComments < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
enable_extension 'hstore'
|
4
|
+
|
5
|
+
create_table :custom_comments do |t|
|
6
|
+
# t.string :title, :limit => 50, :default => ""
|
7
|
+
t.text :message
|
8
|
+
t.references :custom_commentable, polymorphic: true
|
9
|
+
t.references :user, polymorphic: true, index: true
|
10
|
+
t.string :role, default: nil
|
11
|
+
t.hstore :related_attributes
|
12
|
+
t.timestamps
|
13
|
+
end
|
14
|
+
|
15
|
+
add_index :custom_comments, [:custom_commentable_type, :custom_commentable_id],
|
16
|
+
name: :index_custom_comments_on_commentable_type_and_commentable_id
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,86 @@
|
|
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: 20150114100422) do
|
15
|
+
|
16
|
+
# These are extensions that must be enabled in order to support this database
|
17
|
+
enable_extension "plpgsql"
|
18
|
+
enable_extension "hstore"
|
19
|
+
|
20
|
+
create_table "admins", force: true do |t|
|
21
|
+
t.string "name"
|
22
|
+
t.datetime "created_at"
|
23
|
+
t.datetime "updated_at"
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table "comments", force: true do |t|
|
27
|
+
t.text "message"
|
28
|
+
t.integer "commentable_id"
|
29
|
+
t.string "commentable_type"
|
30
|
+
t.integer "user_id"
|
31
|
+
t.string "user_type"
|
32
|
+
t.string "role"
|
33
|
+
t.hstore "related_attributes"
|
34
|
+
t.datetime "created_at"
|
35
|
+
t.datetime "updated_at"
|
36
|
+
end
|
37
|
+
|
38
|
+
add_index "comments", ["commentable_type", "commentable_id"], name: "index_comments_on_commentable_type_and_commentable_id", using: :btree
|
39
|
+
add_index "comments", ["user_id", "user_type"], name: "index_comments_on_user_id_and_user_type", using: :btree
|
40
|
+
|
41
|
+
create_table "custom_comments", force: true do |t|
|
42
|
+
t.text "message"
|
43
|
+
t.integer "custom_commentable_id"
|
44
|
+
t.string "custom_commentable_type"
|
45
|
+
t.integer "user_id"
|
46
|
+
t.string "user_type"
|
47
|
+
t.string "role"
|
48
|
+
t.hstore "related_attributes"
|
49
|
+
t.datetime "created_at"
|
50
|
+
t.datetime "updated_at"
|
51
|
+
end
|
52
|
+
|
53
|
+
add_index "custom_comments", ["custom_commentable_type", "custom_commentable_id"], name: "index_custom_comments_on_commentable_type_and_commentable_id", using: :btree
|
54
|
+
add_index "custom_comments", ["user_id", "user_type"], name: "index_custom_comments_on_user_id_and_user_type", using: :btree
|
55
|
+
|
56
|
+
create_table "letters", force: true do |t|
|
57
|
+
t.string "title"
|
58
|
+
t.datetime "created_at"
|
59
|
+
t.datetime "updated_at"
|
60
|
+
end
|
61
|
+
|
62
|
+
create_table "notes", force: true do |t|
|
63
|
+
t.string "title"
|
64
|
+
t.datetime "created_at"
|
65
|
+
t.datetime "updated_at"
|
66
|
+
end
|
67
|
+
|
68
|
+
create_table "posts", force: true do |t|
|
69
|
+
t.string "title"
|
70
|
+
t.datetime "created_at"
|
71
|
+
t.datetime "updated_at"
|
72
|
+
end
|
73
|
+
|
74
|
+
create_table "topics", force: true do |t|
|
75
|
+
t.string "title"
|
76
|
+
t.datetime "created_at"
|
77
|
+
t.datetime "updated_at"
|
78
|
+
end
|
79
|
+
|
80
|
+
create_table "users", force: true do |t|
|
81
|
+
t.string "name"
|
82
|
+
t.datetime "created_at"
|
83
|
+
t.datetime "updated_at"
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,67 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The page you were looking for doesn't exist (404)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/404.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>The page you were looking for doesn't exist.</h1>
|
62
|
+
<p>You may have mistyped the address or the page may have moved.</p>
|
63
|
+
</div>
|
64
|
+
<p>If you are the application owner check the logs for more information.</p>
|
65
|
+
</div>
|
66
|
+
</body>
|
67
|
+
</html>
|
@@ -0,0 +1,67 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The change you wanted was rejected (422)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/422.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>The change you wanted was rejected.</h1>
|
62
|
+
<p>Maybe you tried to change something you didn't have access to.</p>
|
63
|
+
</div>
|
64
|
+
<p>If you are the application owner check the logs for more information.</p>
|
65
|
+
</div>
|
66
|
+
</body>
|
67
|
+
</html>
|
@@ -0,0 +1,66 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>We're sorry, but something went wrong (500)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/500.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>We're sorry, but something went wrong.</h1>
|
62
|
+
</div>
|
63
|
+
<p>If you are the application owner check the logs for more information.</p>
|
64
|
+
</div>
|
65
|
+
</body>
|
66
|
+
</html>
|
File without changes
|
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe ActsAsCommentableMore do
|
4
|
+
|
5
|
+
describe "managed basic comment" do
|
6
|
+
it "add a comment" do
|
7
|
+
post = create(:post)
|
8
|
+
expect{post.comments.create(message: 'my message')}.to change(Comment, :count).by(1)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "gets all comment" do
|
12
|
+
posts = create_list(:post, 2)
|
13
|
+
posts.each do |post|
|
14
|
+
5.times { |i| post.comments.create(message: "message #{i}") }
|
15
|
+
expect(post.comments.count).to eq 5
|
16
|
+
end
|
17
|
+
expect(Comment.count).to eq 10
|
18
|
+
end
|
19
|
+
|
20
|
+
it "any role of user" do
|
21
|
+
post = create(:post)
|
22
|
+
admin = create(:admin)
|
23
|
+
user = create(:user)
|
24
|
+
user_comment = post.comments.create(message: 'my message', user: user)
|
25
|
+
admin_comment = post.comments.create(message: 'my message', user: admin)
|
26
|
+
expect(user_comment.user).to eq user
|
27
|
+
expect(admin_comment.user).to eq admin
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "class helper" do
|
31
|
+
context "self.find_comments_by_user(user, role: nil)" do
|
32
|
+
before do
|
33
|
+
post_1 = create(:post)
|
34
|
+
post_2 = create(:post)
|
35
|
+
@admin = create(:admin)
|
36
|
+
@user = create(:user)
|
37
|
+
user_comment_post_1 = post_1.comments.create(message: 'my message', user: @user)
|
38
|
+
admin_comment_post_1 = post_1.comments.create(message: 'my message', user: @admin)
|
39
|
+
user_comment_post_2 = post_2.comments.create(message: 'my message', user: @user)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "findby user and role=nil" do
|
43
|
+
Comment.find_comments_by_user(@user).each do |comment|
|
44
|
+
expect(comment.user).to eq @user
|
45
|
+
expect(comment.role).to eq 'comment'
|
46
|
+
end
|
47
|
+
Comment.find_comments_by_user(@admin).each do |comment|
|
48
|
+
expect(comment.user).to eq @admin
|
49
|
+
expect(comment.role).to eq 'comment'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "find by user and role can find" do
|
54
|
+
Comment.find_comments_by_user(@user, :comment).each do |comment|
|
55
|
+
expect(comment.user).to eq @user
|
56
|
+
expect(comment.role).to eq 'comment'
|
57
|
+
end
|
58
|
+
Comment.find_comments_by_user(@admin, :comment).each do |comment|
|
59
|
+
expect(comment.user).to eq @admin
|
60
|
+
expect(comment.role).to eq 'comment'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "find by user and role doesn't have" do
|
65
|
+
expect(Comment.find_comments_by_user(@user, :any_role).count).to eq 0
|
66
|
+
expect(Comment.find_comments_by_user(@admin, :any_role).count).to eq 0
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "managed comments with association options :class_name and :as" do
|
75
|
+
it "add a comment" do
|
76
|
+
topic = create(:topic)
|
77
|
+
expect{topic.comments.create(message: 'my message')}.to change(CustomComment, :count).by(1)
|
78
|
+
end
|
79
|
+
it "gets all comment" do
|
80
|
+
topics = create_list(:topic, 2)
|
81
|
+
topics.each do |topic|
|
82
|
+
5.times { |i| topic.comments.create(message: "message #{i}") }
|
83
|
+
expect(topic.comments.count).to eq 5
|
84
|
+
end
|
85
|
+
expect(CustomComment.count).to eq 10
|
86
|
+
end
|
87
|
+
it "any type of user" do
|
88
|
+
topic = create(:topic)
|
89
|
+
admin = create(:admin)
|
90
|
+
user = create(:user)
|
91
|
+
user_comment = topic.comments.create(message: 'my message', user: user)
|
92
|
+
admin_comment = topic.comments.create(message: 'my message', user: admin)
|
93
|
+
expect(user_comment.user).to eq user
|
94
|
+
expect(admin_comment.user).to eq admin
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "managed comment that specific type" do
|
99
|
+
describe "socpe all_comments" do
|
100
|
+
|
101
|
+
it "default association options" do
|
102
|
+
note = create(:note)
|
103
|
+
5.times { |i| note.private_comments.create(message: 'private message') }
|
104
|
+
3.times { |i| note.publish_comments.create(message: 'publish message') }
|
105
|
+
|
106
|
+
other_note = create(:note)
|
107
|
+
5.times { |i| other_note.private_comments.create(message: 'private message') }
|
108
|
+
|
109
|
+
expect(note.all_comments.count).to eq 8
|
110
|
+
end
|
111
|
+
|
112
|
+
it "custom association options" do
|
113
|
+
letter = create(:letter)
|
114
|
+
5.times { |i| letter.hide_comments.create(message: 'hide message') }
|
115
|
+
8.times { |i| letter.show_comments.create(message: 'show message') }
|
116
|
+
|
117
|
+
other_letter = create(:letter)
|
118
|
+
5.times { |i| other_letter.show_comments.create(message: 'show message') }
|
119
|
+
|
120
|
+
expect(letter.all_comments.count).to eq 13
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
it "doesn't have comments association to get all comment" do
|
126
|
+
note = create(:note)
|
127
|
+
expect{ note.comments }.to raise_error(NoMethodError)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "gets comment specific role by {role}_comments method" do
|
131
|
+
note = create(:note)
|
132
|
+
5.times { |i| note.private_comments.create(message: 'private message') }
|
133
|
+
3.times { |i| note.publish_comments.create(message: 'publish message') }
|
134
|
+
expect(note.private_comments.count).to eq 5
|
135
|
+
expect(note.publish_comments.count).to eq 3
|
136
|
+
end
|
137
|
+
|
138
|
+
it "add comment specific role" do
|
139
|
+
note = create(:note)
|
140
|
+
|
141
|
+
private_note = note.private_comments.build(message: 'private message')
|
142
|
+
expect{private_note.save}.to change(Comment, :count).by(1)
|
143
|
+
expect(private_note.role).to eq 'private'
|
144
|
+
|
145
|
+
publish_note = note.publish_comments.build(message: 'publish message')
|
146
|
+
expect{publish_note.save}.to change(Comment, :count).by(1)
|
147
|
+
expect(publish_note.role).to eq 'publish'
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "class helper" do
|
151
|
+
context "self.find_comments_by_user(user, role: nil)" do
|
152
|
+
before do
|
153
|
+
note_1 = create(:note)
|
154
|
+
note_2 = create(:note)
|
155
|
+
@admin = create(:admin)
|
156
|
+
@user = create(:user)
|
157
|
+
user_private_comment_note_1 = note_1.private_comments.create(message: 'private message user', user: @user)
|
158
|
+
user_publish_comment_note_2 = note_2.publish_comments.create(message: 'publish message user', user: @user)
|
159
|
+
admin_comment_note_1 = note_1.publish_comments.create(message: 'publish message admin', user: @admin)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "findby user and role=nil" do
|
163
|
+
Comment.find_comments_by_user(@user).each do |comment|
|
164
|
+
expect(comment.user).to eq @user
|
165
|
+
expect(comment.role).to eq('private').or eq('publish')
|
166
|
+
end
|
167
|
+
Comment.find_comments_by_user(@admin).each do |comment|
|
168
|
+
expect(comment.user).to eq @admin
|
169
|
+
expect(comment.role).to eq('private').or eq('publish')
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
it "find by user and role can find" do
|
174
|
+
Comment.find_comments_by_user(@user, :private).each do |comment|
|
175
|
+
expect(comment.user).to eq @user
|
176
|
+
expect(comment.role).to eq 'private'
|
177
|
+
end
|
178
|
+
Comment.find_comments_by_user(@admin, :publish).each do |comment|
|
179
|
+
expect(comment.user).to eq @admin
|
180
|
+
expect(comment.role).to eq 'publish'
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
it "find by user and role doesn't have" do
|
185
|
+
expect(Comment.find_comments_by_user(@user, :any_role).count).to eq 0
|
186
|
+
expect(Comment.find_comments_by_user(@admin, :any_role).count).to eq 0
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "record related attributes" do
|
194
|
+
it "can update related_attributes feild with hash array" do
|
195
|
+
post = create(:post)
|
196
|
+
comment = post.comments.create(related_attributes: {user_id: 1, location_name: 'Thailand'})
|
197
|
+
expect(comment.related_attributes['user_id']).to eq nil
|
198
|
+
expect(comment.related_attributes['location_name']).to eq nil
|
199
|
+
expect(comment.related_attributes[:user_id]).to eq '1'
|
200
|
+
expect(comment.related_attributes[:location_name]).to eq 'Thailand'
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|