Skip to content

Commit

Permalink
Merge pull request #15740 from d-m-u/adding_associations_to_import
Browse files Browse the repository at this point in the history
Adds dialog field association info to importer
  • Loading branch information
gmcculloug authored Aug 15, 2017
2 parents a67f19b + eb0a6db commit aad4439
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/services/dialog_import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions spec/lib/services/dialog_import_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit aad4439

Please sign in to comment.