From 366c0064dc84c3782b013570aae40bc123943e3c Mon Sep 17 00:00:00 2001 From: Andrey Bakanovsky Date: Fri, 25 Aug 2023 18:53:44 +0300 Subject: [PATCH 1/3] feat(account invitation): accomplish sign up before accepting the invitation --- .../accept_account_invitations_controller.rb | 13 ++++++++++--- .../users/registrations_controller.rb | 17 +++++++++++++++++ app/controllers/users/sessions_controller.rb | 4 ++-- .../accept_account_invitations_spec.rb | 11 ++++++++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/controllers/accept_account_invitations_controller.rb b/app/controllers/accept_account_invitations_controller.rb index 5c5a717b..75809dd9 100644 --- a/app/controllers/accept_account_invitations_controller.rb +++ b/app/controllers/accept_account_invitations_controller.rb @@ -4,8 +4,15 @@ class AcceptAccountInvitationsController < ApplicationController def show return if user_signed_in? - session[:after_sign_in_url] = request.fullpath - redirect_to new_user_session_url(email: AccountInvitation.find_by!(token: ps.fetch(:token)).email) + session[:after_authentication_url] = request.fullpath + + invitee_email = AccountInvitation.find_by!(token: ps[:token]).email + user = User.find_by(email: invitee_email) + if user.present? + redirect_to new_user_session_url(email: invitee_email) + else + redirect_to new_user_registration_url(email: invitee_email) + end end def update @@ -16,6 +23,6 @@ def update private helper_method memoize def received_invitation - AccountInvitation.unaccepted.for(current_user).find_by!(token: ps.fetch(:token)) + AccountInvitation.unaccepted.for(current_user).find_by!(token: ps[:token]) end end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 43ad96a7..66fa8689 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,4 +1,12 @@ class Users::RegistrationsController < Devise::RegistrationsController + def new + if params.key?(:email) + self.resource = User.new(email: params[:email]) + else + super + end + end + def create super do |user| user.create_account(name: user.email, email: user.email) if user.persisted? @@ -10,4 +18,13 @@ def create def devise_mapping @devise_mapping ||= Devise.mappings[:user] end + + private + + def after_sign_up_path_for(resource) + after_authentication_url = session.delete(:after_authentication_url) + return after_authentication_url if after_authentication_url.present? + + super + end end \ No newline at end of file diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 5a1a5de3..506f2d1d 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -13,8 +13,8 @@ def new private def after_sign_in_path_for(_resource) - after_sign_in_url = session.delete(:after_sign_in_url) - return after_sign_in_url if after_sign_in_url.present? + after_authentication_url = session.delete(:after_authentication_url) + return after_authentication_url if after_authentication_url.present? super end diff --git a/spec/controllers/accept_account_invitations_spec.rb b/spec/controllers/accept_account_invitations_spec.rb index f1777968..5201dbcb 100644 --- a/spec/controllers/accept_account_invitations_spec.rb +++ b/spec/controllers/accept_account_invitations_spec.rb @@ -6,7 +6,7 @@ describe '#show' do let(:account) { create(:account, :parent) } let(:user) { create(:user, account: account) } - let(:second_user) { create(:user, account: account) } + let(:second_user) { create(:user) } let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) } @@ -21,6 +21,15 @@ end end + context 'when the user not signed up' do + let(:account_invitation) { create(:account_invitation, user: user, account: account) } + + it 'redirects to registration url with email' do + subject + expect(response).to redirect_to(new_user_registration_url(email: account_invitation.email)) + end + end + context 'when the user signed in' do before { sign_in second_user } From 26cc009d23ca35ceb794eceae4b8b9ff979e7bb8 Mon Sep 17 00:00:00 2001 From: Andrey Bakanovsky Date: Fri, 25 Aug 2023 23:06:37 +0300 Subject: [PATCH 2/3] feat(account invitation): change based on the comments --- .../accept_account_invitations_controller.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/controllers/accept_account_invitations_controller.rb b/app/controllers/accept_account_invitations_controller.rb index 75809dd9..06f2f963 100644 --- a/app/controllers/accept_account_invitations_controller.rb +++ b/app/controllers/accept_account_invitations_controller.rb @@ -6,13 +6,11 @@ def show session[:after_authentication_url] = request.fullpath - invitee_email = AccountInvitation.find_by!(token: ps[:token]).email + invitee_email = AccountInvitation.find_by!(token: ps.fetch(:token)).email user = User.find_by(email: invitee_email) - if user.present? - redirect_to new_user_session_url(email: invitee_email) - else - redirect_to new_user_registration_url(email: invitee_email) - end + redirect_url = user ? new_user_session_url(email: invitee_email) : new_user_registration_url(email: invitee_email) + + redirect_to redirect_url end def update @@ -23,6 +21,6 @@ def update private helper_method memoize def received_invitation - AccountInvitation.unaccepted.for(current_user).find_by!(token: ps[:token]) + AccountInvitation.unaccepted.for(current_user).find_by!(token: ps.fetch(:token)) end end From 6a781c4e8ec30884d04442c7b11cead054f988e5 Mon Sep 17 00:00:00 2001 From: Andrey Bakanovsky Date: Sat, 26 Aug 2023 23:13:29 +0300 Subject: [PATCH 3/3] feat(account invitation): change var name --- app/controllers/accept_account_invitations_controller.rb | 2 +- app/controllers/users/registrations_controller.rb | 4 ++-- app/controllers/users/sessions_controller.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/accept_account_invitations_controller.rb b/app/controllers/accept_account_invitations_controller.rb index 06f2f963..6ba0d020 100644 --- a/app/controllers/accept_account_invitations_controller.rb +++ b/app/controllers/accept_account_invitations_controller.rb @@ -4,7 +4,7 @@ class AcceptAccountInvitationsController < ApplicationController def show return if user_signed_in? - session[:after_authentication_url] = request.fullpath + session[:after_sign_in_url] = request.fullpath invitee_email = AccountInvitation.find_by!(token: ps.fetch(:token)).email user = User.find_by(email: invitee_email) diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 66fa8689..78f3e84d 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -22,8 +22,8 @@ def devise_mapping private def after_sign_up_path_for(resource) - after_authentication_url = session.delete(:after_authentication_url) - return after_authentication_url if after_authentication_url.present? + after_sign_in_url = session.delete(:after_sign_in_url) + return after_sign_in_url if after_sign_in_url.present? super end diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 506f2d1d..5a1a5de3 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -13,8 +13,8 @@ def new private def after_sign_in_path_for(_resource) - after_authentication_url = session.delete(:after_authentication_url) - return after_authentication_url if after_authentication_url.present? + after_sign_in_url = session.delete(:after_sign_in_url) + return after_sign_in_url if after_sign_in_url.present? super end