Skip to content

Commit

Permalink
Issue95: Interactor for Volunteers#reactivate
Browse files Browse the repository at this point in the history
  • Loading branch information
jconley88 authored and rylanb committed Mar 29, 2017
1 parent 4db616e commit 5eb6667
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
11 changes: 5 additions & 6 deletions app/controllers/volunteers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,15 @@ def knight

def reactivate
v = Volunteer.send(:with_exclusive_scope){ Volunteer.find(params[:id]) }
if (current_volunteer.admin_region_ids & v.region_ids).length > 0
v.active = true
v.save
inactive
return
else
if (current_volunteer.admin_region_ids & v.region_ids).length <= 0
flash[:error] = "You're not permitted to do that!"
redirect_to(root_path)
return
end
unless ReactivateVolunteer.call(volunteer: v).success?
flash[:error] = 'Update failed :('
end
inactive
end

def admin_only
Expand Down
11 changes: 11 additions & 0 deletions app/interactors/reactivate_volunteer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ReactivateVolunteer
include Interactor

delegate :volunteer,
:fail!,
to: :context

def call
fail! unless volunteer.update_attribute(:active, true)
end
end
36 changes: 36 additions & 0 deletions spec/interactors/reactivate_volunteer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rails_helper'

RSpec.describe ReactivateVolunteer do
describe '::call' do
subject do
described_class.call(
volunteer: volunteer
)
end

let(:volunteer) do
create(:volunteer, active: false)
end

it 'activates the volunteer' do
expect(subject.success?).to eq(true)
expect(volunteer.reload.active).to eq(true)
end

it 'fails if it cannot save the volunteer' do
expect(volunteer).to receive(:update_attribute).and_return false
expect(subject.success?).to eq(false)
end

context 'volunteer is already active' do
let(:volunteer) do
create(:volunteer, active: true)
end

it 'leaves volunteer active' do
expect(subject.success?).to eq(true)
expect(volunteer.reload.active).to eq(true)
end
end
end
end

0 comments on commit 5eb6667

Please sign in to comment.