Skip to content

Commit

Permalink
refactor: merge cancellations and registrations controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 13, 2024
1 parent 7e63a26 commit 1d3c4ad
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 16 deletions.
6 changes: 0 additions & 6 deletions app/controllers/users/cancellations_controller.rb

This file was deleted.

9 changes: 8 additions & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class Users::RegistrationsController < Devise::RegistrationsController
skip_before_action :require_no_authentication, only: [:cancel]
before_action :authenticate_user!, only: [:cancel]

def cancel
@conflicting_account_users = current_user.account_users.where(owner: true)
end

def destroy
# Don't let user cancel their accounts if they are an owner of a team.
if current_user.account_users.find_by(owner: true)
flash[:alert] = I18n.t("users.registrations.destroy.owner_of_team")
return redirect_to delete_user_registration_path
return redirect_to cancel_user_registration_path
end

super # Inherits from Devise::RegistrationsController
Expand Down
2 changes: 1 addition & 1 deletion app/views/partials/navigations/_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
label: t('.items.delete_user.label'),
description: t('.items.delete_user.description'),
icon: "fa fa-person-circle-minus",
path: delete_user_registration_path
path: cancel_user_registration_path
)) %>
File renamed without changes.
3 changes: 1 addition & 2 deletions config/locales/kiqr.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ en:
submit_button: "Save changes"
destroy:
owner_of_team: "Can't delete account when owner of a team."
cancellations:
show:
cancel:
title: "Delete account"
description: "Delete your user account and all of your data."
box_title: "Delete your user account"
Expand Down
2 changes: 0 additions & 2 deletions config/routes/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
post "two-factor/verify" => "two_factor#verify", :as => :verify_two_factor
delete "two-factor/destroy" => "two_factor#destroy", :as => :destroy_two_factor

get "delete" => "cancellations#show", :as => :delete_user_registration

resource :preferences, only: %i[edit update], as: :user_preferences
end

Expand Down
4 changes: 4 additions & 0 deletions gems/kiqr/lib/kiqr/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def devise_routes(options)
registrations: options[:controllers][:registrations],
sessions: options[:controllers][:sessions]
}

devise_scope :user do
get "users/cancel-account", controller: options[:controllers][:registrations], action: :cancel, as: :delete_user_registration
end
end

# => Teamable routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Users::RegistrationControllerTest < ActionDispatch::IntegrationTest
test "can view cancellation page" do
sign_in create(:user)
get delete_user_registration_path
get cancel_user_registration_path
assert_response :success
end

Expand All @@ -24,7 +24,7 @@ class Users::RegistrationControllerTest < ActionDispatch::IntegrationTest
sign_in user
delete user_registration_path

assert_redirected_to delete_user_registration_path
assert_redirected_to cancel_user_registration_path
assert User.find_by(id: user.id)
end
end
4 changes: 2 additions & 2 deletions test/system/users/cancellation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class EditAccountsTest < ApplicationSystemTestCase
test "can delete a fresh user" do
user = create(:user)
sign_in(user)
visit delete_user_registration_path
visit cancel_user_registration_path
accept_confirm { click_on "commit" }

assert_text I18n.t("devise.registrations.destroyed")
Expand All @@ -17,7 +17,7 @@ class EditAccountsTest < ApplicationSystemTestCase
team_account.account_users << AccountUser.create(user: user, owner: true)

sign_in(user)
visit delete_user_registration_path
visit cancel_user_registration_path

assert_no_selector("button[name='commit']")
end
Expand Down

0 comments on commit 1d3c4ad

Please sign in to comment.