From 8505b091b9c03d245ea75adf2619650334c4eab6 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Mon, 18 Jul 2022 07:16:10 +0100 Subject: [PATCH] Sort users by name (#746) --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 3a524ecc2..8b9d3f52f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,7 +35,7 @@ class User < ApplicationRecord scope :search_by_email, ->(email) { where("email ILIKE ?", "%#{email}%") } scope :filter_by_active, -> { where(active: true) } scope :search_by, ->(param) { search_by_name(param).or(search_by_email(param)) } - scope :sorted_by_organisation_and_role, -> { joins(:organisation).order("organisations.name", role: :desc) } + scope :sorted_by_organisation_and_role, -> { joins(:organisation).order("organisations.name", role: :desc, name: :asc) } def case_logs if support? diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bbb1c7a15..922b4527e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -196,6 +196,7 @@ RSpec.describe User, type: :model do let!(:user_3) { FactoryBot.create(:user, name: "Tom Smith", email: "tom@example.com", organisation: organisation_1, role: "data_provider") } let!(:user_2) { FactoryBot.create(:user, name: "Jenny Ford", email: "jenny@smith.com", organisation: organisation_1, role: "data_coordinator") } let!(:user_4) { FactoryBot.create(:user, name: "Greg Thomas", email: "greg@org_2.com", organisation: organisation_2, role: "data_coordinator") } + let!(:user_5) { FactoryBot.create(:user, name: "Adam Thomas", email: "adam@org_2.com", organisation: organisation_2, role: "data_coordinator") } context "when searching by name" do it "returns case insensitive matching records" do @@ -219,8 +220,8 @@ RSpec.describe User, type: :model do end context "when using sorted by organisation and role scope" do - it "returns all users sorted by organisation name and then by role" do - expect(described_class.sorted_by_organisation_and_role.to_a).to eq([user_1, user_2, user_3, user_4]) + it "returns all users sorted by organisation name, then by role, then alphabetically by name" do + expect(described_class.sorted_by_organisation_and_role.to_a).to eq([user_1, user_2, user_3, user_5, user_4]) end end end