diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 1aa626a0a..f86b8623c 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,4 +1,6 @@ module UsersHelper + include Pundit::Authorization + def two_step_status(user) user.two_step_status.humanize.capitalize end @@ -66,7 +68,7 @@ def options_for_role_select(selected: nil) end def options_for_organisation_select(selected: nil) - [{ text: "None", value: nil }] + policy_scope(Organisation).map do |organisation| + [{ text: "None", value: nil }] + policy_scope(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 diff --git a/test/helpers/users_helper_test.rb b/test/helpers/users_helper_test.rb index dca168376..fdaee2d2e 100644 --- a/test/helpers/users_helper_test.rb +++ b/test/helpers/users_helper_test.rb @@ -57,18 +57,20 @@ class UsersHelperTest < ActionView::TestCase end context "#options_for_organisation_select" do - should "return organisation options suitable for select component" do - organisation1 = create(:organisation) - organisation2 = create(:organisation) - organisations = [organisation1, organisation2] - stubs(:policy_scope).with(Organisation).returns(organisations) + 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: organisation1.name, value: organisation1.id }, - { text: organisation2.name, value: organisation2.id, selected: true }, + { text: "A Organisation", value: organisation2.id, selected: true }, + { text: "B Organisation", value: organisation1.id }, ] assert_equal expected_options, options end