From e8d66e524397e6d7b31699bf14b3e7d58d99734a Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Wed, 15 Nov 2023 13:05:07 +0000 Subject: [PATCH] Add post feed --- app/controllers/microposts_controller.rb | 11 +++++++++++ app/controllers/visitors_controller.rb | 1 + app/models/user.rb | 4 ++++ app/policies/user_policy.rb | 2 +- app/views/microposts/_micropost.html.haml | 2 ++ app/views/shared/_feed.html.haml | 6 ++++++ app/views/users/show.html.haml | 5 +++-- app/views/visitors/index.html.haml | 5 +++++ 8 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 app/views/shared/_feed.html.haml diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb index 29ff2fda..4941b75d 100644 --- a/app/controllers/microposts_controller.rb +++ b/app/controllers/microposts_controller.rb @@ -1,4 +1,5 @@ class MicropostsController < ApplicationController + before_action :set_micropost, only: %i[ show edit update destroy ] def create @micropost = current_user.microposts.build(micropost_params) authorize @micropost @@ -11,9 +12,19 @@ class MicropostsController < ApplicationController end def destroy + authorize @micropost + @micropost.destroy + respond_to do |format| + format.html { redirect_to request.referrer || root_url, notice: "Post was deleted." } + format.json { head :no_content } + end end private + # Use callbacks to share common setup or constraints between actions. + def set_micropost + @micropost = Micropost.find(params[:id]) + end def micropost_params params.require(:micropost).permit(:content) diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb index 16b78e44..debc151b 100644 --- a/app/controllers/visitors_controller.rb +++ b/app/controllers/visitors_controller.rb @@ -4,5 +4,6 @@ class VisitorsController < ApplicationController def index @doodads = authorize policy_scope(Doodad) @canisters = authorize Canister.all + @feed_items = current_user.feed.paginate(page: params[:page]) end end diff --git a/app/models/user.rb b/app/models/user.rb index 7dbcd249..21c6b919 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -121,4 +121,8 @@ class User < ActiveRecord::Base following.include?(leader) end + def feed + Micropost.where("user_id = ?", id) + end + end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 50a9c0ad..0b8d7ba5 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -30,7 +30,7 @@ class UserPolicy end def show? - @current_user.admin? || @current_user == @user + @current_user.admin? || @current_user == @user || true end def home? diff --git a/app/views/microposts/_micropost.html.haml b/app/views/microposts/_micropost.html.haml index 1d324ced..60bf0b8a 100644 --- a/app/views/microposts/_micropost.html.haml +++ b/app/views/microposts/_micropost.html.haml @@ -2,3 +2,5 @@ %span= link_to micropost.user.display_name, micropost.user %span.content= micropost.content %span.timestamp Posted #{time_ago_in_words(micropost.created_at)} ago. + - if policy(micropost).destroy? + %span= link_to "❌", micropost, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/app/views/shared/_feed.html.haml b/app/views/shared/_feed.html.haml new file mode 100644 index 00000000..10c4cb03 --- /dev/null +++ b/app/views/shared/_feed.html.haml @@ -0,0 +1,6 @@ +- if @feed_items.any? + %ol.microposts + = render @feed_items + = will_paginate @feed_items +- else + %span No items in feed. diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml index 1220de69..fbe0f0e1 100644 --- a/app/views/users/show.html.haml +++ b/app/views/users/show.html.haml @@ -1,5 +1,6 @@ -%section.micropost_form - = render 'shared/micropost_form' +- if current_user.signed_in? + %section.micropost_form + = render 'shared/micropost_form' .contain %h1 #{@user.display_name if @user.display_name} diff --git a/app/views/visitors/index.html.haml b/app/views/visitors/index.html.haml index b59100db..6a041c0f 100644 --- a/app/views/visitors/index.html.haml +++ b/app/views/visitors/index.html.haml @@ -4,8 +4,13 @@ - else %h1 Web3 Social Gaming
Crypto Rewards +- if @feed_items.any? + .contain + %h2 Post feed + = render 'shared/feed' .contain #doodads + %h2 Doodads - if current_user.signed_in? %p= link_to '+ New Doodad', new_doodad_path = render @doodads