Skip to content

Commit

Permalink
Change dialog import to only use old association style if new ones ar…
Browse files Browse the repository at this point in the history
…e nonexistent.

If using a new dialog, the DialogFieldAssociations should be what we're using
  • Loading branch information
d-m-u committed Apr 27, 2018
1 parent 37d782f commit 9e1af4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lib/services/dialog_import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 29 additions & 4 deletions spec/lib/services/dialog_import_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 9e1af4d

Please sign in to comment.