Skip to content

Commit

Permalink
Merge pull request #48 from loftwah/dl/fix-profile-update-2
Browse files Browse the repository at this point in the history
Dl/fix profile update 2
  • Loading branch information
loftwah authored Aug 24, 2024
2 parents 7a006af + 3a74681 commit cf1c5fd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
24 changes: 13 additions & 11 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,27 @@ def edit
def update
@user = current_user
@user.tags = JSON.parse(@user.tags) if @user.tags.is_a?(String)

if params[:user][:password].present? || params[:user][:password_confirmation].present?

# Check if password or email is being updated
is_password_change = params[:user][:password].present? || params[:user][:password_confirmation].present?
is_email_change = params[:user][:email].present? && params[:user][:email] != @user.email

if is_password_change || is_email_change
# Require current password for sensitive changes
successfully_updated = @user.update_with_password(account_update_params)
else
# Do not require current password for non-sensitive changes
params[:user].delete(:current_password)
successfully_updated = @user.update_without_password(account_update_params.except(:current_password))
successfully_updated = @user.update_without_password(account_update_params)
end

if successfully_updated
bypass_sign_in(@user) # Sign in the user bypassing validation
redirect_to edit_user_registration_path, notice: 'Profile updated successfully'
else
render :edit
end
end
end

protected

Expand All @@ -64,12 +70,8 @@ def sign_up_params
end

def account_update_params
params.require(:user).permit(:email, :password, :password_confirmation, :current_password,
:username, :full_name, :avatar, :banner, :description, :tags,
:public_analytics).tap do |user_params|
if user_params[:tags].present?
user_params[:tags] = user_params[:tags].split(',').map(&:strip).to_json
end
params.require(:user).permit(:email, :password, :password_confirmation, :current_password, :username, :full_name, :tags, :avatar, :banner, :description, :public_analytics).tap do |user_params|
user_params[:tags] = user_params[:tags].split(',').map(&:strip).to_json if user_params[:tags].present?
end
end
end
18 changes: 6 additions & 12 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
Rails.application.routes.draw do
get 'analytics/index'
if Rails.env.production?
devise_for :users, skip: [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
put 'users' => 'devise/registrations#update', as: 'user_registration'
end
else
devise_for :users, controllers: {
registrations: 'users/registrations'
}
end

# Use the custom registrations controller in all environments
devise_for :users, controllers: {
registrations: 'users/registrations'
}

resources :links do
member do
Expand All @@ -29,4 +23,4 @@
resources :links, only: [:index, :show, :new, :create, :edit, :update, :destroy]
resources :achievements, only: [:index, :show, :new, :create, :edit, :update, :destroy]
get '/:username', to: 'links#user_links', as: 'user_links'
end
end
35 changes: 35 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
app:
build: . # This tells Docker Compose to build the image from the Dockerfile in the current directory
ports:
- "80:3000"
env_file:
- .env
environment:
- RAILS_ENV=production
- REDIS_URL=redis://redis:6379/0
volumes:
- ./storage:/rails/storage
depends_on:
- redis

redis:
image: redis:6-alpine
volumes:
- redis_data:/data

sidekiq:
build: . # Same as above, using the Dockerfile in the current directory
command: bundle exec sidekiq
env_file:
- .env
environment:
- RAILS_ENV=production
- REDIS_URL=redis://redis:6379/0
volumes:
- ./storage:/rails/storage
depends_on:
- redis

volumes:
redis_data:

0 comments on commit cf1c5fd

Please sign in to comment.