From 13861b799696e491e2e091ffc7c3d06ec8606b98 Mon Sep 17 00:00:00 2001 From: Jon Conley Date: Fri, 24 Mar 2017 09:41:15 -0600 Subject: [PATCH] Issue 95: Interactor for Volunteers#knight --- app/controllers/volunteers_controller.rb | 3 +- app/interactors/toggle_super_admin.rb | 12 ++++++ spec/interactors/toggle_super_admin_spec.rb | 42 +++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 app/interactors/toggle_super_admin.rb create mode 100644 spec/interactors/toggle_super_admin_spec.rb diff --git a/app/controllers/volunteers_controller.rb b/app/controllers/volunteers_controller.rb index 714b62dc..ee3bf0d9 100755 --- a/app/controllers/volunteers_controller.rb +++ b/app/controllers/volunteers_controller.rb @@ -222,8 +222,7 @@ def knight end volunteer = Volunteer.find(params[:volunteer_id]) - volunteer.admin = !volunteer.admin - if volunteer.save + if ToggleSuperAdmin.call(volunteer: volunteer).success? flash[:notice] = "#{volunteer.name} Updated to Admin: #{volunteer.admin}" else flash[:error] = "#{volunteer.errors.full_messages}" diff --git a/app/interactors/toggle_super_admin.rb b/app/interactors/toggle_super_admin.rb new file mode 100644 index 00000000..54734d7e --- /dev/null +++ b/app/interactors/toggle_super_admin.rb @@ -0,0 +1,12 @@ +class ToggleSuperAdmin + include Interactor + + delegate :volunteer, + :fail!, + to: :context + + def call + volunteer.admin = !volunteer.admin + fail! unless volunteer.save + end +end diff --git a/spec/interactors/toggle_super_admin_spec.rb b/spec/interactors/toggle_super_admin_spec.rb new file mode 100644 index 00000000..0a4558ad --- /dev/null +++ b/spec/interactors/toggle_super_admin_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +RSpec.describe ToggleSuperAdmin do + describe '::call' do + let(:volunteer) do + create(:volunteer) + end + + subject do + described_class.call( + volunteer: volunteer + ) + end + + it 'fails if it cannot save the volunteer' do + expect(volunteer).to receive(:save).and_return false + expect(subject.success?).to eq(false) + end + + context 'is admin' do + let(:volunteer) do + create(:volunteer, admin: true) + end + + it 'unsets the admin flag' do + expect(subject.success?).to eq(true) + expect(volunteer.reload.admin).to eq(false) + end + end + + context 'is not admin' do + let(:volunteer) do + create(:volunteer, admin: false) + end + + it 'sets the admin flag' do + expect(subject.success?).to eq(true) + expect(volunteer.reload.admin).to eq(true) + end + end + end +end