parent
5e5737b132
commit
2014730610
|
@ -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
|
|
@ -4,5 +4,7 @@ class Micropost < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
has_rich_text :content
|
has_rich_text :content
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user,
|
||||||
|
:content,
|
||||||
|
presence: true
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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[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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
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
|
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||||
end
|
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|
|
create_table "microposts", force: :cascade do |t|
|
||||||
t.bigint "user_id", null: false
|
t.bigint "user_id", null: false
|
||||||
t.datetime "created_at", 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_attachments", "active_storage_blobs", column: "blob_id"
|
||||||
add_foreign_key "active_storage_variant_records", "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"
|
add_foreign_key "microposts", "users"
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,3 +2,11 @@ one:
|
||||||
record: one (Micropost)
|
record: one (Micropost)
|
||||||
name: content
|
name: content
|
||||||
body: <p>Just some text</p>
|
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>
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -1,7 +1,20 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class MicropostTest < ActiveSupport::TestCase
|
class MicropostTest < ActiveSupport::TestCase
|
||||||
# test "the truth" do
|
setup do
|
||||||
# assert true
|
@micropost = microposts(:one)
|
||||||
# end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue