mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 03:07:52 +01:00
Add comment model and its tests
This commit is contained in:
8
app/models/comment.rb
Normal file
8
app/models/comment.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Comment < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :post
|
||||
belongs_to :parent, class_name: 'Comment', optional: true
|
||||
has_many :children, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
|
||||
|
||||
validates :body, presence: true, length: { minimum: 4 }
|
||||
end
|
||||
12
db/migrate/20190916131938_create_comments.rb
Normal file
12
db/migrate/20190916131938_create_comments.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class CreateComments < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :comments do |t|
|
||||
t.text :body
|
||||
t.references :user, null: false, foreign_key: true
|
||||
t.references :post, null: false, foreign_key: true
|
||||
t.references :parent, foreign_key: { to_table: :comments }
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
17
db/schema.rb
17
db/schema.rb
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2019_08_26_154118) do
|
||||
ActiveRecord::Schema.define(version: 2019_09_16_131938) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -23,6 +23,18 @@ ActiveRecord::Schema.define(version: 2019_08_26_154118) do
|
||||
t.index ["name"], name: "index_boards_on_name", unique: true
|
||||
end
|
||||
|
||||
create_table "comments", force: :cascade do |t|
|
||||
t.text "body"
|
||||
t.bigint "user_id", null: false
|
||||
t.bigint "post_id", null: false
|
||||
t.bigint "parent_id"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["parent_id"], name: "index_comments_on_parent_id"
|
||||
t.index ["post_id"], name: "index_comments_on_post_id"
|
||||
t.index ["user_id"], name: "index_comments_on_user_id"
|
||||
end
|
||||
|
||||
create_table "post_statuses", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "color", null: false
|
||||
@@ -65,6 +77,9 @@ ActiveRecord::Schema.define(version: 2019_08_26_154118) do
|
||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||
end
|
||||
|
||||
add_foreign_key "comments", "comments", column: "parent_id"
|
||||
add_foreign_key "comments", "posts"
|
||||
add_foreign_key "comments", "users"
|
||||
add_foreign_key "posts", "boards"
|
||||
add_foreign_key "posts", "post_statuses"
|
||||
add_foreign_key "posts", "users"
|
||||
|
||||
8
spec/factories/comments.rb
Normal file
8
spec/factories/comments.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :comment do
|
||||
body { "MyText" }
|
||||
user
|
||||
post
|
||||
parent { nil }
|
||||
end
|
||||
end
|
||||
50
spec/models/comment_spec.rb
Normal file
50
spec/models/comment_spec.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Comment, type: :model do
|
||||
let(:comment) { FactoryBot.build(:comment) }
|
||||
|
||||
it 'should be valid' do
|
||||
expect(comment).to be_valid
|
||||
end
|
||||
|
||||
it 'has a body' do
|
||||
nil_body = FactoryBot.build(:comment, body: nil)
|
||||
empty_body = FactoryBot.build(:comment, body: '')
|
||||
|
||||
expect(nil_body).to be_invalid
|
||||
expect(empty_body).to be_invalid
|
||||
end
|
||||
|
||||
it 'has a body with 4 or more characters' do
|
||||
short_body = FactoryBot.build(:comment, body: 'a' * 3)
|
||||
|
||||
expect(short_body).to be_invalid
|
||||
end
|
||||
|
||||
it 'can have no parent' do
|
||||
no_parent = FactoryBot.build(:comment, parent: nil)
|
||||
|
||||
expect(no_parent).to be_valid
|
||||
expect(no_parent.parent).to be_nil
|
||||
end
|
||||
|
||||
it 'can have a parent' do
|
||||
parent = FactoryBot.build(:comment)
|
||||
child = FactoryBot.build(:comment, parent: parent)
|
||||
|
||||
expect(child).to be_valid
|
||||
expect(child.parent).to eq(parent)
|
||||
end
|
||||
|
||||
it 'can have no children' do
|
||||
expect(comment.children).to be_empty
|
||||
end
|
||||
|
||||
it 'can have 1+ children' do
|
||||
parent = FactoryBot.create(:comment)
|
||||
child1 = FactoryBot.create(:comment, parent: parent)
|
||||
child2 = FactoryBot.create(:comment, parent: parent)
|
||||
|
||||
expect(parent.children.length).to eq(2)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user