Make microposts polymorphic for profiles and users

This commit is contained in:
Jesse C. Fisher 2023-11-15 16:11:32 +00:00
parent a6f084a372
commit 0f11f515d2
10 changed files with 39 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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"