From fa8374e700ec575f7137cfc8ea4904e69f341692 Mon Sep 17 00:00:00 2001 From: BigG1947 <31384049+BigG1947@users.noreply.github.com> Date: Mon, 26 Apr 2021 13:04:48 +0300 Subject: [PATCH] add routing tags count filter (#949) --- app/admin/routing/destinations.rb | 2 +- app/admin/routing/dialpeers.rb | 2 +- .../concerns/routing_tag_ids_scopeable.rb | 2 + app/models/dialpeer.rb | 1 + app/models/routing/destination.rb | 1 + .../acts_as_filter_by_routing_tag_ids.rb | 4 +- .../routing/destinations/filters_spec.rb | 81 +++++++++++++++++++ .../routing/dialpeers/filters_spec.rb | 80 ++++++++++++++++++ 8 files changed, 170 insertions(+), 3 deletions(-) diff --git a/app/admin/routing/destinations.rb b/app/admin/routing/destinations.rb index d504200a0..ac9dc1494 100644 --- a/app/admin/routing/destinations.rb +++ b/app/admin/routing/destinations.rb @@ -75,7 +75,7 @@ filter :acd_limit filter :short_calls_limit - acts_as_filter_by_routing_tag_ids + acts_as_filter_by_routing_tag_ids routing_tag_ids_count: true permit_params :enabled, :prefix, :dst_number_min_length, :dst_number_max_length, :rate_group_id, :next_rate, :connect_fee, :initial_interval, :next_interval, :dp_margin_fixed, diff --git a/app/admin/routing/dialpeers.rb b/app/admin/routing/dialpeers.rb index 9152e5446..4d6d3cb2e 100644 --- a/app/admin/routing/dialpeers.rb +++ b/app/admin/routing/dialpeers.rb @@ -199,7 +199,7 @@ def update filter :external_id filter :exclusive_route, as: :select, collection: [['Yes', true], ['No', false]] - acts_as_filter_by_routing_tag_ids + acts_as_filter_by_routing_tag_ids routing_tag_ids_count: true form do |f| f.semantic_errors *f.object.errors.keys diff --git a/app/models/concerns/routing_tag_ids_scopeable.rb b/app/models/concerns/routing_tag_ids_scopeable.rb index 9ca91e745..54a4e8ece 100644 --- a/app/models/concerns/routing_tag_ids_scopeable.rb +++ b/app/models/concerns/routing_tag_ids_scopeable.rb @@ -15,5 +15,7 @@ module RoutingTagIdsScopeable where("routing_tag_ids = '{}'") # no tags end } + + scope :routing_tag_ids_count_equals, ->(count) { where('array_length(routing_tag_ids, 1) = ?', count) if count.to_i >= 0 } end end diff --git a/app/models/dialpeer.rb b/app/models/dialpeer.rb index 1f6337e0f..306a33a26 100644 --- a/app/models/dialpeer.rb +++ b/app/models/dialpeer.rb @@ -227,6 +227,7 @@ def self.ransackable_scopes(_auth_object = nil) routing_for_contains routing_tag_ids_covers tagged + routing_tag_ids_count_equals ] end end diff --git a/app/models/routing/destination.rb b/app/models/routing/destination.rb index 65a202db3..ba7d07960 100644 --- a/app/models/routing/destination.rb +++ b/app/models/routing/destination.rb @@ -178,6 +178,7 @@ def self.ransackable_scopes(_auth_object = nil) routing_for_contains routing_tag_ids_covers tagged + routing_tag_ids_count_equals ] end end diff --git a/lib/resource_dsl/acts_as_filter_by_routing_tag_ids.rb b/lib/resource_dsl/acts_as_filter_by_routing_tag_ids.rb index f2b206c56..da752349d 100644 --- a/lib/resource_dsl/acts_as_filter_by_routing_tag_ids.rb +++ b/lib/resource_dsl/acts_as_filter_by_routing_tag_ids.rb @@ -2,7 +2,7 @@ module ResourceDSL module ActsAsFilterByRoutingTagIds - def acts_as_filter_by_routing_tag_ids(routing_tag_ids_covers: true) + def acts_as_filter_by_routing_tag_ids(routing_tag_ids_covers: true, routing_tag_ids_count: false) if routing_tag_ids_covers filter :routing_tag_ids_covers, as: :select, collection: -> { Routing::RoutingTag.pluck(:name, :id) }, @@ -14,6 +14,8 @@ def acts_as_filter_by_routing_tag_ids(routing_tag_ids_covers: true) input_html: { class: 'chosen', multiple: true } filter :tagged, as: :select, collection: [['Yes', true], ['No', false]], input_html: { class: 'chosen' } + + filter :routing_tag_ids_count, as: :numeric, filters: [:equals] if routing_tag_ids_count end end end diff --git a/spec/features/routing/destinations/filters_spec.rb b/spec/features/routing/destinations/filters_spec.rb index dba278f7c..2c60f7332 100644 --- a/spec/features/routing/destinations/filters_spec.rb +++ b/spec/features/routing/destinations/filters_spec.rb @@ -114,4 +114,85 @@ end end end + + describe 'filter by routing tags count' do + let!(:other_destinations) { create_list :destination, 2 } + let!(:tags) { create_list :routing_tag, 3 } + let!(:destination_tagged) { create :destination, routing_tag_ids: tags.map(&:id) } + + context 'when user set negative tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: negative_value + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: negative_value) + end + end + + let(:negative_value) { -2 } + + it 'shoul be return all destinations' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: (other_destinations << destination_tagged).size + expect(page).to have_table_cell column: 'Id', text: destination_tagged.id + end + end + + context 'when user set correct tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: tags.size + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: tags.size) + end + end + + it 'shoul be return destinations with correct routing tags count' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: 1 + expect(page).to have_table_cell column: 'Id', text: destination_tagged.id + end + + context 'when set specific routing tag cover and routing tag count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: 1 + fill_in_chosen 'Routing tag ids covers', with: specific_tag.name, multiple: true + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: 1) + expect(page).to have_field_chosen('Routing tag ids covers', with: specific_tag.name, exact: false) + end + end + let!(:specific_tag) { tags.first } + let!(:destination_with_one_tag) { create :destination, routing_tag_ids: [specific_tag.id] } + + it 'should return only destinations with specific tag' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: 1 + within_table_row(id: destination_with_one_tag.id) do + expect(page).to have_table_cell(column: 'Routing Tags', text: specific_tag.name) + end + end + end + + context 'when there are no destinations wtih specified routing tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: tags.size + 1 + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: tags.size + 1) + end + end + + it 'shouldn`t return any rows' do + subject + + expect(page).to_not have_table + expect(page).to have_text('No Destinations found') + end + end + end + end end diff --git a/spec/features/routing/dialpeers/filters_spec.rb b/spec/features/routing/dialpeers/filters_spec.rb index 6ce2ce428..8c4752865 100644 --- a/spec/features/routing/dialpeers/filters_spec.rb +++ b/spec/features/routing/dialpeers/filters_spec.rb @@ -76,4 +76,84 @@ end end end + + describe 'filter by routing tags count' do + let!(:tags) { create_list :routing_tag, 3 } + let!(:dialpeer_tagged) { create :dialpeer, routing_tag_ids: tags.map(&:id) } + + context 'when user set negative tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: negative_value + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: negative_value) + end + end + + let(:negative_value) { -2 } + + it 'shoul be return all dialpeers' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: (other_dialpeers << dialpeer_tagged).size + expect(page).to have_table_cell column: 'Id', text: dialpeer_tagged.id + end + end + + context 'when user set correct tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: tags.size + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: tags.size) + end + end + + it 'shoul be return dialpeers with correct routing tags count' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: 1 + expect(page).to have_table_cell column: 'Id', text: dialpeer_tagged.id + end + + context 'when set specific routing tag cover and routing tag count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: 1 + fill_in_chosen 'Routing tag ids covers', with: specific_tag.name, multiple: true + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: 1) + expect(page).to have_field_chosen('Routing tag ids covers', with: specific_tag.name, exact: false) + end + end + let!(:specific_tag) { tags.first } + let!(:dialpeers_with_one_tag) { create :dialpeer, routing_tag_ids: [specific_tag.id] } + + it 'should return only dialpeers with specific tag' do + subject + + expect(page).to have_table + expect(page).to have_table_row count: 1 + within_table_row(id: dialpeers_with_one_tag.id) do + expect(page).to have_table_cell(column: 'Routing Tags', text: specific_tag.name) + end + end + end + + context 'when there are no dialpeers wtih specified routing tags count' do + let(:filter_records) do + within_filters do + fill_in name: 'q[routing_tag_ids_count_equals]', with: tags.size + 1 + expect(page).to have_field(name: 'q[routing_tag_ids_count_equals]', with: tags.size + 1) + end + end + + it 'shouldn`t return any rows' do + subject + + expect(page).to_not have_table + expect(page).to have_text('No Dialpeers found') + end + end + end + end end