Skip to content

Commit

Permalink
fix(editions) prevent calling method on nil
Browse files Browse the repository at this point in the history
- when a request is sent without an assigned_to param to update an edition, an error was being thrown as the update_assignment function expected a value to always be passed
  • Loading branch information
FelixMarcusMillne committed Aug 21, 2023
1 parent a0d3b7f commit 4ed960b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
11 changes: 4 additions & 7 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,12 @@ def new_assignee
def update_assignment(edition, assignee)
return if edition.assigned_to == assignee

unless assignee.has_editor_permissions?(resource)
flash[:danger] = "Chosen assignee does not have correct editor permissions."
return
end

if assignee
if !assignee
current_user.unassign(edition)
elsif assignee.has_editor_permissions?(resource)
current_user.assign(edition, assignee)
else
current_user.unassign(edition)
flash[:danger] = "Chosen assignee does not have correct editor permissions."
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/functional/editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ class EditionsControllerTest < ActionController::TestCase
assert_equal bob, @guide.assigned_to
end

should "clear assignment if no assignment is passed" do
post :update,
params: {
id: @guide.id,
edition: {},
}

@guide.reload
assert_nil @guide.assigned_to
end

should "not create a new action if the assignment is unchanged" do
bob = FactoryBot.create(:user, :govuk_editor)
@user.assign(@guide, bob)
Expand Down

0 comments on commit 4ed960b

Please sign in to comment.