diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0669180..834b644 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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 \ No newline at end of file +end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 9c48307..6a8f4fa 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,18 +1,94 @@
-
-
- <% if @user.profile_picture.attached? %> -
- <%= image_tag @user.profile_picture, class: "img-fluid rounded-start", alt: "Profile picture of #{@user.first_name}" %> +
+
+
+
+

<%= @user.username %>

+ + <% 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| %> +
+ <%= f.file_field :profile_picture, class: 'form-control' %> +
+ <%= f.submit 'Upload Picture', class: 'btn btn-primary mt-3' %> + <% end %>
- <% end %> -
+
+
+ + +
+
-
<%= @user.username %>
- <% if @user.bio.present? %> -

<%= @user.bio %>

- <% else %> -

No bio available.

+

Bio

+ <%= form_with model: @user, url: update_bio_user_path(@user), method: :patch do |f| %> +
+ <%= f.text_area :bio, class: 'form-control', rows: 5, placeholder: "No bio yet" %> +
+ <%= f.submit 'Update Bio', class: 'btn btn-primary mt-2' %> + <% end %> +
+
+
+
+ +
+ + +
+
+
+

Your Entries

+ <% @user.entries.each do |entry| %> +
+
+

<%= entry.text %>

+ <%= entry.created_at.strftime("%B %d, %Y") %> + +
+
Comments
+
+ <% entry.comments.each do |comment| %> +
+
+ <%= image_tag(comment.user.profile_picture, class: 'img-thumbnail mr-2', alt: comment.user.username) if comment.user.profile_picture.attached? %> +
+ <%= comment.user.username %> +

<%= comment.text %>

+
+
+
+ <% end %> +
+ + <%= form_with model: [entry, Comment.new], local: true do |f| %> +
+ <%= f.text_area :text, class: 'form-control', placeholder: 'Add a comment...' %> +
+ <%= f.submit 'Post Comment', class: 'btn btn-primary btn-sm' %> + <% end %> +
+
+
<% end %>
diff --git a/config/routes.rb b/config/routes.rb index 8800730..4fecd41 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/db/migrate/20250107013021_change_default_for_bio_in_users.rb b/db/migrate/20250107013021_change_default_for_bio_in_users.rb new file mode 100644 index 0000000..298cc5e --- /dev/null +++ b/db/migrate/20250107013021_change_default_for_bio_in_users.rb @@ -0,0 +1,5 @@ +class ChangeDefaultForBioInUsers < ActiveRecord::Migration[6.0] + def change + change_column_default :users, :bio, nil + end +end \ No newline at end of file diff --git a/public/cat.png b/public/cat.png new file mode 100644 index 0000000..ad7eee8 Binary files /dev/null and b/public/cat.png differ diff --git a/spec/requests/dashboard_spec.rb b/spec/requests/dashboard_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/requests/entries_spec.rb b/spec/requests/entries_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/requests/friendships_spec.rb b/spec/requests/friendships_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb new file mode 100644 index 0000000..357c141 --- /dev/null +++ b/spec/requests/users_spec.rb @@ -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