Skip to content

Commit

Permalink
Consolidate organisation options methods
Browse files Browse the repository at this point in the history
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 than
duplicates a helper test added here
  • Loading branch information
yndajas committed Oct 1, 2024
1 parent f4a72e7 commit 28b1e03
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 94 deletions.
9 changes: 9 additions & 0 deletions app/helpers/organisation_helper.rb
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: "None", value: nil }] + 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
11 changes: 0 additions & 11 deletions app/helpers/role_organisations_helper.rb

This file was deleted.

8 changes: 0 additions & 8 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ def options_for_role_select(selected: nil)
end
end

def options_for_organisation_select(selected: nil)
[{ text: "None", value: nil }] + Organisation.not_closed.order(:name).map do |organisation|
{ text: organisation.name_with_abbreviation, value: organisation.id }.tap do |option|
option[:selected] = true if option[:value] == selected
end
end
end

def options_for_permission_option_select(application:, user: nil)
application.sorted_supported_permissions_grantable_from_ui.map do |permission|
{
Expand Down
2 changes: 1 addition & 1 deletion app/views/account/organisations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_your_organisation_select(current_user)
options: options_for_organisation_select(selected_id: current_user.organisation_id)
} %>

<div class="govuk-button-group">
Expand Down
2 changes: 1 addition & 1 deletion app/views/batch_invitations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
id: "batch_invitation_organisation_id",
name: "batch_invitation[organisation_id]",
label: "Organisation",
options: Organisation.not_closed.order(:name).map { |organisation| { text: organisation.name_with_abbreviation, value: organisation.id } }
options: options_for_organisation_select
} %>

<div class="govuk-button-group">
Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/invitations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_organisation_select(selected: f.object.organisation_id)
options: options_for_organisation_select(selected_id: f.object.organisation_id)
} %>

<div class="govuk-button-group">
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/organisations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_organisation_select(selected: @user.organisation_id),
options: options_for_organisation_select(selected_id: @user.organisation_id),
error_message: @user.errors[:organisation_id].any? ? @user.errors.full_messages_for(:organisation_id).to_sentence : nil
} %>

Expand Down
8 changes: 0 additions & 8 deletions test/controllers/batch_invitations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ def users_csv(filename = "users.csv")

assert_select "#batch_invitation_organisation_id option", organisation.name
end

should "exclude closed organisations from select" do
create(:organisation)
create(:organisation, closed: true)
get :new

assert_select "#batch_invitation_organisation_id option", 1
end
end

context "POST create" do
Expand Down
40 changes: 40 additions & 0 deletions test/helpers/organisation_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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")
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
end
43 changes: 0 additions & 43 deletions test/helpers/role_organisations_helper_test.rb

This file was deleted.

20 changes: 0 additions & 20 deletions test/helpers/users_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,6 @@ class UsersHelperTest < ActionView::TestCase
end
end

context "#options_for_organisation_select" do
should "return organisation options suitable for select component, sorted alphabetically and exluding closed organisations" do
user = create(:admin_user)
stubs(:current_user).returns(user)

organisation1 = create(:organisation, name: "B Organisation")
organisation2 = create(:organisation, name: "A Organisation")
create(:organisation, name: "Closed Organisation", closed: true)

options = options_for_organisation_select(selected: organisation2.id)

expected_options = [
{ text: "None", value: nil },
{ text: "A Organisation", value: organisation2.id, selected: true },
{ text: "B Organisation", value: organisation1.id },
]
assert_equal expected_options, options
end
end

context "#options_for_permission_option_select" do
should "return permission options suitable for option-select component" do
application = create(:application)
Expand Down

0 comments on commit 28b1e03

Please sign in to comment.