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 committed Mar 24, 2017
1 parent 1fa7267 commit 7758e2a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/volunteers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def knight
end

volunteer = Volunteer.find(params[:volunteer_id])
volunteer.admin = !volunteer.admin
if volunteer.save

if Knight.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/knight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Knight
include Interactor

delegate :subject,
:fail!,
to: :context

def call
subject.admin = !subject.admin
fail! unless subject.save
end
end
25 changes: 25 additions & 0 deletions spec/interactors/knight_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

RSpec.describe Knight do
describe '::call' do
let(:volunteer) do
create(:volunteer, admin: false)
end

subject do
described_class.call(
subject: volunteer
)
end

it 'sets the admin flad' do
expect(subject.success?).to eq(true)
expect(volunteer.reload.admin).to eq(true)
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
end
end

0 comments on commit 7758e2a

Please sign in to comment.