From 6c1ee4d9658fed5cb08c82ff40a2343c27a79d43 Mon Sep 17 00:00:00 2001 From: Liam <43280985+LiamAshdown@users.noreply.github.com> Date: Wed, 26 Jul 2023 16:37:30 +0100 Subject: [PATCH] fix: Contacts page sort by created_at [CW-2262] (#7584) Fixes the contacts page sort by created_at. fixes: #7577 --- app/controllers/api/v1/accounts/contacts_controller.rb | 1 + app/models/contact.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index ba78cb805..587e082ce 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -4,6 +4,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController sort_on :name, internal_name: :order_on_name, type: :scope, scope_params: [:direction] sort_on :phone_number, type: :string sort_on :last_activity_at, internal_name: :order_on_last_activity_at, type: :scope, scope_params: [:direction] + sort_on :created_at, internal_name: :order_on_created_at, type: :scope, scope_params: [:direction] sort_on :company, internal_name: :order_on_company_name, type: :scope, scope_params: [:direction] sort_on :city, internal_name: :order_on_city, type: :scope, scope_params: [:direction] sort_on :country, internal_name: :order_on_country_name, type: :scope, scope_params: [:direction] diff --git a/app/models/contact.rb b/app/models/contact.rb index 8727ccd78..ef48a7dda 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -62,6 +62,14 @@ class Contact < ApplicationRecord ) ) } + scope :order_on_created_at, lambda { |direction| + order( + Arel::Nodes::SqlLiteral.new( + sanitize_sql_for_order("\"contacts\".\"created_at\" #{direction} + NULLS LAST") + ) + ) + } scope :order_on_company_name, lambda { |direction| order( Arel::Nodes::SqlLiteral.new(