Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imports old associations #16471

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/services/dialog_import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def import_from_dialogs(dialogs)
)
)
fields = new_or_existing_dialog.dialog_fields
associations_to_be_created.each do |association|
(associations_to_be_created + build_old_association_list(fields).flatten).reject(&:blank?).each do |association|
association.values.each do |values|
values.each do |responder|
next if fields.select { |field| field.name == responder }.empty?
Expand All @@ -147,6 +147,35 @@ def dialog_with_label?(label)
Dialog.where("label" => label).exists?
end

def build_old_association_list(fields)
trigger_fields = absolute_position(fields.select(&:trigger_auto_refresh))
responder_fields = absolute_position(fields.select(&:auto_refresh))
trigger_fields.enum_for(:each_with_index).collect do |tf, index|
specific_responders = if trigger_fields[index + 1]
responder_fields.select { |rf| responder_range(tf, trigger_fields[index + 1]).cover?(rf[:position]) }.pluck(:name)
else
responder_fields.select { |rf| responder_range(tf, nil).cover?(rf[:position]) }.pluck(:name)
end
{tf[:name] => specific_responders}
end
end

def absolute_position(dialog_fields)
dialog_fields.collect do |f|
field_position = f.position
dialog_group_position = f.dialog_group.position
dialog_tab_position = f.dialog_group.dialog_tab.position
index = field_position + dialog_group_position * 1000 + dialog_tab_position * 100_000
{:name => f.name, :position => index}
end
end

def responder_range(trigger_min, trigger_max)
min = trigger_min[:position] + 1
max = trigger_max.present? ? trigger_max[:position] - 1 : 100_000_000
(min..max)
end

def destroy_queued_deletion(import_file_upload_id)
MiqQueue.unqueue(
:class_name => "ImportFileUpload",
Expand Down
15 changes: 11 additions & 4 deletions spec/lib/services/dialog_import_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
end

let(:dialog_groups) do
[{"label" => "New Box", "dialog_fields" => dialog_fields}]
[{"label" => "New Box", "dialog_fields" => dialog_fields, :position => 1}]
end

let(:dialog_tabs) do
[{"label" => "New Tab", "dialog_groups" => dialog_groups}]
[{"label" => "New Tab", "dialog_groups" => dialog_groups, :position => 4}]
end

let(:dialogs) do
Expand All @@ -30,7 +30,9 @@
before do
built_dialog_field = DialogField.create(:name => "dialog_field")
built_dialog_field2 = DialogField.create(:name => "dialog_field_2")
allow(dialog_field_importer).to receive(:import_field).and_return(built_dialog_field, built_dialog_field2)
built_dialog_field3 = DialogField.create(:name => "df_with_old_trigger", :trigger_auto_refresh => true, :position => 0)
built_dialog_field4 = DialogField.create(:name => "df_with_old_responder", :auto_refresh => true, :position => 1)
allow(dialog_field_importer).to receive(:import_field).and_return(built_dialog_field, built_dialog_field2, built_dialog_field3, built_dialog_field4)
end
end

Expand Down Expand Up @@ -187,7 +189,12 @@
it "sets associations" do
expect do
dialog_import_service.import_all_service_dialogs_from_yaml_file("filename")
end.to change(DialogFieldAssociation, :count).by(1)
end.to change(DialogFieldAssociation, :count).by(2)

expect(DialogField.find(DialogFieldAssociation.last.respond_id).name).to eq("df_with_old_responder")
expect(DialogField.find(DialogFieldAssociation.last.trigger_id).name).to eq("df_with_old_trigger")
expect(DialogField.find(DialogFieldAssociation.first.trigger_id).name).to eq("dialog_field_2")
expect(DialogField.find(DialogFieldAssociation.first.respond_id).name).to eq("dialog_field")
end
end
end
Expand Down