diff --git a/lib/services/dialog_import_service.rb b/lib/services/dialog_import_service.rb index daba3758230..6436de53cd8 100644 --- a/lib/services/dialog_import_service.rb +++ b/lib/services/dialog_import_service.rb @@ -104,7 +104,15 @@ def create_import_file_upload(file_contents) def import_from_dialogs(dialogs) raise ParsedNonDialogYamlError if dialogs.empty? + associations = [] dialogs.each do |dialog| + dialog["dialog_tabs"].flat_map do |tab| + tab["dialog_groups"].flat_map do |group| + group["dialog_fields"].flat_map do |field| + associations << { field["name"] => field["dialog_field_responders"] } unless field["dialog_field_responders"].nil? + end + end + end new_or_existing_dialog = Dialog.where(:label => dialog["label"]).first_or_create dialog['id'] = new_or_existing_dialog.id new_or_existing_dialog.update_attributes( @@ -113,6 +121,16 @@ def import_from_dialogs(dialogs) "resource_actions" => build_resource_actions(dialog) ) ) + fields = new_or_existing_dialog.dialog_fields + associations.each do |association| + association.values.each do |values| + values.each do |responder| + next if fields.select { |field| field.name == responder }.empty? + DialogFieldAssociation.create(:trigger_id => fields.find { |field| field.name.include?(association.keys.first) }.id, + :respond_id => fields.find { |field| field.name == responder }.id) + end + end + end end rescue DialogFieldImporter::InvalidDialogFieldTypeError raise diff --git a/spec/lib/services/dialog_import_service_spec.rb b/spec/lib/services/dialog_import_service_spec.rb index 1763c34953a..2ba87edea71 100644 --- a/spec/lib/services/dialog_import_service_spec.rb +++ b/spec/lib/services/dialog_import_service_spec.rb @@ -8,7 +8,8 @@ shared_context "DialogImportService dialog setup" do let(:dialog_fields) do - [{"name" => "FavoriteColor", "label" => "Favorite Color"}] + [{"name" => "FavoriteColor", "label" => "Favorite Color"}, + {"name" => "dialog_field_2", "dialog_field_responders" => ["dialog_field"] }] end let(:dialog_groups) do @@ -28,7 +29,8 @@ before do built_dialog_field = DialogField.create(:name => "dialog_field") - allow(dialog_field_importer).to receive(:import_field).and_return(built_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) end end @@ -181,6 +183,12 @@ expect(dialog_field_importer).to receive(:import_field).with(dialog_fields[0]) dialog_import_service.import_all_service_dialogs_from_yaml_file("filename") end + + 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 end end