Make microposts polymorphic for profiles and users

This commit is contained in:
TheNeedful-DO 2023-11-15 16:11:32 +00:00
parent 89eb06febb
commit 466f98ea99
10 changed files with 39 additions and 16 deletions

View File

@ -1,13 +1,14 @@
class MicropostsController < ApplicationController class MicropostsController < ApplicationController
before_action :set_micropost, only: %i[ show edit update destroy ] before_action :set_micropost, only: %i[ show edit update destroy ]
def create def create
@micropost = current_user.microposts.build(micropost_params) @micropost = Micropost.new(micropost_params)
authorize @micropost authorize @micropost
if @micropost.save if @micropost.save
flash[:success] = "Micropost created!" flash[:success] = "Post created!"
redirect_to root_url redirect_to request.referrer || root_url
else else
render 'users/show' flash[:error] = "Post failed!"
redirect_to request.referrer || root_url
end end
end end
@ -27,6 +28,6 @@ class MicropostsController < ApplicationController
end end
def micropost_params def micropost_params
params.require(:micropost).permit(:content) params.require(:micropost).permit(:content, :micropostable_type, :micropostable_id)
end end
end end

View File

@ -10,6 +10,12 @@ class ProfilesController < ApplicationController
# GET /profiles/1 or /profiles/1.json # GET /profiles/1 or /profiles/1.json
def show def show
authorize @profile 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 end
# GET /profiles/new # GET /profiles/new

View File

@ -1,6 +1,7 @@
class Micropost < ActiveRecord::Base class Micropost < ActiveRecord::Base
belongs_to :user belongs_to :micropostable, polymorphic: true
default_scope -> { order(created_at: :desc) } default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 } validates :content, presence: true, length: { maximum: 140 }
# belongs_to :user
# validates :user_id, presence: true
end end

View File

@ -5,4 +5,9 @@ class Profile < ActiveRecord::Base
foreign_key: "followed_id", foreign_key: "followed_id",
dependent: :destroy dependent: :destroy
has_many :followers, through: :passive_relationships, source: :follower has_many :followers, through: :passive_relationships, source: :follower
has_many :microposts, as: :micropostable
def display_name
name
end
end end

View File

@ -11,7 +11,7 @@ class User < ActiveRecord::Base
foreign_key: "follower_id", foreign_key: "follower_id",
dependent: :destroy dependent: :destroy
has_many :following, through: :active_relationships, source: :followed has_many :following, through: :active_relationships, source: :followed
has_many :microposts, dependent: :destroy has_many :microposts, as: :micropostable
def icid def icid
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize

View File

@ -1,5 +1,5 @@
%li{id: "micropost-#{micropost.id}"} %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.content= micropost.content
%span.timestamp Posted #{time_ago_in_words(micropost.created_at)} ago. %span.timestamp Posted #{time_ago_in_words(micropost.created_at)} ago.
- if policy(micropost).destroy? - if policy(micropost).destroy?

View File

@ -25,3 +25,14 @@
.stats .stats
%p #{@profile.followers.count} Followers %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

View File

@ -2,5 +2,7 @@
= f.error_notification = f.error_notification
.form-inputs .form-inputs
= f.text_area :content, placeholder: "I got something to say..." = f.text_area :content, placeholder: "I got something to say..."
= f.hidden_field :micropostable_type
= f.hidden_field :micropostable_id
.form-actions .form-actions
= f.button :submit, "Post" = f.button :submit, "Post"

View File

@ -2,10 +2,8 @@ class CreateMicroposts < ActiveRecord::Migration[6.1]
def change def change
create_table :microposts do |t| create_table :microposts do |t|
t.text :content t.text :content
t.references :user, null: false, foreign_key: true t.references :micropostable, polymorphic: true
t.timestamps t.timestamps
end end
add_index :microposts, [:user_id, :created_at]
end end
end end

View File

@ -58,11 +58,11 @@ ActiveRecord::Schema.define(version: 2023_11_14_160910) do
create_table "microposts", force: :cascade do |t| create_table "microposts", force: :cascade do |t|
t.text "content" 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 "created_at", precision: 6, null: false
t.datetime "updated_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 ["micropostable_type", "micropostable_id"], name: "index_microposts_on_micropostable"
t.index ["user_id"], name: "index_microposts_on_user_id"
end end
create_table "play_to_beats", force: :cascade do |t| 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 "canisters", "users"
add_foreign_key "doodads", "users" add_foreign_key "doodads", "users"
add_foreign_key "microposts", "users"
add_foreign_key "profiles", "users" add_foreign_key "profiles", "users"
add_foreign_key "projects", "users" add_foreign_key "projects", "users"
add_foreign_key "social_links", "profiles" add_foreign_key "social_links", "profiles"