diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 00000000..3ffb8d89 --- /dev/null +++ b/app/models/post.rb @@ -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 diff --git a/db/migrate/20190824141258_create_posts.rb b/db/migrate/20190824141258_create_posts.rb new file mode 100644 index 00000000..cc63263b --- /dev/null +++ b/db/migrate/20190824141258_create_posts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index e415bd18..47b6b8fd 100644 --- a/db/schema.rb +++ b/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_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 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 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| t.string "email", 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 end + add_foreign_key "posts", "boards" + add_foreign_key "posts", "post_statuses" + add_foreign_key "posts", "users" end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb new file mode 100644 index 00000000..89b39e4b --- /dev/null +++ b/spec/factories/posts.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :post do + title { "Post Title" } + description { "Post Description" } + board + user + post_status + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 00000000..c431e117 --- /dev/null +++ b/spec/models/post_spec.rb @@ -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