From e3a01ae633e4d00f6145501893285e1c01d0223c Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Fri, 6 Oct 2023 08:26:16 +0100 Subject: [PATCH] Add summaries to blog posts --- app/controllers/blog_posts_controller.rb | 1 + app/models/blog_post.rb | 1 + app/views/blog_posts/_blog_post.html.erb | 4 ++-- app/views/blog_posts/_form.html.erb | 3 +++ app/views/blog_posts/show.html.erb | 9 ++++++++- db/migrate/20231006070935_add_summary_to_blog_post.rb | 7 +++++++ db/schema.rb | 3 ++- test/fixtures/blog_posts.yml | 2 ++ test/models/blog_post_test.rb | 5 +++++ 9 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20231006070935_add_summary_to_blog_post.rb diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb index eab1540..5daaf4c 100644 --- a/app/controllers/blog_posts_controller.rb +++ b/app/controllers/blog_posts_controller.rb @@ -52,6 +52,7 @@ class BlogPostsController < ApplicationController params.require(:blog_post).permit( :title, :content, + :summary, :published, :slug, ) diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb index 86a4bc0..ced1f25 100644 --- a/app/models/blog_post.rb +++ b/app/models/blog_post.rb @@ -9,6 +9,7 @@ class BlogPost < ApplicationRecord :published, :slug, :content, + :summary, presence: true validates :slug, uniqueness: true diff --git a/app/views/blog_posts/_blog_post.html.erb b/app/views/blog_posts/_blog_post.html.erb index c5ad15c..7d70142 100644 --- a/app/views/blog_posts/_blog_post.html.erb +++ b/app/views/blog_posts/_blog_post.html.erb @@ -1,6 +1,6 @@
-

<%= blog_post.title %>

- <%= blog_post.content %> +

<%= link_to blog_post.title, blog_post %>

+

<%= blog_post.summary %>

<%= link_to "Edit", edit_blog_post_path(blog_post) if blog_post.user == current_user %> <%= link_to blog_post.created_at.strftime("%Y-%m-%d %H:%M"), blog_post %> diff --git a/app/views/blog_posts/_form.html.erb b/app/views/blog_posts/_form.html.erb index 762ecce..b0ff114 100644 --- a/app/views/blog_posts/_form.html.erb +++ b/app/views/blog_posts/_form.html.erb @@ -5,6 +5,9 @@ <%= f.rich_text_area :content %> + <%= f.label :summary %> + <%= f.text_area :summary %> + <%= f.label :slug %> <%= f.text_field :slug %> diff --git a/app/views/blog_posts/show.html.erb b/app/views/blog_posts/show.html.erb index 04fbf6f..a9131a0 100644 --- a/app/views/blog_posts/show.html.erb +++ b/app/views/blog_posts/show.html.erb @@ -1,3 +1,10 @@ -<%= render @blog_post %> +
+

<%= @blog_post.title %>

+

<%= @blog_post.content %>

+
+ <%= link_to "Edit", edit_blog_post_path(@blog_post) if @blog_post.user == current_user %> + <%= link_to @blog_post.created_at.strftime("%Y-%m-%d %H:%M"), @blog_post %> +
+
<%= t(".discuss_post_html") %> diff --git a/db/migrate/20231006070935_add_summary_to_blog_post.rb b/db/migrate/20231006070935_add_summary_to_blog_post.rb new file mode 100644 index 0000000..347249f --- /dev/null +++ b/db/migrate/20231006070935_add_summary_to_blog_post.rb @@ -0,0 +1,7 @@ +class AddSummaryToBlogPost < ActiveRecord::Migration[7.1] + def change + add_column :blog_posts, :summary, :string, null: true + BlogPost.update_all(summary: "") + change_column_null :blog_posts, :summary, false + end +end diff --git a/db/schema.rb b/db/schema.rb index a43dd6a..3a4ee54 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_10_05_151621) do +ActiveRecord::Schema[7.1].define(version: 2023_10_06_070935) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -59,6 +59,7 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_05_151621) do t.string "slug", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "summary", null: false t.index ["user_id"], name: "index_blog_posts_on_user_id" end diff --git a/test/fixtures/blog_posts.yml b/test/fixtures/blog_posts.yml index 6f7838f..afb2e2c 100644 --- a/test/fixtures/blog_posts.yml +++ b/test/fixtures/blog_posts.yml @@ -3,9 +3,11 @@ published: user: trevor published: true slug: published + summary: Published! draft: title: A draft post user: trevor published: false slug: draft + summary: Draft! diff --git a/test/models/blog_post_test.rb b/test/models/blog_post_test.rb index 217e52f..4c8f434 100644 --- a/test/models/blog_post_test.rb +++ b/test/models/blog_post_test.rb @@ -32,4 +32,9 @@ class BlogPostTest < ActiveSupport::TestCase @post.content = nil assert_not @post.valid? end + + test "must have summary" do + @post.summary = nil + assert_not @post.valid? + end end