Commit b33c6384 authored by James Lopez's avatar James Lopez

update code based on feedback

parent 8f2adb80
......@@ -210,10 +210,8 @@ class Admin::UsersController < Admin::ApplicationController
]
end
def update_user
result = Users::UpdateService.new(user).execute do |user|
yield(user)
end
def update_user(&block)
result = Users::UpdateService.new(user).execute(&block)
result[:status] == :success
end
......
......@@ -41,8 +41,10 @@ class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
def create
if current_user.validate_and_consume_otp!(params[:pin_code])
codes = nil
Users::UpdateService.new(current_user, otp_required_for_login: true).execute! do |user|
@codes = user.generate_otp_backup_codes!
codes = user.generate_otp_backup_codes!
end
render 'create'
......
module Users
# Service for building a new user.
class BuildService < BaseService
def initialize(current_user, params = {})
@current_user = current_user
......
module Users
# Service for creating a new user.
class CreateService < BaseService
def initialize(current_user, params = {})
@current_user = current_user
......
module Users
# Service for updating a user.
class UpdateService < BaseService
def initialize(user, params = {})
@user = user
......@@ -7,6 +6,8 @@ module Users
end
def execute(validate: true, &block)
yield(@user) if block_given?
assign_attributes(&block)
if @user.save(validate: validate)
......@@ -27,8 +28,6 @@ module Users
private
def assign_attributes(&block)
yield(@user) if block_given?
@user.assign_attributes(params) if params.any?
end
end
......
......@@ -132,11 +132,13 @@ module API
return { success: false, message: 'Two-factor authentication is not enabled for this user' }
end
codes = nil
::Users::UpdateService.new(user).execute! do |user|
@codes = user.generate_otp_backup_codes!
codes = user.generate_otp_backup_codes!
end
{ success: true, recovery_codes: @codes }
{ success: true, recovery_codes: codes }
end
post "/notify_post_receive" do
......
......@@ -7,7 +7,7 @@ describe Users::UpdateService, services: true do
it 'updates the name' do
result = update_user(user, name: 'New Name')
expect(result).to eq({ status: :success })
expect(result).to eq(status: :success)
expect(user.name).to eq('New Name')
end
......@@ -30,9 +30,9 @@ describe Users::UpdateService, services: true do
expect(user.name).to eq('New Name')
end
it 'returns an error result when record cannot be updated' do
it 'raises an error when record cannot be updated' do
expect do
update_user(user, { email: 'invalid' })
update_user(user, email: 'invalid')
end.to raise_error(ActiveRecord::RecordInvalid)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment