Skip to content

Commit

Permalink
Merge pull request #69 from Code-the-Dream-School/68_fix_profil_page
Browse files Browse the repository at this point in the history
complete user profile page
  • Loading branch information
christopher-ga authored Jan 9, 2025
2 parents 516bd45 + dfa5ec3 commit 4762cfc
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 17 deletions.
31 changes: 27 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[show edit update ]
before_action :set_user, only: %i[show edit update update_bio]
before_action :set_user_for_upload, only: [:upload_profile_picture]

def index
@users = User.all
end

def show
@friends = @user.friends
@entries = @user.entries
end


def edit
end

Expand All @@ -20,14 +22,35 @@ def update
end
end

def upload_profile_picture
if params[:user][:profile_picture].present?
@user.profile_picture.attach(params[:user][:profile_picture])
redirect_to @user, notice: 'Profile picture uploaded successfully.'
else
redirect_to @user, alert: 'No picture selected.'
end
end

def update_bio
@user = current_user
if @user.update(user_params)
redirect_to @user, notice: 'Bio updated successfully.'
else
render :show
end
end

private

def set_user
@user = User.find_by(id: params[:id])
@user = current_user
end

def set_user_for_upload
@user = User.find(params[:id])
end

def user_params
params.require(:user).permit(:email, :username, :bio, :profile_picture)
end
end
end
100 changes: 88 additions & 12 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,94 @@
<div class="container mt-4">
<div class="card mb-3 shadow-sm" style="max-width: 600px; margin: 0 auto;">
<div class="row g-0">
<% if @user.profile_picture.attached? %>
<div class="col-md-4">
<%= image_tag @user.profile_picture, class: "img-fluid rounded-start", alt: "Profile picture of #{@user.first_name}" %>
<div class="row mb-4">
<div class="col-md-4">
<div class="card">
<div class="card-body text-center">
<h3 class="card-title"><%= @user.username %></h3>

<% if @user.profile_picture.attached? %>
<%= image_tag @user.profile_picture, class: "img-fluid rounded-circle", alt: "Profile picture of #{@user.username}", style: "width: 150px; height: 150px; object-fit: cover;" %>
<% else %>
<%= image_tag 'default.png', class: "img-fluid rounded-circle", alt: "Default profile picture" %>
<% end %>

<%= form_with model: @user, url: upload_profile_picture_user_path(@user), method: :patch, local: true, enctype: "multipart/form-data" do |f| %>
<div class="form-group mt-3">
<%= f.file_field :profile_picture, class: 'form-control' %>
</div>
<%= f.submit 'Upload Picture', class: 'btn btn-primary mt-3' %>
<% end %>
</div>
<% end %>
<div class="col-md-8">
</div>
</div>


<div class="col-md-8">
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= @user.username %> </h5>
<% if @user.bio.present? %>
<p class="card-text"><%= @user.bio %></p>
<% else %>
<p class="card-text text-muted">No bio available.</p>
<h4>Bio</h4>
<%= form_with model: @user, url: update_bio_user_path(@user), method: :patch do |f| %>
<div class="form-group">
<%= f.text_area :bio, class: 'form-control', rows: 5, placeholder: "No bio yet" %>
</div>
<%= f.submit 'Update Bio', class: 'btn btn-primary mt-2' %>
<% end %>
</div>
</div>
</div>
</div>

<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h4>Friends</h4>
<div class="list-group">
<% @user.friends.each do |friend| %>
<a href="<%= profile_path(friend) %>" class="list-group-item list-group-item-action">
<%= image_tag(friend.profile_picture, class: 'img-thumbnail mr-2', alt: friend.username) if friend.profile_picture.attached? %>
<%= friend.username %>
</a>
<% end %>
</div>
</div>
</div>
</div>

<div class="col-md-8">
<div class="card">
<div class="card-body">
<h4 class="mt-4">Your Entries</h4>
<% @user.entries.each do |entry| %>
<div class="card mb-3">
<div class="card-body">
<p><%= entry.text %></p>
<small><%= entry.created_at.strftime("%B %d, %Y") %></small>

<div class="comments-section mt-3">
<h5>Comments</h5>
<div class="comments-container" style="max-height: 200px; overflow-y: auto;">
<% entry.comments.each do |comment| %>
<div class="comment">
<div class="d-flex mb-2">
<%= image_tag(comment.user.profile_picture, class: 'img-thumbnail mr-2', alt: comment.user.username) if comment.user.profile_picture.attached? %>
<div>
<strong><%= comment.user.username %></strong>
<p><%= comment.text %></p>
</div>
</div>
</div>
<% end %>
</div>

<%= form_with model: [entry, Comment.new], local: true do |f| %>
<div class="form-group">
<%= f.text_area :text, class: 'form-control', placeholder: 'Add a comment...' %>
</div>
<%= f.submit 'Post Comment', class: 'btn btn-primary btn-sm' %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
resources :comments, only: [:index, :show, :new, :create, :edit, :update, :destroy]
end

resources :users, only: [:show, :edit, :update]
resources :users, only: [:show, :edit, :update] do
member do
patch :update_bio
patch :upload_profile_picture
end
end

resources :notifications, only: [:show, :edit, :update, :new, :create]

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20250107013021_change_default_for_bio_in_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeDefaultForBioInUsers < ActiveRecord::Migration[6.0]
def change
change_column_default :users, :bio, nil
end
end
Binary file added public/cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added spec/requests/dashboard_spec.rb
Empty file.
Empty file added spec/requests/entries_spec.rb
Empty file.
Empty file.
50 changes: 50 additions & 0 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rails_helper'

RSpec.describe "Users", type: :request do
let!(:user) { create(:user, bio: nil, profile_picture: Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/files/default.png'), 'image/png')) }

let(:valid_attributes) do
{
bio: Faker::Lorem.sentence,
profile_picture: Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/files/default.png'), 'image/png')
}
end

let(:invalid_attributes) do
{
bio: nil,
profile_picture: nil
}
end

describe "GET /edit" do
it "returns a successful response" do
sign_in user
get edit_user_registration_path
expect(response).to have_http_status(:ok)
end
end

describe "PATCH /update" do
context "with valid parameters" do
it "updates the user's bio and profile picture" do
sign_in user
patch user_registration_path, params: { user: valid_attributes }
user.reload
expect(user.bio).to eq(valid_attributes[:bio])
expect(user.profile_picture).to be_attached
end
end

context "with invalid parameters" do
it "does not update the user and re-renders the edit template" do
sign_in user
patch user_registration_path, params: { user: invalid_attributes }
user.reload
expect(user.bio).to be_nil
expect(user.profile_picture.filename).to eq('default.png')
expect(response.body).to include('error')
end
end
end
end

0 comments on commit 4762cfc

Please sign in to comment.