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

Adds dialog field association info to importer #15740

Merged
merged 1 commit into from
Aug 15, 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
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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you're planning to add tests because it's hard to visualize this data structure looks like in my head.

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