Skip to content

Commit

Permalink
#1677, add the "Any tag" for batch update form
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanov-Anton committed Feb 10, 2025
1 parent 7afbe4c commit cd014c3
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/build_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $(document).on('mass_update_modal_dialog:after_open', function (event, form) {
'name': 'routing_tag_ids[]',
'class': 'chosen',
'id': 'batch_update_routing_tag_ids',
'value': '', // reset default value
'value': null, // reset default value
'multiple': true
});
var tagsCheckbox = $('#mass_update_dialog_routing_tag_ids');
Expand Down
15 changes: 8 additions & 7 deletions app/forms/batch_update_form/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ def form_data

_attributes.each do |name, options|
type = options.fetch(:type, 'string')
data[name] = public_send("form_data_#{type}", options)
data[name] = public_send("form_data_#{type}", name, options)
end

data
end

def form_data_string(_options)
def form_data_string(_attribute_name, _options)
'text'
end

def form_data_date(_options)
def form_data_date(_attribute_name, _options)
'datepicker'
end

def form_data_boolean(_options)
def form_data_boolean(_attribute_name, _options)
[%w[Yes true], %w[No false]]
end

Expand All @@ -121,7 +121,7 @@ def form_data_boolean(_options)
# :primary_key [Symbol] default :id.
# :scope [Proc,Symbol,nil] optional.
# @return [Array<Array(2)>]
def form_data_foreign_key(options)
def form_data_foreign_key(_attribute_name, options = {})
klass = options.fetch(:class_name).constantize
display_name = options.fetch(:display_name, :name)
primary_key = options.fetch(:primary_key, :id)
Expand All @@ -130,10 +130,11 @@ def form_data_foreign_key(options)
scope = klass.all
scope = scope.public_send(custom_scope) if custom_scope.is_a?(Symbol)
scope = custom_scope.call(scope) if custom_scope.is_a?(Proc)
scope.pluck(display_name, primary_key)
result = scope.pluck(display_name, primary_key)
result
end

def form_data_integer_collection(options)
def form_data_integer_collection(_attribute_name, options)
c = options.fetch(:collection)
c
end
Expand Down
4 changes: 3 additions & 1 deletion app/forms/batch_update_form/destination.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class BatchUpdateForm::Destination < BatchUpdateForm::Base
include BatchUpdateForm::RoutingTagMethods

model_class 'Routing::Destination'
attribute :enabled, type: :boolean
attribute :prefix
Expand All @@ -25,7 +27,7 @@ class BatchUpdateForm::Destination < BatchUpdateForm::Base
attribute :asr_limit
attribute :acd_limit
attribute :short_calls_limit
attribute :routing_tag_ids, type: :foreign_key, class_name: 'Routing::RoutingTag'
attribute :routing_tag_ids, type: :foreign_key, class_name: 'Routing::RoutingTag', any_tag: true, scope: ->(scope) { scope.order(:name) }

# presence validations
validates :dst_number_min_length, presence: true, if: :dst_number_min_length_changed?
Expand Down
4 changes: 3 additions & 1 deletion app/forms/batch_update_form/dialpeer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class BatchUpdateForm::Dialpeer < BatchUpdateForm::Base
include BatchUpdateForm::RoutingTagMethods

model_class 'Dialpeer'
attribute :enabled, type: :boolean
attribute :prefix
Expand Down Expand Up @@ -34,7 +36,7 @@ class BatchUpdateForm::Dialpeer < BatchUpdateForm::Base
attribute :src_rewrite_result
attribute :dst_rewrite_rule
attribute :dst_rewrite_result
attribute :routing_tag_ids, type: :foreign_key, class_name: 'Routing::RoutingTag'
attribute :routing_tag_ids, type: :foreign_key, class_name: 'Routing::RoutingTag', any_tag: true, scope: ->(scope) { scope.order(:name) }

# presence
validates :dst_number_min_length, presence: true, if: :dst_number_min_length_changed?
Expand Down
28 changes: 28 additions & 0 deletions app/forms/batch_update_form/routing_tag_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module BatchUpdateForm::RoutingTagMethods
extend ActiveSupport::Concern

class_methods do
def form_data_foreign_key(attribute_name, options = {})
return super(attribute_name, options) unless attribute_name == :routing_tag_ids

display_name = options.fetch(:display_name, :name)
primary_key = options.fetch(:primary_key, :id)

scope = Routing::RoutingTag.order(:name)
scope = scope.pluck(display_name, primary_key)
scope = add_any_tag_option(scope, options)
scope
end

