Move confirmation to callbacks
This commit is contained in:
parent
3d6975fae9
commit
df261cc930
3 changed files with 49 additions and 7 deletions
|
@ -7,14 +7,9 @@ class PassportConfirmationsController < ApplicationController
|
|||
|
||||
# POST /passports/:passport_id/passport_confirmations
|
||||
def create
|
||||
@passport_confirmation = PassportConfirmation.new(
|
||||
passport: @passport,
|
||||
user: current_user,
|
||||
)
|
||||
authorize @passport.passport_confirmations.build user: current_user
|
||||
|
||||
authorize @passport_confirmation
|
||||
|
||||
return redirect_to @passport unless @passport_confirmation.save
|
||||
return redirect_to @passport unless @passport.save
|
||||
|
||||
redirect_to @passport
|
||||
end
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Passport < ApplicationRecord
|
||||
REQUIRED_CONFIRMATIONS = 3
|
||||
|
||||
enum sex: %i[male female]
|
||||
|
||||
has_one_attached :image
|
||||
|
@ -25,4 +27,10 @@ class Passport < ApplicationRecord
|
|||
before_validation do
|
||||
self.patronymic = nil if patronymic.blank?
|
||||
end
|
||||
|
||||
before_save do
|
||||
next unless passport_confirmations.length >= REQUIRED_CONFIRMATIONS
|
||||
|
||||
self.confirmed = true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -73,4 +73,43 @@ RSpec.describe 'POST /passports/:passport_id/passport_confirmations' do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when passport confirmations count is almost enough' do
|
||||
let(:current_user) { create :user }
|
||||
|
||||
before do
|
||||
(Passport::REQUIRED_CONFIRMATIONS - 1).times do
|
||||
create :passport_confirmation, passport: passport
|
||||
end
|
||||
|
||||
sign_in current_user
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { make_request }.to \
|
||||
change(PassportConfirmation, :count)
|
||||
.from(Passport::REQUIRED_CONFIRMATIONS - 1)
|
||||
.to(Passport::REQUIRED_CONFIRMATIONS)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { make_request }.to \
|
||||
change { passport.reload.confirmed? }.from(false).to(true)
|
||||
end
|
||||
|
||||
context 'after request' do
|
||||
before { make_request }
|
||||
|
||||
specify do
|
||||
expect(response).to redirect_to passport
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(PassportConfirmation.last).to have_attributes(
|
||||
passport: passport,
|
||||
user: current_user,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue