Skip to content

Commit

Permalink
Merge pull request #2417 from alphagov/improve-organisation-select-on…
Browse files Browse the repository at this point in the history
…-role-organisations-page

Improve organisation select on role organisations page
  • Loading branch information
chrislo authored Oct 10, 2023
2 parents 5b36a0c + 924e00c commit 42b2e27
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/helpers/role_organisations_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion app/views/account/role_organisations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 43 additions & 0 deletions test/helpers/role_organisations_helper_test.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/models/organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 42b2e27

Please sign in to comment.