Skip to content

Commit

Permalink
add new network.name sorting to destination resource
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlutsenko authored and gigorok committed Apr 30, 2024
1 parent 5e1e0d9 commit 80b5e28
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/resources/api/rest/admin/routing/destination_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def self.creatable_fields(context)
end

def self.sortable_fields(_context = nil)
super + [:'country.name']
super + %i[country.name network.name]
end

def self.resource_for(type)
Expand All @@ -105,6 +105,9 @@ def self.sort_records(records, order_options, context = {})
when 'country.name'
local_records = 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}")
order_options.delete('network.name')
else
local_records = apply_sort(local_records, order_options, context)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@
end

describe 'sort' do
context 'by country & prefix' do
context 'by country, network & prefix' do
let(:destinations) { nil }
let!(:net_type) { FactoryBot.create(:network_type) }
let!(:network) { FactoryBot.create(:network) }

let!(:first_destination_afghanistan) do
afghanistan = System::Country.find_by!(name: 'Afghanistan')
network_prefix = FactoryBot.create(:network_prefix, country: afghanistan)
network = FactoryBot.create(:network, name: 'AfghanistanNet', network_type: net_type)
network_prefix = FactoryBot.create(:network_prefix, country: afghanistan, network:)
record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '111')
record.update!(network_prefix_id: network_prefix.id)
record
end

let!(:second_destination_ukraine) do
ukraine = System::Country.find_by!(name: 'Ukraine')
network_prefix = FactoryBot.create(:network_prefix, country: ukraine)
network = FactoryBot.create(:network, name: 'UkraineNetABC', network_type: net_type)
network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network:)
record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '999')
record.update!(network_prefix_id: network_prefix.id)
record
Expand Down Expand Up @@ -96,6 +97,60 @@
end
end
end

context 'by country, network and prefix' do
let!(:network_ua_cba) { FactoryBot.create(:network, name: 'UkraineNetCBA', network_type: net_type) }

let!(:third_destination_ukraine) do
ukraine = System::Country.find_by!(name: 'Ukraine')
network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network: network_ua_cba)
record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '777')
record.update!(network_prefix_id: network_prefix.id)
record
end

let!(:fourth_destination_ukraine) do
ukraine = System::Country.find_by!(name: 'Ukraine')
network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network: network_ua_cba)
record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '444')
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' } }

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,
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])
end
end

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

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,
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])
end
end
end
end
end
end
Expand Down

0 comments on commit 80b5e28

Please sign in to comment.