Add BlogPost model

Also fleshed out Micropost tests.
This commit is contained in:
Trevor Vallender 2023-10-05 16:46:07 +01:00
parent 5e5737b132
commit 2014730610
8 changed files with 116 additions and 5 deletions

19
app/models/blog_post.rb Normal file
View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class BlogPost < ApplicationRecord
belongs_to :user
has_rich_text :content
validates :title,
:user,
:published,
:slug,
:content,
presence: true
validates :slug, uniqueness: true
def to_param
slug
end
end

View File

@ -4,5 +4,7 @@ class Micropost < ApplicationRecord
belongs_to :user
has_rich_text :content
validates :user, presence: true
validates :user,
:content,
presence: true
end

View File

@ -0,0 +1,12 @@
class CreateBlogPosts < ActiveRecord::Migration[7.1]
def change
create_table :blog_posts do |t|
t.string :title, null: false
t.references :user, null: false, foreign_key: true
t.boolean :published, null: false, default: false
t.string :slug, null: false
t.timestamps
end
end
end

13
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2023_09_29_145226) do
ActiveRecord::Schema[7.1].define(version: 2023_10_05_151621) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -52,6 +52,16 @@ ActiveRecord::Schema[7.1].define(version: 2023_09_29_145226) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "blog_posts", force: :cascade do |t|
t.string "title", null: false
t.bigint "user_id", null: false
t.boolean "published", default: false, null: false
t.string "slug", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_blog_posts_on_user_id"
end
create_table "microposts", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "created_at", null: false
@ -74,5 +84,6 @@ ActiveRecord::Schema[7.1].define(version: 2023_09_29_145226) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "blog_posts", "users"
add_foreign_key "microposts", "users"
end

View File

@ -2,3 +2,11 @@ one:
record: one (Micropost)
name: content
body: <p>Just some text</p>
published_blog_post:
record: published (BlogPost)
name: content
body: <p>Just some text</p>
draft_blog_post:
record: draft (BlogPost)
name: content
body: <p>Just some text</p>

11
test/fixtures/blog_posts.yml vendored Normal file
View File

@ -0,0 +1,11 @@
published:
title: A published post
user: trevor
published: true
slug: published
draft:
title: A draft post
user: trevor
published: false
slug: draft

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
require "test_helper"
class BlogPostTest < ActiveSupport::TestCase
setup do
@post = blog_posts(:published)
assert @post.valid?
end
test "must have a title" do
@post.title = nil
assert_not @post.valid?
end
test "must have a user" do
@post.user = nil
assert_not @post.valid?
end
test "must have a published status" do
@post.published = nil
assert_not @post.valid?
end
test "must have a slug" do
@post.slug = nil
assert_not @post.valid?
end
test "must have content" do
@post.content = nil
assert_not @post.valid?
end
end

View File

@ -1,7 +1,20 @@
# frozen_string_literal: true
require "test_helper"
class MicropostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
setup do
@micropost = microposts(:one)
assert @micropost.valid?
end
test "must have a user" do
@micropost.user = nil
assert_not @micropost.valid?
end
test "must have content" do
@micropost.content = nil
assert_not @micropost.valid?
end
end