From 2014730610da1682114db309e80158670cabaab8 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 5 Oct 2023 16:46:07 +0100 Subject: [PATCH] Add BlogPost model Also fleshed out Micropost tests. --- app/models/blog_post.rb | 19 ++++++++++ app/models/micropost.rb | 4 ++- .../20231005151621_create_blog_posts.rb | 12 +++++++ db/schema.rb | 13 ++++++- test/fixtures/action_text/rich_texts.yml | 8 +++++ test/fixtures/blog_posts.yml | 11 ++++++ test/models/blog_post_test.rb | 35 +++++++++++++++++++ test/models/micropost_test.rb | 19 ++++++++-- 8 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 app/models/blog_post.rb create mode 100644 db/migrate/20231005151621_create_blog_posts.rb create mode 100644 test/fixtures/blog_posts.yml create mode 100644 test/models/blog_post_test.rb diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb new file mode 100644 index 0000000..86a4bc0 --- /dev/null +++ b/app/models/blog_post.rb @@ -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 diff --git a/app/models/micropost.rb b/app/models/micropost.rb index 90e148a..89b9d52 100644 --- a/app/models/micropost.rb +++ b/app/models/micropost.rb @@ -4,5 +4,7 @@ class Micropost < ApplicationRecord belongs_to :user has_rich_text :content - validates :user, presence: true + validates :user, + :content, + presence: true end diff --git a/db/migrate/20231005151621_create_blog_posts.rb b/db/migrate/20231005151621_create_blog_posts.rb new file mode 100644 index 0000000..564f2a4 --- /dev/null +++ b/db/migrate/20231005151621_create_blog_posts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ea3b800..a43dd6a 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[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 diff --git a/test/fixtures/action_text/rich_texts.yml b/test/fixtures/action_text/rich_texts.yml index 3952ef5..10d53f0 100644 --- a/test/fixtures/action_text/rich_texts.yml +++ b/test/fixtures/action_text/rich_texts.yml @@ -2,3 +2,11 @@ one: record: one (Micropost) name: content body:

Just some text

+published_blog_post: + record: published (BlogPost) + name: content + body:

Just some text

+draft_blog_post: + record: draft (BlogPost) + name: content + body:

Just some text

diff --git a/test/fixtures/blog_posts.yml b/test/fixtures/blog_posts.yml new file mode 100644 index 0000000..6f7838f --- /dev/null +++ b/test/fixtures/blog_posts.yml @@ -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 diff --git a/test/models/blog_post_test.rb b/test/models/blog_post_test.rb new file mode 100644 index 0000000..217e52f --- /dev/null +++ b/test/models/blog_post_test.rb @@ -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 diff --git a/test/models/micropost_test.rb b/test/models/micropost_test.rb index 9a20fe6..2fd9540 100644 --- a/test/models/micropost_test.rb +++ b/test/models/micropost_test.rb @@ -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