From f8bb1b4a5bc2548dd7798f2b334d8e5c653c96af Mon Sep 17 00:00:00 2001 From: Sergio-e <33036058+Sergio-e@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:44:38 -0600 Subject: [PATCH] Back-end profile improvements --- Gemfile | 3 -- app/controllers/profiles_controller.rb | 29 ++++++++++-------- app/models/profile.rb | 31 ++++++++++---------- app/models/user.rb | 22 +++++++------- app/views/main/index.html.erb | 1 + app/views/profiles/edit.html.erb | 29 +++++++++--------- app/views/profiles/show.html.erb | 3 +- db/migrate/20240628210301_create_profiles.rb | 4 ++- db/migrate/20240628211820_create_users.rb | 2 -- db/schema.rb | 6 ++-- db/seeds.rb | 13 ++++++++ spec/factories/profiles.rb | 26 ++++++++-------- spec/factories/users.rb | 14 ++++----- spec/models/profile_spec.rb | 26 ++++++++-------- spec/models/user_spec.rb | 14 ++++----- 15 files changed, 120 insertions(+), 103 deletions(-) diff --git a/Gemfile b/Gemfile index c5e2aa88..3bdde43b 100644 --- a/Gemfile +++ b/Gemfile @@ -21,9 +21,6 @@ gem "tailwindcss-rails", "~> 2.6" gem "stimulus-rails" gem "turbo-rails" -# Authorization -gem "action_policy", "~> 0.7.0" - # Authentication gem "bcrypt", "~> 3.1.20" diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 73bfa55b..d65d099a 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -1,30 +1,35 @@ class ProfilesController < ApplicationController def show - @profile = current_user.profile + @profile = current_profile end def edit - @profile = current_user.profile || Profile.new + @profile = current_profile end def update - if current_user.profile.nil? - current_user.profile = Profile.new(profile_params) - current_user.save! + @profile = current_profile + @profile.assign_attributes(profile_params) + + if @profile.save + redirect_to profile_path, notice: t("controllers.profiles.update.success") else - current_user.profile.update!(profile_params) + # TODO display errors + render :edit, status: :unprocessable_entity end - current_user.update!(notifications_params) - redirect_to profile_path, notice: t("controllers.profiles.update.success") end private - def profile_params - params.permit(:name, :location, :bio, :is_public, :twitter_url, :linkedin_url, :github_url, :image) + def current_profile + current_user.profile || current_user.build_profile end - def notifications_params - params.permit(:in_app_notifications_enabled, :mail_notifications_enabled) + def profile_params + params.require(:profile).permit( + :name, :location, :bio, :is_public, :image, + :twitter_url, :linkedin_url, :github_url, + :in_app_notifications, :mail_notifications + ) end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 7194c3a9..df389ef0 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -2,29 +2,30 @@ # # Table name: profiles # -# id :integer not null, primary key -# bio :string -# github_url :string -# is_public :boolean default(FALSE), not null -# linkedin_url :string -# location :string -# name :string -# profileable_type :string not null -# twitter_url :string -# created_at :datetime not null -# updated_at :datetime not null -# profileable_id :integer not null +# id :integer not null, primary key +# bio :text +# github_url :string +# in_app_notifications :boolean default(TRUE), not null +# is_public :boolean default(FALSE), not null +# linkedin_url :string +# location :string +# mail_notifications :boolean default(TRUE), not null +# name :string +# profileable_type :string not null +# twitter_url :string +# created_at :datetime not null +# updated_at :datetime not null +# profileable_id :integer not null # # Indexes # # index_profiles_on_profileable (profileable_type,profileable_id) # class Profile < ApplicationRecord - belongs_to :profileable, polymorphic: true + has_one_attached :image - has_one :self_ref, class_name: "Profile", foreign_key: :id, inverse_of: :self_ref, dependent: :destroy has_one :user, through: :self_ref, source: :profileable, source_type: "User" has_one :speaker, through: :self_ref, source: :profileable, source_type: "Speaker" - has_one_attached :image + belongs_to :profileable, polymorphic: true end diff --git a/app/models/user.rb b/app/models/user.rb index 8b641a70..72cbddf6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,14 +2,12 @@ # # Table name: users # -# id :integer not null, primary key -# email :string not null -# in_app_notifications_enabled :boolean default(TRUE), not null -# mail_notifications_enabled :boolean default(TRUE), not null -# password_digest :string not null -# role :string -# created_at :datetime not null -# updated_at :datetime not null +# id :integer not null, primary key +# email :string not null +# password_digest :string not null +# role :string +# created_at :datetime not null +# updated_at :datetime not null # # Indexes # @@ -20,6 +18,10 @@ class User < ApplicationRecord normalizes :email, with: ->(email) { email.strip.downcase } + generates_token_for :password_reset, expires_in: PASSWORD_RESET_EXPIRATION do + password_salt&.last(10) + end + has_secure_password has_one :profile, as: :profileable, dependent: :destroy @@ -30,7 +32,5 @@ class User < ApplicationRecord validates :password_digest, presence: true validates :password, length: {minimum: 8}, if: -> { password.present? } - generates_token_for :password_reset, expires_in: PASSWORD_RESET_EXPIRATION do - password_salt&.last(10) - end + after_create_commit { create_profile! } end diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb index bae11ff6..f2d623dc 100644 --- a/app/views/main/index.html.erb +++ b/app/views/main/index.html.erb @@ -1 +1,2 @@
Discoverability
Notifications