diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..306ab33 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.1.4 diff --git a/app/assets/images/default_avatar.jpeg b/app/assets/images/default_avatar.jpeg new file mode 100644 index 0000000..903ab6b Binary files /dev/null and b/app/assets/images/default_avatar.jpeg differ diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index cc9d429..f467b01 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -1,4 +1,12 @@ class ProfilesController < ApplicationController - def show + before_action :authenticate_user! + before_action :set_user + + def show; end + + private + + def set_user + @user = current_user end end diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb new file mode 100644 index 0000000..475f6f5 --- /dev/null +++ b/app/helpers/profile_helper.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Helper methods for the User Profile +module ProfileHelper + def avatar_for(user, classes: "h-12 w-12 rounded-full" ) + user_name = user.name || "User" + if user.avatar.attached? + image_tag user.avatar, alt: user_name, class: classes + else + image_tag 'default_avatar.jpeg', alt: user_name, class: classes + end + end +end diff --git a/app/models/profile.rb b/app/models/profile.rb index 638716d..8f12731 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -2,4 +2,20 @@ class Profile < ApplicationRecord belongs_to :user has_one_attached :picture + + before_validation :set_default_picture, on: :create + + alias owner user + alias avatar picture + + private + + def set_default_picture + return if picture.attached? + + picture.attach(io: File.open(Rails.root.join('app', 'assets', 'images', 'default_avatar.jpeg')), + filename: 'default_avatar.jpeg', + content_type: 'image/jpeg') + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 856f301..98d73ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,8 @@ class User < ApplicationRecord after_create :new_profile + delegate :name, :address_1, :address_2, :city, :state, :country, :avatar, to: :profile + def new_profile self.profile = Profile.new save! diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index a0ac11f..cc0723f 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -51,8 +51,8 @@