mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 03:07:52 +01:00
Add Post model
This commit is contained in:
7
app/models/post.rb
Normal file
7
app/models/post.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class Post < ApplicationRecord
|
||||||
|
belongs_to :board
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :post_status, optional: true
|
||||||
|
|
||||||
|
validates :title, presence: true, length: { in: 4..64 }
|
||||||
|
end
|
||||||
13
db/migrate/20190824141258_create_posts.rb
Normal file
13
db/migrate/20190824141258_create_posts.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
class CreatePosts < ActiveRecord::Migration[6.0]
|
||||||
|
def change
|
||||||
|
create_table :posts do |t|
|
||||||
|
t.string :title, null: false
|
||||||
|
t.text :description
|
||||||
|
t.references :board, null: false, foreign_key: true
|
||||||
|
t.references :user, null: false, foreign_key: true
|
||||||
|
t.references :post_status, foreign_key: true
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
18
db/schema.rb
18
db/schema.rb
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2019_08_24_093755) do
|
ActiveRecord::Schema.define(version: 2019_08_24_141258) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -31,6 +31,19 @@ ActiveRecord::Schema.define(version: 2019_08_24_093755) do
|
|||||||
t.index ["name"], name: "index_post_statuses_on_name", unique: true
|
t.index ["name"], name: "index_post_statuses_on_name", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "posts", force: :cascade do |t|
|
||||||
|
t.string "title", null: false
|
||||||
|
t.text "description"
|
||||||
|
t.bigint "board_id", null: false
|
||||||
|
t.bigint "user_id", null: false
|
||||||
|
t.bigint "post_status_id"
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.index ["board_id"], name: "index_posts_on_board_id"
|
||||||
|
t.index ["post_status_id"], name: "index_posts_on_post_status_id"
|
||||||
|
t.index ["user_id"], name: "index_posts_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "users", force: :cascade do |t|
|
create_table "users", force: :cascade do |t|
|
||||||
t.string "email", default: "", null: false
|
t.string "email", default: "", null: false
|
||||||
t.string "encrypted_password", default: "", null: false
|
t.string "encrypted_password", default: "", null: false
|
||||||
@@ -50,4 +63,7 @@ ActiveRecord::Schema.define(version: 2019_08_24_093755) do
|
|||||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "posts", "boards"
|
||||||
|
add_foreign_key "posts", "post_statuses"
|
||||||
|
add_foreign_key "posts", "users"
|
||||||
end
|
end
|
||||||
|
|||||||
9
spec/factories/posts.rb
Normal file
9
spec/factories/posts.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :post do
|
||||||
|
title { "Post Title" }
|
||||||
|
description { "Post Description" }
|
||||||
|
board
|
||||||
|
user
|
||||||
|
post_status
|
||||||
|
end
|
||||||
|
end
|
||||||
55
spec/models/post_spec.rb
Normal file
55
spec/models/post_spec.rb
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Post, type: :model do
|
||||||
|
let(:post) { FactoryBot.build(:post) }
|
||||||
|
|
||||||
|
it 'should be valid' do
|
||||||
|
expect(post).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a non-null and non-empty title' do
|
||||||
|
nil_post = FactoryBot.build(:post, title: nil)
|
||||||
|
empty_post = FactoryBot.build(:post, title: "")
|
||||||
|
|
||||||
|
expect(nil_post).to be_invalid
|
||||||
|
expect(empty_post).to be_invalid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a title between 4 and 64 characters' do
|
||||||
|
too_short_post = FactoryBot.build(:post, title: "a" * 3)
|
||||||
|
short_post = FactoryBot.build(:post, title: "a" * 4)
|
||||||
|
long_post = FactoryBot.build(:post, title: "a" * 64)
|
||||||
|
too_long_post = FactoryBot.build(:post, title: "a" * 65)
|
||||||
|
|
||||||
|
expect(too_short_post).to be_invalid
|
||||||
|
expect(short_post).to be_valid
|
||||||
|
expect(long_post).to be_valid
|
||||||
|
expect(too_long_post).to be_invalid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a description that can be nil or empty' do
|
||||||
|
nil_description_post = FactoryBot.build(:post, description: nil)
|
||||||
|
empty_description_post = FactoryBot.build(:post, description: "")
|
||||||
|
|
||||||
|
expect(nil_description_post).to be_valid
|
||||||
|
expect(empty_description_post).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a reference to a post status that can be nil' do
|
||||||
|
no_status_post = FactoryBot.build(:post, post_status_id: nil)
|
||||||
|
|
||||||
|
expect(no_status_post).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a reference to a user than cannot be nil' do
|
||||||
|
no_user_post = FactoryBot.build(:post, user_id: nil)
|
||||||
|
|
||||||
|
expect(no_user_post).to be_invalid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'has a reference to a board than cannot be nil' do
|
||||||
|
no_board_post = FactoryBot.build(:post, board_id: nil)
|
||||||
|
|
||||||
|
expect(no_board_post).to be_invalid
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user