diff --git a/app/helpers/role_organisations_helper.rb b/app/helpers/role_organisations_helper.rb new file mode 100644 index 000000000..9e875914d --- /dev/null +++ b/app/helpers/role_organisations_helper.rb @@ -0,0 +1,11 @@ +module RoleOrganisationsHelper + def options_for_your_organisation_select(current_user) + organisations = Pundit.policy_scope(current_user, Organisation).not_closed.order(:name) + + organisations.map do |organisation| + { text: organisation.name_with_abbreviation, + value: organisation.id, + selected: current_user.organisation == organisation } + end + end +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index b5d3e155d..0d56b6177 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -12,6 +12,8 @@ class Organisation < ApplicationRecord before_save :strip_whitespace_from_name + scope :not_closed, -> { where(closed: false) } + def name_with_abbreviation return_value = if abbreviation.present? && abbreviation != name "#{name} – #{abbreviation}" diff --git a/app/views/account/role_organisations/show.html.erb b/app/views/account/role_organisations/show.html.erb index 1edede628..e2d3db4c0 100644 --- a/app/views/account/role_organisations/show.html.erb +++ b/app/views/account/role_organisations/show.html.erb @@ -60,7 +60,7 @@ id: "user_organisation_id", name: "user[organisation_id]", label: "Organisation", - options: policy_scope(Organisation).map { |organisation| { text: organisation.name_with_abbreviation, value: organisation.id, selected: current_user.organisation == organisation } } + options: options_for_your_organisation_select(current_user) } %> <%= render "govuk_publishing_components/components/button", { text: "Change organisation" diff --git a/test/helpers/role_organisations_helper_test.rb b/test/helpers/role_organisations_helper_test.rb new file mode 100644 index 000000000..3a95bfe9a --- /dev/null +++ b/test/helpers/role_organisations_helper_test.rb @@ -0,0 +1,43 @@ +require "test_helper" + +class RoleOrganisationsHelperTest < ActionView::TestCase + context "#options_for_your_organisation_select" do + setup do + @user_organisation = create(:organisation, name: "User Organisation", abbreviation: "UO") + @other_organisation = create(:organisation, name: "Other Organisation") + @closed_organisation = create(:organisation, name: "Closed Organisation", closed: true) + @user = create(:admin_user, organisation: @user_organisation) + end + + should "return options suitable for select component with users organisation selected" do + options = options_for_your_organisation_select(@user) + + expected_options = [ + { + text: "Other Organisation", + value: @other_organisation.id, + selected: false, + }, + { + text: "User Organisation – UO", + value: @user_organisation.id, + selected: true, + }, + ] + + assert_equal expected_options, options + end + + should "sort by organisation name alphabetically" do + options = options_for_your_organisation_select(@user) + + assert_equal ["Other Organisation", "User Organisation – UO"], (options.map { |o| o[:text] }) + end + + should "not include closed organisations" do + closed_organisation_option = options_for_your_organisation_select(@user).detect { |o| o[:value] == @closed_organisation.id } + + assert_not closed_organisation_option + end + end +end diff --git a/test/models/organisation_test.rb b/test/models/organisation_test.rb index 25ec7ae2d..4d96fdb53 100644 --- a/test/models/organisation_test.rb +++ b/test/models/organisation_test.rb @@ -23,6 +23,12 @@ def setup assert_equal "An organisation", organisation.name end + test "#not_closed" do + create(:organisation, closed: true) + + assert_equal [@organisation], Organisation.not_closed.to_a + end + context "displaying name with abbreviation" do should "use abbreviation when it is not the same as name" do organisation = build(:organisation, name: "An Organisation", abbreviation: "ABBR")