From 9e1af4dcfc1d5eb9aef4bc90b4ad70e10da776bd Mon Sep 17 00:00:00 2001 From: d-m-u Date: Fri, 27 Apr 2018 13:33:42 -0400 Subject: [PATCH] Change dialog import to only use old association style if new ones are nonexistent. If using a new dialog, the DialogFieldAssociations should be what we're using --- lib/services/dialog_import_service.rb | 7 ++-- .../services/dialog_import_service_spec.rb | 33 ++++++++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/services/dialog_import_service.rb b/lib/services/dialog_import_service.rb index a86efa806d67..525c656d9dcb 100644 --- a/lib/services/dialog_import_service.rb +++ b/lib/services/dialog_import_service.rb @@ -144,16 +144,15 @@ def import_from_dialogs(dialogs) dialog.except!(:blueprint_id, 'blueprint_id') # blueprint_id might appear in some old dialogs, but no longer exists new_or_existing_dialog = Dialog.where(:label => dialog["label"]).first_or_create dialog['id'] = new_or_existing_dialog.id - associations_to_be_created = build_association_list(dialog) + new_associations = build_association_list(dialog) new_or_existing_dialog.update_attributes( dialog.merge( "dialog_tabs" => build_dialog_tabs(dialog), "resource_actions" => build_resource_actions(dialog) ) ) - old_associations = build_old_association_list(new_or_existing_dialog.dialog_fields).flatten - association_list = (associations_to_be_created + old_associations).reject(&:blank?) - build_associations(new_or_existing_dialog, association_list) + association_list = new_associations.reject(&:blank?).present? ? new_associations : build_old_association_list(new_or_existing_dialog.dialog_fields).flatten + build_associations(new_or_existing_dialog, association_list.reject(&:blank?)) end end diff --git a/spec/lib/services/dialog_import_service_spec.rb b/spec/lib/services/dialog_import_service_spec.rb index c30292716461..3a4281ff815e 100644 --- a/spec/lib/services/dialog_import_service_spec.rb +++ b/spec/lib/services/dialog_import_service_spec.rb @@ -186,19 +186,44 @@ dialog_import_service.import_all_service_dialogs_from_yaml_file("filename") end - it "sets associations" do + it "sets only new associations when both new and old style exist" do expect do dialog_import_service.import_all_service_dialogs_from_yaml_file("filename") - end.to change(DialogFieldAssociation, :count).by(2) + end.to change(DialogFieldAssociation, :count).by(1) - 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 + describe "association creation" do + context "with only old defunct associations present" do + before do + fields = [] + 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) + fields << built_dialog_field3 + fields << built_dialog_field4 + allow(dialog_field_importer).to receive(:import_field).and_return(built_dialog_field3, built_dialog_field4) + group = [{"label" => "New Box", "dialog_fields" => fields, :position => 1}] + tab = [{"label" => "New Tab", "dialog_groups" => group, :position => 2}] + dialog = [{"label" => "Test", "dialog_tabs" => tab}] + allow(YAML).to receive(:load_file).with("filename").and_return(dialog) + end + + it "sets only old associations when only old style exists" do + + expect do + dialog_import_service.import_all_service_dialogs_from_yaml_file("filename") + end.to change(DialogFieldAssociation, :count).by(1) + + expect(DialogField.find(DialogFieldAssociation.first.trigger_id).name).to eq("df_with_old_trigger") + expect(DialogField.find(DialogFieldAssociation.first.respond_id).name).to eq("df_with_old_responder") + end + end + end + describe "#import_service_dialogs" do include_context "DialogImportService dialog setup"