-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate organisation options methods
These are essentially doing the same thing, so there's no need to have multiple methods for them I've deleted a test from a controller spec that duplicates a helper test added here I had considered removing the `Organisation` policy scope. The scope gives GOV.UK admins access to all organisations and everyone else access to none, but is only used in places that are accessible only to GOV.UK admins (also based on the organisation policy), so it felt like a bit of a double-authorisation situation. However, George and I felt a bit uneasy about the potential for the `options_for_organisation_select` method being used elsewhere in future, and the authorisation therefore being skipped. As a result, we've retained the double authorisation, and I've included a scope-based context in the helper tests Another thing I spotted is that the `User` model has a `manageable_organisations` method, in turn relying on e.g. `Roles::Admin.manageable_organisations_for` or `Roles::SuperOrganisationAdmin.manageable_organisations_for`. This is used for filtering users by organisation in the users page/table, rather than populating a select field when creating/editing users, but the name makes the remits a little hard to differentiate without close inspection
- Loading branch information
Showing
11 changed files
with
67 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module OrganisationHelper | ||
def options_for_organisation_select(selected_id: nil) | ||
[{ text: Organisation::NONE, value: nil }] + policy_scope(Organisation).not_closed.map do |organisation| | ||
{ text: organisation.name_with_abbreviation, value: organisation.id }.tap do |option| | ||
option[:selected] = true if option[:value] == selected_id | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "test_helper" | ||
|
||
class OrganisationHelperTest < ActionView::TestCase | ||
setup do | ||
@gds = create(:organisation, name: "Government Digital Service", abbreviation: "GDS") | ||
@gas = create(:organisation, name: "Government Analogue Service", abbreviation: "GAS") | ||
@unabbreviated = create(:organisation, name: "unAbbreviaTed") | ||
|
||
stubs(:policy_scope).with(Organisation).returns(Organisation.all) | ||
end | ||
|
||
should "return a select option for each organisation sorted alphabetically, preceded by a 'None' option" do | ||
expected = [ | ||
{ text: "None", value: nil }, | ||
{ text: "Government Analogue Service - GAS", value: @gas.id }, | ||
{ text: "Government Digital Service - GDS", value: @gds.id }, | ||
{ text: "unAbbreviaTed", value: @unabbreviated.id }, | ||
] | ||
|
||
assert_equal expected, options_for_organisation_select | ||
end | ||
|
||
context "when a closed organisation exists" do | ||
setup do | ||
@closed = create(:organisation, name: "Prehistoric Government Service", closed: true) | ||
end | ||
|
||
should "exclude the closed organisation" do | ||
unexpected_closed_option = { text: "Prehistoric Government Service", value: @closed.id } | ||
|
||
assert_not_includes options_for_organisation_select, unexpected_closed_option | ||
end | ||
end | ||
|
||
context "when a selection is provided" do | ||
should "mark the selection as selected" do | ||
selected_option = { text: "Government Analogue Service - GAS", value: @gas.id, selected: true } | ||
|
||
assert_includes options_for_organisation_select(selected_id: @gas.id), selected_option | ||
end | ||
end | ||
|
||
context "when there are no organisations the current user can select" do | ||
setup do | ||
stubs(:policy_scope).with(Organisation).returns(Organisation.where("false")) | ||
end | ||
|
||
should "return only a 'None' option" do | ||
expected = [{ text: "None", value: nil }] | ||
|
||
assert_equal expected, options_for_organisation_select | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters