diff --git a/app/models/dialog.rb b/app/models/dialog.rb index 821c1ab9bf9..d0ccf48f9a6 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -86,13 +86,13 @@ def validate_field_data end end - def load_values_into_fields(values, overwrite = true) + def load_values_into_fields(values, ignore_nils = true) values = values.with_indifferent_access dialog_field_hash.each_value do |field| field.dialog = self new_value = values[field.automate_key_name] || values[field.name] || values.dig("parameters", field.name) - new_value ||= field.value unless overwrite + new_value ||= field.value unless ignore_nils field.value = new_value end @@ -118,6 +118,14 @@ def initialize_value_context(_values) dialog_field_hash.each_value(&:initialize_value_context) end + def initialize_static_values + dialog_field_hash.each_value do |field| + field.dialog = self + end + + dialog_field_hash.each_value(&:initialize_static_values) + end + def init_fields_with_values_for_request(values) values = values.with_indifferent_access diff --git a/app/models/dialog_field.rb b/app/models/dialog_field.rb index 0d2ad2ed9f3..9d98cf44cf0 100644 --- a/app/models/dialog_field.rb +++ b/app/models/dialog_field.rb @@ -103,6 +103,12 @@ def initialize_value_context end end + def initialize_static_values + if @value.blank? && !dynamic + @value = default_value + end + end + def initialize_with_given_value(given_value) self.default_value = given_value end diff --git a/app/models/resource_action_workflow.rb b/app/models/resource_action_workflow.rb index afa343aa0fd..9f5f66a8071 100644 --- a/app/models/resource_action_workflow.rb +++ b/app/models/resource_action_workflow.rb @@ -93,31 +93,6 @@ def create_values_hash } end - def load_dialog(resource_action, values, options) - if resource_action.nil? - resource_action = load_resource_action(values) - @settings[:resource_action_id] = resource_action.id if resource_action - end - - dialog = resource_action.dialog if resource_action - if dialog - dialog.target_resource = @target - if options[:display_view_only] - dialog.init_fields_with_values_for_request(values) - elsif options[:provision_workflow] || options[:init_defaults] - dialog.initialize_value_context(values) - dialog.load_values_into_fields(values, false) - elsif options[:refresh] || options[:submit_workflow] - dialog.load_values_into_fields(values) - elsif options[:reconfigure] - dialog.initialize_with_given_values(values) - else - dialog.initialize_value_context(values) - end - end - dialog - end - def init_field_hash @dialog.dialog_fields.each_with_object({}) { |df, result| result[df.name] = df } end @@ -148,6 +123,38 @@ def validate(_values = nil) private + def load_dialog(resource_action, values, options) + if resource_action.nil? + resource_action = load_resource_action(values) + @settings[:resource_action_id] = resource_action.id if resource_action + end + + dialog = resource_action.dialog if resource_action + load_proper_dialog_values(dialog, options, values) if dialog + + dialog + end + + def load_proper_dialog_values(dialog, options, values) + dialog.target_resource = @target + + if options[:display_view_only] + dialog.init_fields_with_values_for_request(values) + elsif options[:provision_workflow] || options[:init_defaults] + dialog.initialize_value_context(values) + dialog.load_values_into_fields(values, false) + elsif options[:submit_workflow] + dialog.load_values_into_fields(values) + elsif options[:refresh] + dialog.initialize_static_values + dialog.load_values_into_fields(values, false) + elsif options[:reconfigure] + dialog.initialize_with_given_values(values) + else + dialog.initialize_value_context(values) + end + end + def create_request?(values) ra = load_resource_action(values) !ra.resource.kind_of?(CustomButton) && has_request_class? diff --git a/spec/models/dialog_field_spec.rb b/spec/models/dialog_field_spec.rb index a45d8b670d4..0640cbce1e5 100644 --- a/spec/models/dialog_field_spec.rb +++ b/spec/models/dialog_field_spec.rb @@ -162,6 +162,45 @@ end end + describe "#initialize_static_values" do + let(:field) { described_class.new(:dynamic => dynamic, :value => value) } + let(:field_with_default) { described_class.new(:dynamic => dynamic, :value => value, :default_value => "test") } + + context "when the field is dynamic" do + let(:dynamic) { true } + let(:value) { "value" } + + it "does not change the value" do + field.initialize_static_values + expect(field.instance_variable_get(:@value)).to eq("value") + end + end + + context "when the field is not dynamic" do + let(:dynamic) { false } + + context "with a user-adjusted value" do + let(:value) { "not dynamic" } + + it "does not adjust the value" do + field.initialize_static_values + expect(field.instance_variable_get(:@value)).to eq("not dynamic") + end + end + + context "without a user-adjusted value" do + context "with a default value" do + let(:value) { nil } + + it "does adjust the value" do + field_with_default.initialize_static_values + expect(field_with_default.instance_variable_get(:@value)).to eq("test") + end + end + end + end + end + describe "#initialize_with_given_value" do let(:field) { described_class.new(:default_value => "not the given value") } diff --git a/spec/models/dialog_spec.rb b/spec/models/dialog_spec.rb index 27d1b8457d3..bb37fb598f3 100644 --- a/spec/models/dialog_spec.rb +++ b/spec/models/dialog_spec.rb @@ -59,6 +59,30 @@ end end + describe "#initialize_static_values" do + let(:dialog) { described_class.new } + let(:field1) { DialogField.new(:name => "name1") } + let(:field2) { DialogField.new(:name => "name2") } + + before do + allow(dialog).to receive(:dialog_fields).and_return([field1, field2]) + allow(field1).to receive(:initialize_static_values) + allow(field2).to receive(:initialize_static_values) + end + + it "sets the current dialog to each field" do + dialog.initialize_static_values + expect(field1.dialog).to eq(dialog) + expect(field2.dialog).to eq(dialog) + end + + it "calls initialize_static_values on each field" do + expect(field1).to receive(:initialize_static_values) + expect(field2).to receive(:initialize_static_values) + dialog.initialize_static_values + end + end + describe "#content" do it "returns the serialized content" do dialog = FactoryBot.create(:dialog, :description => "foo", :label => "bar") @@ -496,16 +520,16 @@ 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 + context "when ignore_nils is true" do + it "accepts nil values as a value" 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 + context "when ignore_nils is false" do + it "does not set values to nil" do vars = {:field1 => "10.8.99.248"} dialog.load_values_into_fields(vars, false) expect(dialog_field2.value).to eq("321") diff --git a/spec/models/resource_action_workflow_spec.rb b/spec/models/resource_action_workflow_spec.rb index f2ee3a74eac..a4b2dead4f5 100644 --- a/spec/models/resource_action_workflow_spec.rb +++ b/spec/models/resource_action_workflow_spec.rb @@ -215,6 +215,7 @@ allow(ResourceAction).to receive(:find).and_return(resource_action) allow(dialog).to receive(:load_values_into_fields).with(values) allow(dialog).to receive(:initialize_value_context).with(values) + allow(dialog).to receive(:initialize_static_values) allow(dialog).to receive(:init_fields_with_values_for_request).with(values) allow(dialog).to receive(:target_resource=) end @@ -241,8 +242,9 @@ context "when the options are set to a refresh request" do let(:options) { {:refresh => true} } - it "loads the values into fields" do - expect(dialog).to receive(:load_values_into_fields).with(values) + it "initializes the static values and then loads the values into fields" do + expect(dialog).to receive(:initialize_static_values).ordered + expect(dialog).to receive(:load_values_into_fields).with(values, false).ordered ResourceActionWorkflow.new(values, nil, resource_action, options) end end