From 0aace2986a9a7a6d4c84a9ba6819d3df7821e267 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 14 Oct 2023 10:15:24 -0400 Subject: Blogs have an author now --- app/controllers/admin/blogs_controller.rb | 1 + app/mailers/comment_mailer.rb | 6 ++---- app/models/blog.rb | 2 ++ app/models/user.rb | 2 ++ app/views/blogs/_blog.html.haml | 4 ++-- db/migrate/20231014140431_add_user_to_blog.rb | 5 +++++ db/schema.rb | 5 ++++- 7 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20231014140431_add_user_to_blog.rb diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index 8e288a8..f0ce519 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -25,6 +25,7 @@ class Admin::BlogsController < Admin::AdminController def create @blog = Blog.new(blog_params) + @blog.user = current_user if @blog.save flash.notice = "Blog created successfully!" diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb index e72b17d..aeed1b0 100644 --- a/app/mailers/comment_mailer.rb +++ b/app/mailers/comment_mailer.rb @@ -1,13 +1,11 @@ class CommentMailer < ApplicationMailer def new_comment_email @comment = params[:comment] - @admin = User.first # this is weird - mail(to: @admin.email, subject: "[Four Island] Comment on #{@comment.blog.title}") + mail(to: @comment.blog.user.email, subject: "[Four Island] Comment on #{@comment.blog.title}") end def new_pending_comment_email @comment = params[:comment] - @admin = User.first # this is weird - mail(to: @admin.email, subject: "[Four Island] Pending comment on #{@comment.blog.title}") + mail(to: @comment.blog.user.email, subject: "[Four Island] Pending comment on #{@comment.blog.title}") end end diff --git a/app/models/blog.rb b/app/models/blog.rb index e640466..e43990e 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -4,10 +4,12 @@ class Blog < ApplicationRecord acts_as_taggable has_many :comments + belongs_to :user validates :title, presence: true validates :body, presence: true, if: :published validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published + validates :user, presence: true before_validation :set_draft_title before_save :set_published_at diff --git a/app/models/user.rb b/app/models/user.rb index 555729a..f9f68fa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,4 +5,6 @@ class User < ApplicationRecord :recoverable, :rememberable, :trackable, :validatable has_secure_token :pokeviewer_token + + has_many :blogs end diff --git a/app/views/blogs/_blog.html.haml b/app/views/blogs/_blog.html.haml index 5467683..ec61bb5 100644 --- a/app/views/blogs/_blog.html.haml +++ b/app/views/blogs/_blog.html.haml @@ -5,7 +5,7 @@ %span.post-day= blog.visible_date.day .blog-title %h2= link_to_unless_current blog.title, blog - .post-author Hatkirby + .post-author= blog.user.login.capitalize %ul.post-tag-3 - blog.tags.each do |tag| %li= link_to tag, tag_url(tag.name) @@ -16,6 +16,6 @@ - else = markdown(blog.body) %cite.bubble - %strong Hatkirby + %strong= blog.user.login.capitalize on = blog.visible_date.strftime("%B #{blog.visible_date.day.ordinalize}, %Y at %-I:%M:%S%P") diff --git a/db/migrate/20231014140431_add_user_to_blog.rb b/db/migrate/20231014140431_add_user_to_blog.rb new file mode 100644 index 0000000..74af555 --- /dev/null +++ b/db/migrate/20231014140431_add_user_to_blog.rb @@ -0,0 +1,5 @@ +class AddUserToBlog < ActiveRecord::Migration[7.0] + def change + add_reference :blogs, :user, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2a9b1df..56fd9bb 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.0].define(version: 2023_10_12_190529) do +ActiveRecord::Schema[7.0].define(version: 2023_10_14_140431) do create_table "audits", force: :cascade do |t| t.integer "auditable_id" t.string "auditable_type" @@ -41,6 +41,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_12_190529) do t.datetime "updated_at", precision: nil, null: false t.boolean "published", default: false, null: false t.datetime "published_at", precision: nil + t.integer "user_id" + t.index ["user_id"], name: "index_blogs_on_user_id" end create_table "ckeditor_assets", force: :cascade do |t| @@ -358,6 +360,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_12_190529) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "blogs", "users" add_foreign_key "comments", "blogs" add_foreign_key "pokeviewer_items", "pokeviewer_moves", column: "move_id" add_foreign_key "pokeviewer_pokedex_entries", "pokeviewer_species", column: "species_id" -- cgit 1.4.1