Skip to content

Commit

Permalink
Issue 95: Interactor for Volunteers#knight
Browse files Browse the repository at this point in the history
  • Loading branch information
jconley88 authored and rylanb committed Mar 25, 2017
1 parent 68d738a commit 13861b7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/controllers/volunteers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
12 changes: 12 additions & 0 deletions app/interactors/toggle_super_admin.rb
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions spec/interactors/toggle_super_admin_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 13861b7

Please sign in to comment.