From e73c014c798bcf6b6736698e71d5550b3ad69540 Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Mon, 13 Aug 2018 08:00:10 -0700 Subject: [PATCH] Allow nil values to not overwrite old values in #load_values_into_fields Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1613935 --- app/models/dialog.rb | 7 +++-- app/models/resource_action_workflow.rb | 2 +- spec/models/dialog_spec.rb | 33 ++++++++++++++++++++ spec/models/resource_action_workflow_spec.rb | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/models/dialog.rb b/app/models/dialog.rb index 00122abceb6a..e64d057a5501 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -88,12 +88,15 @@ def validate_field_data result end - def load_values_into_fields(values) + def load_values_into_fields(values, overwrite = true) values = values.with_indifferent_access dialog_field_hash.each_value do |field| field.dialog = self - field.value = values[field.automate_key_name] || values[field.name] + new_value = values[field.automate_key_name] || values[field.name] + new_value ||= field.value unless overwrite + + field.value = new_value end end diff --git a/app/models/resource_action_workflow.rb b/app/models/resource_action_workflow.rb index 237f8db17907..4f2f3d5b47b3 100644 --- a/app/models/resource_action_workflow.rb +++ b/app/models/resource_action_workflow.rb @@ -97,7 +97,7 @@ def load_dialog(resource_action, values, options) dialog.init_fields_with_values_for_request(values) elsif options[:provision_workflow] dialog.initialize_value_context(values) - dialog.load_values_into_fields(values) + dialog.load_values_into_fields(values, false) elsif options[:refresh] || options[:submit_workflow] dialog.load_values_into_fields(values) elsif options[:reconfigure] diff --git a/spec/models/dialog_spec.rb b/spec/models/dialog_spec.rb index 1f46e6ee8447..a9c74d6bb825 100644 --- a/spec/models/dialog_spec.rb +++ b/spec/models/dialog_spec.rb @@ -470,6 +470,12 @@ dialog.load_values_into_fields(vars) expect(dialog_field1.value).to eq("10.8.99.248") end + + it "sets field value when prefixed with 'dialog'" do + vars = {"dialog_field1" => "10.8.99.248"} + dialog.load_values_into_fields(vars) + expect(dialog_field1.value).to eq("10.8.99.248") + end end context "symbol values" do @@ -478,6 +484,33 @@ dialog.load_values_into_fields(vars) expect(dialog_field1.value).to eq("10.8.99.248") end + + it "sets field value when prefixed with 'dialog'" do + vars = {:dialog_field1 => "10.8.99.248"} + dialog.load_values_into_fields(vars) + expect(dialog_field1.value).to eq("10.8.99.248") + end + end + + context "when the incoming values are missing keys" do + let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1, dialog_field2]) } + let(:dialog_field2) { DialogField.new(:value => "321", :name => "field2") } + + context "when overwrite is true" do + it "sets nil values" do + vars = {:field1 => "10.8.99.248"} + dialog.load_values_into_fields(vars) + expect(dialog_field2.value).to eq(nil) + end + end + + context "when overwrite is false" do + it "does not set nil values" do + vars = {:field1 => "10.8.99.248"} + dialog.load_values_into_fields(vars, false) + expect(dialog_field2.value).to eq("321") + end + end end end diff --git a/spec/models/resource_action_workflow_spec.rb b/spec/models/resource_action_workflow_spec.rb index 11e15e70eed4..8db9826fbbeb 100644 --- a/spec/models/resource_action_workflow_spec.rb +++ b/spec/models/resource_action_workflow_spec.rb @@ -163,7 +163,7 @@ it "initializes the value context and then loads the values into fields" do expect(dialog).to receive(:initialize_value_context).with(values).ordered - expect(dialog).to receive(:load_values_into_fields).with(values).ordered + expect(dialog).to receive(:load_values_into_fields).with(values, false).ordered ResourceActionWorkflow.new(values, nil, resource_action, options) end end