From 669df703aacb7964bbff14bf0cda6e5766c3847c Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Wed, 15 Nov 2023 16:11:32 +0000 Subject: [PATCH] Make microposts polymorphic for profiles and users --- app/controllers/microposts_controller.rb | 11 ++++++----- app/controllers/profiles_controller.rb | 6 ++++++ app/models/micropost.rb | 5 +++-- app/models/profile.rb | 5 +++++ app/models/user.rb | 2 +- app/views/microposts/_micropost.html.haml | 2 +- app/views/profiles/show.html.haml | 11 +++++++++++ app/views/shared/_micropost_form.html.haml | 2 ++ db/migrate/20231114160910_create_microposts.rb | 4 +--- db/schema.rb | 7 +++---- 10 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb index 4941b75d..51a32bba 100644 --- a/app/controllers/microposts_controller.rb +++ b/app/controllers/microposts_controller.rb @@ -1,13 +1,14 @@ class MicropostsController < ApplicationController before_action :set_micropost, only: %i[ show edit update destroy ] def create - @micropost = current_user.microposts.build(micropost_params) + @micropost = Micropost.new(micropost_params) authorize @micropost if @micropost.save - flash[:success] = "Micropost created!" - redirect_to root_url + flash[:success] = "Post created!" + redirect_to request.referrer || root_url else - render 'users/show' + flash[:error] = "Post failed!" + redirect_to request.referrer || root_url end end @@ -27,6 +28,6 @@ class MicropostsController < ApplicationController end def micropost_params - params.require(:micropost).permit(:content) + params.require(:micropost).permit(:content, :micropostable_type, :micropostable_id) end end diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index c790f0a3..b83fe368 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -10,6 +10,12 @@ class ProfilesController < ApplicationController # GET /profiles/1 or /profiles/1.json def show authorize @profile + if @profile.user == current_user + @micropost = @profile.microposts.build + else + @micropost = current_user.microposts.build if current_user.signed_in? + end + @microposts = @profile.microposts.paginate(page: params[:page]) end # GET /profiles/new diff --git a/app/models/micropost.rb b/app/models/micropost.rb index ad4b4197..c309c277 100644 --- a/app/models/micropost.rb +++ b/app/models/micropost.rb @@ -1,6 +1,7 @@ class Micropost < ActiveRecord::Base - belongs_to :user + belongs_to :micropostable, polymorphic: true default_scope -> { order(created_at: :desc) } - validates :user_id, presence: true validates :content, presence: true, length: { maximum: 140 } + # belongs_to :user + # validates :user_id, presence: true end diff --git a/app/models/profile.rb b/app/models/profile.rb index 4c84113a..14225d5b 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -5,4 +5,9 @@ class Profile < ActiveRecord::Base foreign_key: "followed_id", dependent: :destroy has_many :followers, through: :passive_relationships, source: :follower + has_many :microposts, as: :micropostable + + def display_name + name + end end diff --git a/app/models/user.rb b/app/models/user.rb index 21c6b919..84164b14 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,7 +11,7 @@ class User < ActiveRecord::Base foreign_key: "follower_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed - has_many :microposts, dependent: :destroy + has_many :microposts, as: :micropostable def icid stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize diff --git a/app/views/microposts/_micropost.html.haml b/app/views/microposts/_micropost.html.haml index 60bf0b8a..35b64c85 100644 --- a/app/views/microposts/_micropost.html.haml +++ b/app/views/microposts/_micropost.html.haml @@ -1,5 +1,5 @@ %li{id: "micropost-#{micropost.id}"} - %span= link_to micropost.user.display_name, micropost.user + %span= link_to micropost.micropostable.display_name, micropost.micropostable %span.content= micropost.content %span.timestamp Posted #{time_ago_in_words(micropost.created_at)} ago. - if policy(micropost).destroy? diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml index 6cf0766a..f4b6960a 100644 --- a/app/views/profiles/show.html.haml +++ b/app/views/profiles/show.html.haml @@ -25,3 +25,14 @@ .stats %p #{@profile.followers.count} Followers + +- if @profile.user == current_user + %section.micropost_form + = render 'shared/micropost_form' + +- if @profile.microposts.any? + %section.microposts + %h3 Posts #{@microposts.count} + %ol.microposts + = render @microposts + = will_paginate @microposts diff --git a/app/views/shared/_micropost_form.html.haml b/app/views/shared/_micropost_form.html.haml index 66e50c57..d9fafb92 100644 --- a/app/views/shared/_micropost_form.html.haml +++ b/app/views/shared/_micropost_form.html.haml @@ -2,5 +2,7 @@ = f.error_notification .form-inputs = f.text_area :content, placeholder: "I got something to say..." + = f.hidden_field :micropostable_type + = f.hidden_field :micropostable_id .form-actions = f.button :submit, "Post" diff --git a/db/migrate/20231114160910_create_microposts.rb b/db/migrate/20231114160910_create_microposts.rb index 869c6337..e636e490 100644 --- a/db/migrate/20231114160910_create_microposts.rb +++ b/db/migrate/20231114160910_create_microposts.rb @@ -2,10 +2,8 @@ class CreateMicroposts < ActiveRecord::Migration[6.1] def change create_table :microposts do |t| t.text :content - t.references :user, null: false, foreign_key: true - + t.references :micropostable, polymorphic: true t.timestamps end - add_index :microposts, [:user_id, :created_at] end end diff --git a/db/schema.rb b/db/schema.rb index 6fc54316..814443f2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -58,11 +58,11 @@ ActiveRecord::Schema.define(version: 2023_11_14_160910) do create_table "microposts", force: :cascade do |t| t.text "content" - t.bigint "user_id", null: false + t.string "micropostable_type" + t.bigint "micropostable_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at" - t.index ["user_id"], name: "index_microposts_on_user_id" + t.index ["micropostable_type", "micropostable_id"], name: "index_microposts_on_micropostable" end create_table "play_to_beats", force: :cascade do |t| @@ -222,7 +222,6 @@ ActiveRecord::Schema.define(version: 2023_11_14_160910) do add_foreign_key "canisters", "users" add_foreign_key "doodads", "users" - add_foreign_key "microposts", "users" add_foreign_key "profiles", "users" add_foreign_key "projects", "users" add_foreign_key "social_links", "profiles"