def add_any_tag_option(scope, options)
if options[:any_tag] == true
scope.prepend([[Routing::RoutingTag::ANY_TAG]])
elsif options[:any_tag] != false
raise ArgumentError, 'options[:any_tag] must be a boolean'
end
scope
end
end
end
61 changes: 54 additions & 7 deletions spec/features/routing/destinations/batch_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
let!(:profit_control_mode_id) { Routing::RateProfitControlMode::MODE_PER_CALL }
let!(:routing_tags) { create_list(:routing_tag, 5) }

before do
visit destinations_path
click_button 'Update batch'
expect(page).to have_selector('.ui-dialog')
end

subject do
fill_batch_form
click_button 'OK'
Expand Down Expand Up @@ -46,7 +40,7 @@
asr_limit: '0.9',
acd_limit: '1',
short_calls_limit: '4',
routing_tag_ids: routing_tags.map { |tag| tag.id.to_s }
routing_tag_ids: routing_tags.sort_by(&:name).map { |tag| tag.id.to_s }
}
end

Expand Down Expand Up @@ -178,6 +172,12 @@
end

context 'should check validates' do
before do
visit destinations_path
click_button 'Update batch'
expect(page).to have_selector('.ui-dialog')
end

context 'when change :dp_margin_percent' do
let(:assign_params) { { dp_margin_percent: '0' } }

Expand Down Expand Up @@ -207,4 +207,51 @@
end
end
end

context 'when user wants to change routing_tag_ids to "NOT TAGGED"' do
subject { click_button :OK }

let(:_destinations) { nil }
let(:assign_params) { {} }

before do
visit destinations_path
click_button 'Update batch'
page.scroll_to find_button('OK')
check :Routing_tag_ids
end

it 'should create Job to update routing_tag_ids field of desctination to default value: []' do
expect do
subject
expect(page).to have_selector '.flash', text: success_message
end.to enqueue_job(AsyncBatchUpdateJob).with('Routing::Destination', be_present, { routing_tag_ids: '' }, be_present)

# ensure that destination model converts "" to [] because the system consider [] as Routing::RoutingTag::NOT_TAGGED
expect(Routing::Destination.new(routing_tag_ids: '')).to have_attributes(routing_tag_ids: [])
end
end

context 'when user wants to change routing_tag_ids to "ANY TAG"' do
let(:_destinations) { nil }
let(:assign_params) { {} }

before do
visit destinations_path
click_button 'Update batch'
page.scroll_to find_button('OK')
check :Routing_tag_ids
end

it 'should created Job to update routing_tag_ids to any tag: [nil]' do
fill_in_chosen 'routing_tag_ids[]', with: Routing::RoutingTag::ANY_TAG, multiple: true
expect do
subject
expect(page).to have_selector '.flash', text: success_message
end.to enqueue_job(AsyncBatchUpdateJob).with('Routing::Destination', be_present, { routing_tag_ids: [''] }, be_present)

# ensure that destination model converts [""] to [nil] because the system consider [nil] as Routing::RoutingTag::ANY_TAG
expect(Routing::Destination.new(routing_tag_ids: [''])).to have_attributes(routing_tag_ids: [nil])
end
end
end
26 changes: 25 additions & 1 deletion spec/features/routing/dialpeers/batch_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
src_rewrite_result: '12',
dst_rewrite_rule: '12',
dst_rewrite_result: '12',
routing_tag_ids: routing_tags.map { |tag| tag.id.to_s }
routing_tag_ids: routing_tags.sort_by(&:name).map { |tag| tag.id.to_s }
}
end
let(:fill_batch_form) do
Expand Down Expand Up @@ -266,4 +266,28 @@
end
end
end

context 'when user wants to change routing_tag_ids to "ANY TAG"' do
subject { click_button :OK }

let(:_dialpeers) { nil }
let(:routing_group) { nil }
let!(:routeset_discriminator) { nil }
let(:assign_params) { {} }

before do
check :Routing_tag_ids
fill_in_chosen 'routing_tag_ids[]', with: Routing::RoutingTag::ANY_TAG, multiple: true
end

it 'should created Job to update routing_tag_ids to any tag: [nil]' do
expect do
subject
expect(page).to have_selector '.flash', text: success_message
end.to enqueue_job(AsyncBatchUpdateJob).with('Dialpeer', be_present, { routing_tag_ids: [''] }, be_present)

# ensure that dialpeer model converts "" to [] because the system considers [""] as Routing::RoutingTag::ANY_TAG
expect(Dialpeer.new(routing_tag_ids: [''])).to have_attributes(routing_tag_ids: [nil])
end
end
end

0 comments on commit cd014c3

Please sign in to comment.