Commit 9531b596 authored by Dylan Griffith's avatar Dylan Griffith Committed by Sean McGivern

Extract ::Gitlab::Search::SortOptions sort_and_direction

This is to reduce the duplication in handling the two sorting params.
parent 1ecfca53
......@@ -139,14 +139,14 @@ module Elastic
def apply_sort(query_hash, options)
# Due to different uses of sort param we prefer order_by when
# present
case [options[:order_by], options[:sort]]
when %w[created_at asc], [nil, 'created_asc']
case ::Gitlab::Search::SortOptions.sort_and_direction(options[:order_by], options[:sort])
when :created_at_asc
query_hash.merge(sort: {
created_at: {
order: 'asc'
}
})
when %w[created_at desc], [nil, 'created_desc']
when :created_at_desc
query_hash.merge(sort: {
created_at: {
order: 'desc'
......
# frozen_string_literal: true
module Gitlab
module Search
module SortOptions
def sort_and_direction(order_by, sort)
# Due to different uses of sort param in web vs. API requests we prefer
# order_by when present
case [order_by, sort]
when %w[created_at asc], [nil, 'created_asc']
:created_at_asc
when %w[created_at desc], [nil, 'created_desc']
:created_at_desc
else
:unknown
end
end
module_function :sort_and_direction # rubocop: disable Style/AccessModifierDeclarations
end
end
end
......@@ -131,10 +131,10 @@ module Gitlab
def apply_sort(scope)
# Due to different uses of sort param we prefer order_by when
# present
case [order_by, sort]
when %w[created_at asc], [nil, 'created_asc']
case ::Gitlab::Search::SortOptions.sort_and_direction(order_by, sort)
when :created_at_asc
scope.reorder('created_at ASC')
when %w[created_at desc], [nil, 'created_desc']
when :created_at_desc
scope.reorder('created_at DESC')
else
scope.reorder('created_at DESC')
......
# frozen_string_literal: true
require 'fast_spec_helper'
require 'gitlab/search/sort_options'
RSpec.describe ::Gitlab::Search::SortOptions do
describe '.sort_and_direction' do
context 'using order_by and sort' do
it 'returns matched options' do
expect(described_class.sort_and_direction('created_at', 'asc')).to eq(:created_at_asc)
expect(described_class.sort_and_direction('created_at', 'desc')).to eq(:created_at_desc)
end
end
context 'using just sort' do
it 'returns matched options' do
expect(described_class.sort_and_direction(nil, 'created_asc')).to eq(:created_at_asc)
expect(described_class.sort_and_direction(nil, 'created_desc')).to eq(:created_at_desc)
end
end
context 'when unknown option' do
it 'returns unknown' do
expect(described_class.sort_and_direction(nil, 'foo_asc')).to eq(:unknown)
expect(described_class.sort_and_direction(nil, 'bar_desc')).to eq(:unknown)
expect(described_class.sort_and_direction(nil, 'created_bar')).to eq(:unknown)
expect(described_class.sort_and_direction('created_at', 'foo')).to eq(:unknown)
expect(described_class.sort_and_direction('foo', 'desc')).to eq(:unknown)
expect(described_class.sort_and_direction('created_at', nil)).to eq(:unknown)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment