Browse Source

Sort users by name (#746)

pull/750/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
8505b091b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/user.rb
  2. 5
      spec/models/user_spec.rb

2
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?

5
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

Loading…
Cancel
Save