Skip to content

Commit

Permalink
fix bug with sorting for destination resource
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlutsenko authored and gigorok committed May 28, 2024
1 parent 776550e commit a464d60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
7 changes: 4 additions & 3 deletions app/resources/api/rest/admin/routing/destination_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,18 @@ def self.resource_for(type)
# related to issue https://github.com/cerebris/jsonapi-resources/issues/1409
def self.sort_records(records, order_options, context = {})
local_records = records
custom_sort_fields = %w[country.name network.name]

order_options.each_pair do |field, direction|
case field.to_s
when 'country.name'
local_records = records.left_joins(:country).order("countries.name #{direction}")
local_records = local_records.left_joins(:country).order("countries.name #{direction}")
order_options.delete('country.name')
when 'network.name'
local_records = records.left_joins(:network).order("networks.name #{direction}")
local_records = local_records.left_joins(:network).order("networks.name #{direction}")
order_options.delete('network.name')
else
local_records = apply_sort(local_records, order_options, context)
local_records = apply_sort(local_records, order_options.except(*custom_sort_fields), context)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@
end
end

context 'when sort by prefix in ASC order and then by country name' do
let(:index_params) { { sort: 'prefix,country.name' } }

it 'returns ordered records' do
subject

expect(response_body[:errors]).to be_nil
expect(response_body[:data].pluck(:id)).to eq [first_destination_afghanistan.id.to_s, third_destination_ukraine.id.to_s, second_destination_ukraine.id.to_s]
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq %w[111 555 999]
end
end

context 'when sort by country name in ASC order and then by prefix in DESC order' do
let(:index_params) { { sort: 'country.name,-prefix' } }

Expand All @@ -96,6 +108,18 @@
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq %w[111 999 555]
end
end

context 'when sort by prefix in DESC order and then by country name' do
let(:index_params) { { sort: '-prefix,country.name' } }

it 'returns ordered records' do
subject

expect(response_body[:errors]).to be_nil
expect(response_body[:data].pluck(:id)).to eq [second_destination_ukraine.id.to_s, third_destination_ukraine.id.to_s, first_destination_afghanistan.id.to_s]
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq %w[999 555 111]
end
end
end

context 'by country, network and prefix' do
Expand All @@ -117,6 +141,15 @@
record
end

let!(:fifth_destination_ukraine) do
ukraine = System::Country.find_by!(name: 'Ukraine')
network_abc_ua = FactoryBot.create(:network, name: 'ABCUkraineNet', network_type: net_type)
network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network: network_abc_ua)
record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '333')
record.update!(network_prefix_id: network_prefix.id)
record
end

context 'when sort by country name and then by prefix in ASC order' do
let(:index_params) { { sort: 'country.name,network.name,prefix' } }

Expand All @@ -126,11 +159,12 @@
expect(response_body[:errors]).to be_nil
expect(response_body[:data].pluck(:id)).to eq([
first_destination_afghanistan.id.to_s,
fifth_destination_ukraine.id.to_s,
second_destination_ukraine.id.to_s,
fourth_destination_ukraine.id.to_s,
third_destination_ukraine.id.to_s
])
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 999 444 777])
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 333 999 444 777])
end
end

Expand All @@ -143,11 +177,12 @@
expect(response_body[:errors]).to be_nil
expect(response_body[:data].pluck(:id)).to eq([
first_destination_afghanistan.id.to_s,
fifth_destination_ukraine.id.to_s,
second_destination_ukraine.id.to_s,
third_destination_ukraine.id.to_s,
fourth_destination_ukraine.id.to_s
])
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 999 777 444])
expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 333 999 777 444])
end
end
end
Expand Down

0 comments on commit a464d60

Please sign in to comment.