From 956d9b591244606d131b0166c5f08e6e8fb152da Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Tue, 10 Apr 2018 09:08:52 -0700 Subject: [PATCH 1/3] Modify dynamic loading to be consistent across all field types https://bugzilla.redhat.com/show_bug.cgi?id=1559757 --- app/models/dialog.rb | 8 +- app/models/dialog_field.rb | 7 +- app/models/dialog_field_check_box.rb | 5 - app/models/dialog_field_date_control.rb | 5 +- app/models/dialog_field_date_time_control.rb | 5 +- app/models/dialog_field_sorted_item.rb | 12 +- app/models/dialog_field_tag_control.rb | 4 + app/models/dialog_field_text_box.rb | 1 - spec/models/dialog_field_sorted_item_spec.rb | 129 +++++++++++++++++++ spec/models/dialog_field_spec.rb | 41 ++++++ spec/models/dialog_field_text_box_spec.rb | 96 +++----------- 11 files changed, 214 insertions(+), 99 deletions(-) diff --git a/app/models/dialog.rb b/app/models/dialog.rb index e841ab90130..e146eddce9f 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -91,10 +91,8 @@ def validate_field_data def init_fields_with_values(values) dialog_field_hash.each do |key, field| field.dialog = self - values[key] = field.value + field.initialize_value_context end - dialog_field_hash.each { |key, field| values[key] = field.initialize_with_values(values) } - dialog_field_hash.each { |_key, field| field.update_values(values) } end def init_fields_with_values_for_request(values) @@ -112,10 +110,6 @@ def content(target = nil, resource_action = nil, all_attributes = false) workflow = ResourceActionWorkflow.new({}, User.current_user, resource_action, :target => target) - workflow.dialog.dialog_fields.each do |dialog_field| - # Accessing dialog_field.values forces an update for any values coming from automate - dialog_field.values = dialog_field.values - end DialogSerializer.new.serialize(Array[workflow.dialog], all_attributes) end diff --git a/app/models/dialog_field.rb b/app/models/dialog_field.rb index 036495ee0df..4ebbe1c3d09 100644 --- a/app/models/dialog_field.rb +++ b/app/models/dialog_field.rb @@ -91,8 +91,13 @@ def self.field_types FIELD_CONTROLS end + def initialize_value_context + @value = values_from_automate if dynamic && @value.blank? + end + def initialize_with_values(dialog_values) - @value = value_from_dialog_fields(dialog_values) || default_value + # override in subclasses + nil end def update_values(_dialog_values) diff --git a/app/models/dialog_field_check_box.rb b/app/models/dialog_field_check_box.rb index 3d74a9a3cc1..e6d0e76dfc4 100644 --- a/app/models/dialog_field_check_box.rb +++ b/app/models/dialog_field_check_box.rb @@ -5,11 +5,6 @@ def checked? value == "t" end - def value - @value = values_from_automate if dynamic && @value.blank? - @value - end - def initial_values false end diff --git a/app/models/dialog_field_date_control.rb b/app/models/dialog_field_date_control.rb index 4b1983f792e..f8a38e69c49 100644 --- a/app/models/dialog_field_date_control.rb +++ b/app/models/dialog_field_date_control.rb @@ -17,9 +17,8 @@ def automate_output_value end def value - @value = dynamic ? values_from_automate : default_time if @value.blank? - - Date.parse(@value).strftime("%m/%d/%Y") + value_to_parse = @value.blank? ? default_time : @value + Date.parse(value_to_parse).strftime("%m/%d/%Y") end def normalize_automate_values(automate_hash) diff --git a/app/models/dialog_field_date_time_control.rb b/app/models/dialog_field_date_time_control.rb index e4652745222..eae6e394175 100644 --- a/app/models/dialog_field_date_time_control.rb +++ b/app/models/dialog_field_date_time_control.rb @@ -7,9 +7,8 @@ def automate_output_value end def value - @value = dynamic ? values_from_automate : default_time if @value.blank? - - Time.parse(@value).strftime("%m/%d/%Y %H:%M") + value_to_parse = @value.blank? ? default_time : @value + Time.parse(value_to_parse).strftime("%m/%d/%Y %H:%M") end def refresh_json_value diff --git a/app/models/dialog_field_sorted_item.rb b/app/models/dialog_field_sorted_item.rb index a2f4d1d0dd7..c81ebef7a22 100644 --- a/app/models/dialog_field_sorted_item.rb +++ b/app/models/dialog_field_sorted_item.rb @@ -1,6 +1,14 @@ class DialogFieldSortedItem < DialogField AUTOMATE_VALUE_FIELDS = %w(sort_by sort_order data_type default_value required read_only visible description).freeze + def initialize_value_context + if load_values_on_init? + raw_values + else + @raw_values = initial_values + end + end + def initialize_with_values(dialog_values) if load_values_on_init? raw_values @@ -95,7 +103,7 @@ def sort_data(data_to_sort) data_to_sort end - def determine_selected_default_value + def determine_selected_value if dynamic? && force_multi_value && !default_value.kind_of?(Array) self.default_value = Array.wrap(default_value) end @@ -108,7 +116,7 @@ def raw_values reject_extraneous_nil_values unless dynamic? @raw_values = sort_data(@raw_values) add_nil_option unless dynamic? - determine_selected_default_value + determine_selected_value @raw_values end diff --git a/app/models/dialog_field_tag_control.rb b/app/models/dialog_field_tag_control.rb index b69e8d4bafb..9a8048990b2 100644 --- a/app/models/dialog_field_tag_control.rb +++ b/app/models/dialog_field_tag_control.rb @@ -1,4 +1,8 @@ class DialogFieldTagControl < DialogFieldSortedItem + def initialize_value_context + @value = get_default_value + end + def initialize_with_values(dialog_values) @value = value_from_dialog_fields(dialog_values) || get_default_value end diff --git a/app/models/dialog_field_text_box.rb b/app/models/dialog_field_text_box.rb index 313f3f82514..0a21b0fcf09 100644 --- a/app/models/dialog_field_text_box.rb +++ b/app/models/dialog_field_text_box.rb @@ -2,7 +2,6 @@ class DialogFieldTextBox < DialogField AUTOMATE_VALUE_FIELDS = %w(data_type protected required validator_rule validator_type read_only visible description).freeze def value - @value = values_from_automate if dynamic && @value.blank? return nil if @value.nil? convert_value_to_type end diff --git a/spec/models/dialog_field_sorted_item_spec.rb b/spec/models/dialog_field_sorted_item_spec.rb index b3d40561866..89c40a16c7f 100644 --- a/spec/models/dialog_field_sorted_item_spec.rb +++ b/spec/models/dialog_field_sorted_item_spec.rb @@ -1,4 +1,133 @@ describe DialogFieldSortedItem do + describe "#initialize_value_context" do + let(:dialog_field) do + described_class.new( + :name => "potato_name", + :default_value => default_value, + :dynamic => true, + :load_values_on_init => load_values_on_init, + :show_refresh_button => show_refresh_button, + :values => [%w(test test), %w(test2 test2)] + ) + end + let(:default_value) { "test2" } + + context "when show_refresh_button is true" do + let(:show_refresh_button) { true } + + context "when load_values_on_init is true" do + let(:load_values_on_init) { true } + + context "when the dialog values match the automate key name" do + let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("dialog potato value") + end + end + + context "when the dialog values match the dialog name" do + let(:dialog_values) { {"potato_name" => "potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("potato value") + end + end + + context "when the values from the passed in dialog values do not match either the automate or dialog name" do + let(:dialog_values) { {"not_potato_name" => "not potato value"} } + + it "defaults to nil" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq(nil) + end + end + end + + context "when load_values_on_init is false" do + let(:load_values_on_init) { false } + let(:dialog_values) { "potato" } + + it "sets the raw values to the initial values" do + dialog_field.initialize_value_context + expect(dialog_field.instance_variable_get(:@raw_values)).to eq([[nil, ""]]) + end + end + end + + context "when show_refresh_button is false" do + let(:show_refresh_button) { false } + + context "when load_values_on_init is true" do + let(:load_values_on_init) { true } + + context "when the dialog values match the automate key name" do + let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("dialog potato value") + end + end + + context "when the dialog values match the dialog name" do + let(:dialog_values) { {"potato_name" => "potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("potato value") + end + end + + context "when the values from the passed in dialog values do not match either the automate or dialog name" do + let(:dialog_values) { {"not_potato_name" => "not potato value"} } + + context "when the default value does not exist in the list of options" do + let(:default_value) { "default value" } + + it "defaults to nil" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq(nil) + end + end + end + end + + context "when load_values_on_init is false" do + let(:load_values_on_init) { false } + + context "when the dialog values match the automate key name" do + let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("dialog potato value") + end + end + + context "when the dialog values match the dialog name" do + let(:dialog_values) { {"potato_name" => "potato value"} } + + it "uses the values given" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq("potato value") + end + end + + context "when the values from the passed in dialog values do not match either the automate or dialog name" do + let(:dialog_values) { {"not_potato_name" => "not potato value"} } + + it "defaults to nil" do + dialog_field.initialize_value_context + expect(dialog_field.value).to eq(nil) + end + end + end + end + end + describe "#initialize_with_values" do let(:dialog_field) do described_class.new( diff --git a/spec/models/dialog_field_spec.rb b/spec/models/dialog_field_spec.rb index 13e62d0603f..3a1fbae0354 100644 --- a/spec/models/dialog_field_spec.rb +++ b/spec/models/dialog_field_spec.rb @@ -140,6 +140,47 @@ end end + describe "#initialize_value_context" do + let(:field) { described_class.new(:dynamic => dynamic, :value => value) } + + context "when the field is dynamic" do + let(:dynamic) { true } + + context "when the value is blank" do + let(:value) { "" } + let(:automate_value) { "some value from automate" } + + before do + allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).and_return(automate_value) + end + + it "sets the value to the automate value" do + field.initialize_value_context + expect(field.instance_variable_get(:@value)).to eq("some value from automate") + end + end + + context "when the value is not blank" do + let(:value) { "not blank" } + + it "does not adjust the value" do + field.initialize_value_context + expect(field.instance_variable_get(:@value)).to eq("not blank") + end + end + end + + context "when the field is not dynamic" do + let(:dynamic) { false } + let(:value) { "not dynamic" } + + it "does not adjust the value" do + field.initialize_value_context + expect(field.instance_variable_get(:@value)).to eq("not dynamic") + end + end + end + describe "#automate_output_values" do let(:dialog_field) { described_class.new(:data_type => data_type, :value => "123") } diff --git a/spec/models/dialog_field_text_box_spec.rb b/spec/models/dialog_field_text_box_spec.rb index 8a6e7b28047..002f0abe205 100644 --- a/spec/models/dialog_field_text_box_spec.rb +++ b/spec/models/dialog_field_text_box_spec.rb @@ -9,7 +9,7 @@ context "dialog field text box without options hash" do let(:df) { FactoryGirl.build(:dialog_field_text_box, :label => 'test field', :name => 'test field') } - + it "#protected?" do expect(df).not_to be_protected end @@ -18,15 +18,6 @@ df.protected = true expect(df).to be_protected end - - describe "#initialize_with_values" do - it "decrypts protected automate dialog values" do - password = "test" - df.protected = true - df.initialize_with_values(df.automate_key_name => MiqPassword.encrypt(password)) - expect(df.value).to eq(password) - end - end end context "dialog field text box without protected field" do @@ -160,92 +151,43 @@ end describe "#value" do - let(:dialog_field) { described_class.new(:dynamic => dynamic, :value => value, :data_type => data_type) } + let(:dialog_field) { described_class.new(:value => value, :data_type => data_type) } - context "when the dialog field is dynamic" do - let(:dynamic) { true } - - context "when the dialog field has a value already" do - let(:value) { "test" } - - context "when the data type is integer" do - let(:data_type) { "integer" } - - it "converts the data into an integer" do - expect(dialog_field.value).to eq(0) - end - end + context "when the value is nil" do + let(:value) { nil } - context "when the data type is string" do - let(:data_type) { "string" } + context "when the data_type is integer" do + let(:data_type) { "integer" } - it "returns the current value" do - expect(dialog_field.value).to eq("test") - end + it "returns nil" do + expect(dialog_field.value).to be_nil end end - context "when the dialog field does not have a value" do - let(:value) { "" } - - before do - allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return("processor") - end - - context "when the data type is an integer" do - let(:data_type) { "integer" } + context "when the data_type is string" do + let(:data_type) { "string" } - it "converts the data into an integer" do - expect(dialog_field.value).to eq(0) - end - end - - context "when the data type is a string" do - let(:data_type) { "string" } - - it "returns the values from the value processor" do - expect(dialog_field.value).to eq("processor") - end + it "returns nil" do + expect(dialog_field.value).to be_nil end end end - context "when the dialog field is not dynamic" do - let(:dynamic) { false } + context "when the value is not nil" do + let(:value) { "test" } - context "when the data type is integer" do + context "when the data_type is integer" do let(:data_type) { "integer" } - context "when the value is nil" do - let(:value) { nil } - - it "returns nil" do - expect(dialog_field.value).to eq(nil) - end - end - - context "when the value is not nil" do - let(:value) { "test" } - - it "converts the data into an integer" do - expect(dialog_field.value).to eq(0) - end - end - - context "when there is no value" do - let(:value) { nil } - - it "does not convert nil value to zero" do - expect(dialog_field.value).to be_nil - end + it "returns an integer converted value" do + expect(dialog_field.value).to eq(0) end end - context "when the data type is string" do + context "when the data_type is string" do let(:data_type) { "string" } - let(:value) { "test" } - it "returns the current value" do + it "returns the string" do expect(dialog_field.value).to eq("test") end end From c2121dffbee1937c41e0a3e16132bd3232450e1c Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Mon, 23 Apr 2018 00:12:16 -0700 Subject: [PATCH 2/3] Modify dynamic loading based on whether it is initial load or a refresh https://bugzilla.redhat.com/show_bug.cgi?id=1559757 --- app/models/dialog.rb | 12 +++++++++++- app/models/dialog_field.rb | 5 +++++ app/models/dialog_field_check_box.rb | 2 +- app/models/dialog_field_date_control.rb | 2 +- app/models/dialog_field_serializer.rb | 4 ++-- app/models/dialog_field_sorted_item.rb | 4 ++++ app/models/dialog_field_text_box.rb | 2 +- app/models/resource_action_workflow.rb | 4 +++- 8 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/models/dialog.rb b/app/models/dialog.rb index e146eddce9f..9f0a79733c4 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -88,9 +88,19 @@ def validate_field_data result end - def init_fields_with_values(values) + def load_values_into_fields(values) + dialog_field_hash.each do |_key, field| + field.dialog = self + field.value = values[field.automate_key_name] || values[field.name] + end + end + + def initialize_value_context(values) dialog_field_hash.each do |key, field| field.dialog = self + end + + dialog_field_hash.each do |_key, field| field.initialize_value_context end end diff --git a/app/models/dialog_field.rb b/app/models/dialog_field.rb index 4ebbe1c3d09..1d9279cb101 100644 --- a/app/models/dialog_field.rb +++ b/app/models/dialog_field.rb @@ -91,6 +91,10 @@ def self.field_types FIELD_CONTROLS end + def extract_dynamic_values + value + end + def initialize_value_context @value = values_from_automate if dynamic && @value.blank? end @@ -127,6 +131,7 @@ def resource end def update_and_serialize_values + trigger_automate_value_updates DialogFieldSerializer.serialize(self) end diff --git a/app/models/dialog_field_check_box.rb b/app/models/dialog_field_check_box.rb index e6d0e76dfc4..4110fadbf6f 100644 --- a/app/models/dialog_field_check_box.rb +++ b/app/models/dialog_field_check_box.rb @@ -28,7 +28,7 @@ def refresh_json_value end def trigger_automate_value_updates - values_from_automate + @value = values_from_automate end private diff --git a/app/models/dialog_field_date_control.rb b/app/models/dialog_field_date_control.rb index f8a38e69c49..9a220f67455 100644 --- a/app/models/dialog_field_date_control.rb +++ b/app/models/dialog_field_date_control.rb @@ -45,7 +45,7 @@ def refresh_json_value end def trigger_automate_value_updates - values_from_automate + @value = values_from_automate end private diff --git a/app/models/dialog_field_serializer.rb b/app/models/dialog_field_serializer.rb index 6374a7a9b8b..167cb32e39a 100644 --- a/app/models/dialog_field_serializer.rb +++ b/app/models/dialog_field_serializer.rb @@ -17,8 +17,8 @@ def serialize(dialog_field, all_attributes = false) } if dialog_field.dynamic? - dynamic_values = dialog_field.trigger_automate_value_updates - extra_attributes["values"] = dynamic_values + # dynamic_values = dialog_field.trigger_automate_value_updates + extra_attributes["values"] = dialog_field.extract_dynamic_values end if dialog_field.type == "DialogFieldTagControl" diff --git a/app/models/dialog_field_sorted_item.rb b/app/models/dialog_field_sorted_item.rb index c81ebef7a22..9bce501a7e2 100644 --- a/app/models/dialog_field_sorted_item.rb +++ b/app/models/dialog_field_sorted_item.rb @@ -44,6 +44,10 @@ def values raw_values end + def extract_dynamic_values + @raw_values + end + def get_default_value trigger_automate_value_updates default_value diff --git a/app/models/dialog_field_text_box.rb b/app/models/dialog_field_text_box.rb index 0a21b0fcf09..663a34a66a2 100644 --- a/app/models/dialog_field_text_box.rb +++ b/app/models/dialog_field_text_box.rb @@ -70,7 +70,7 @@ def refresh_json_value end def trigger_automate_value_updates - values_from_automate + @value = values_from_automate end private diff --git a/app/models/resource_action_workflow.rb b/app/models/resource_action_workflow.rb index 9fe9b21df3b..9af525971f9 100644 --- a/app/models/resource_action_workflow.rb +++ b/app/models/resource_action_workflow.rb @@ -103,8 +103,10 @@ def load_dialog(resource_action, values, options) dialog.target_resource = @target if options[:display_view_only] dialog.init_fields_with_values_for_request(values) + elsif options[:refresh] + dialog.load_values_into_fields(values) else - dialog.init_fields_with_values(values) + dialog.initialize_value_context(values) end end dialog From dac46a2afd04c41c929a62163a5270b370c38814 Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Wed, 25 Apr 2018 11:56:04 -0700 Subject: [PATCH 3/3] Address rubocop concerns and fix specs https://bugzilla.redhat.com/show_bug.cgi?id=1559757 --- app/models/dialog.rb | 10 +- app/models/dialog_field.rb | 4 + app/models/dialog_field_check_box.rb | 4 - app/models/dialog_field_date_control.rb | 6 +- app/models/dialog_field_date_time_control.rb | 4 +- app/models/dialog_field_serializer.rb | 1 - app/models/dialog_field_sorted_item.rb | 5 + app/models/dialog_field_text_box.rb | 4 - spec/models/dialog_field_check_box_spec.rb | 37 ------- spec/models/dialog_field_date_control_spec.rb | 18 ++-- .../dialog_field_date_time_control_spec.rb | 26 ++--- spec/models/dialog_field_serializer_spec.rb | 2 +- spec/models/dialog_field_sorted_item_spec.rb | 96 ++++++++++--------- spec/models/dialog_field_spec.rb | 42 ++------ spec/models/resource_action_workflow_spec.rb | 31 +++--- 15 files changed, 110 insertions(+), 180 deletions(-) diff --git a/app/models/dialog.rb b/app/models/dialog.rb index 9f0a79733c4..70393a630bc 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -89,20 +89,18 @@ def validate_field_data end def load_values_into_fields(values) - dialog_field_hash.each do |_key, field| + dialog_field_hash.each_value do |field| field.dialog = self field.value = values[field.automate_key_name] || values[field.name] end end - def initialize_value_context(values) - dialog_field_hash.each do |key, field| + def initialize_value_context(_values) + dialog_field_hash.each_value do |field| field.dialog = self end - dialog_field_hash.each do |_key, field| - field.initialize_value_context - end + dialog_field_hash.each_value(&:initialize_value_context) end def init_fields_with_values_for_request(values) diff --git a/app/models/dialog_field.rb b/app/models/dialog_field.rb index 1d9279cb101..5e08b1d9ede 100644 --- a/app/models/dialog_field.rb +++ b/app/models/dialog_field.rb @@ -135,6 +135,10 @@ def update_and_serialize_values DialogFieldSerializer.serialize(self) end + def trigger_automate_value_updates + @value = values_from_automate + end + def update_dialog_field_responders(id_list) dialog_field_responders.destroy_all diff --git a/app/models/dialog_field_check_box.rb b/app/models/dialog_field_check_box.rb index 4110fadbf6f..d73abc63a70 100644 --- a/app/models/dialog_field_check_box.rb +++ b/app/models/dialog_field_check_box.rb @@ -27,10 +27,6 @@ def refresh_json_value {:checked => checked?, :read_only => read_only?, :visible => visible?} end - def trigger_automate_value_updates - @value = values_from_automate - end - private def required_value_error? diff --git a/app/models/dialog_field_date_control.rb b/app/models/dialog_field_date_control.rb index 9a220f67455..f9179e0fb42 100644 --- a/app/models/dialog_field_date_control.rb +++ b/app/models/dialog_field_date_control.rb @@ -17,7 +17,7 @@ def automate_output_value end def value - value_to_parse = @value.blank? ? default_time : @value + value_to_parse = @value.presence || default_time Date.parse(value_to_parse).strftime("%m/%d/%Y") end @@ -44,10 +44,6 @@ def refresh_json_value {:date => Date.parse(@value).strftime("%m/%d/%Y"), :read_only => read_only?, :visible => visible?} end - def trigger_automate_value_updates - @value = values_from_automate - end - private def default_time diff --git a/app/models/dialog_field_date_time_control.rb b/app/models/dialog_field_date_time_control.rb index eae6e394175..c4112c1f772 100644 --- a/app/models/dialog_field_date_time_control.rb +++ b/app/models/dialog_field_date_time_control.rb @@ -7,8 +7,8 @@ def automate_output_value end def value - value_to_parse = @value.blank? ? default_time : @value - Time.parse(value_to_parse).strftime("%m/%d/%Y %H:%M") + value_to_parse = @value.presence || default_time + Time.zone.parse(value_to_parse).strftime("%m/%d/%Y %H:%M") end def refresh_json_value diff --git a/app/models/dialog_field_serializer.rb b/app/models/dialog_field_serializer.rb index 167cb32e39a..07472948e3e 100644 --- a/app/models/dialog_field_serializer.rb +++ b/app/models/dialog_field_serializer.rb @@ -17,7 +17,6 @@ def serialize(dialog_field, all_attributes = false) } if dialog_field.dynamic? - # dynamic_values = dialog_field.trigger_automate_value_updates extra_attributes["values"] = dialog_field.extract_dynamic_values end diff --git a/app/models/dialog_field_sorted_item.rb b/app/models/dialog_field_sorted_item.rb index 9bce501a7e2..7b9b03f34b5 100644 --- a/app/models/dialog_field_sorted_item.rb +++ b/app/models/dialog_field_sorted_item.rb @@ -87,6 +87,11 @@ def refresh_json_value(checked_value) {:refreshed_values => refreshed_values, :checked_value => @value, :read_only => read_only?, :visible => visible?} end + def force_multi_value + # override in subclasses + nil + end + private def add_nil_option diff --git a/app/models/dialog_field_text_box.rb b/app/models/dialog_field_text_box.rb index 663a34a66a2..c4c220fd6ab 100644 --- a/app/models/dialog_field_text_box.rb +++ b/app/models/dialog_field_text_box.rb @@ -69,10 +69,6 @@ def refresh_json_value {:text => @value, :read_only => read_only?, :visible => visible?} end - def trigger_automate_value_updates - @value = values_from_automate - end - private def convert_value_to_type diff --git a/spec/models/dialog_field_check_box_spec.rb b/spec/models/dialog_field_check_box_spec.rb index 7d7f028cceb..281f66bc4f7 100644 --- a/spec/models/dialog_field_check_box_spec.rb +++ b/spec/models/dialog_field_check_box_spec.rb @@ -1,41 +1,4 @@ describe DialogFieldCheckBox do - describe "#value" do - let(:dialog_field) { described_class.new(:dynamic => dynamic, :value => value) } - - context "when the dialog field is dynamic" do - let(:dynamic) { true } - - context "when the current value is blank" do - let(:value) { "" } - - before do - allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return("processor") - end - - it "returns the values from the value processor" do - expect(dialog_field.value).to eq("processor") - end - end - - context "when the current value is not blank" do - let(:value) { "test" } - - it "returns the current value" do - expect(dialog_field.value).to eq("test") - end - end - end - - context "when the dialog field is not dynamic" do - let(:dynamic) { false } - let(:value) { "test" } - - it "returns the current value" do - expect(dialog_field.value).to eq("test") - end - end - end - describe "#checked?" do let(:dialog_field) { described_class.new(:value => value) } diff --git a/spec/models/dialog_field_date_control_spec.rb b/spec/models/dialog_field_date_control_spec.rb index f4bf119797b..6f4555a2512 100644 --- a/spec/models/dialog_field_date_control_spec.rb +++ b/spec/models/dialog_field_date_control_spec.rb @@ -25,25 +25,23 @@ context "when the value is blank" do let(:value) { "" } + before do + allow(described_class).to receive(:server_timezone).and_return("UTC") + end + context "when the field is dynamic" do let(:dynamic) { true } - before do - allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return("2015-01-02") - end - - it "returns the values from the value processor" do - expect(dialog_field.value).to eq("01/02/2015") + it "returns tomorrow's date" do + Timecop.freeze(Time.new(2015, 1, 2, 0, 0, 0, 0)) do + expect(dialog_field.value).to eq("01/03/2015") + end end end context "when the field is not dynamic" do let(:dynamic) { false } - before do - allow(described_class).to receive(:server_timezone).and_return("UTC") - end - it "returns tomorrow's date" do Timecop.freeze(Time.new(2015, 1, 2, 0, 0, 0, 0)) do expect(dialog_field.value).to eq("01/03/2015") diff --git a/spec/models/dialog_field_date_time_control_spec.rb b/spec/models/dialog_field_date_time_control_spec.rb index 706f47f0361..7d9420bd086 100644 --- a/spec/models/dialog_field_date_time_control_spec.rb +++ b/spec/models/dialog_field_date_time_control_spec.rb @@ -49,9 +49,10 @@ describe "#automate_output_value" do let(:dialog_field) { described_class.new(:value => value) } + let(:server) { double("MiqServer", :server_timezone => "UTC") } before do - allow(described_class).to receive(:server_timezone).and_return("UTC") + allow(MiqServer).to receive(:my_server).and_return(server) end context "when the dialog_field is blank" do @@ -83,6 +84,11 @@ describe "#value" do let(:dialog_field) { described_class.new(:dynamic => dynamic, :value => value) } + let(:server) { double("MiqServer", :server_timezone => "UTC") } + + before do + allow(MiqServer).to receive(:my_server).and_return(server) + end context "when the value is not blank" do let(:value) { "04/07/2015 00:00" } @@ -110,23 +116,16 @@ context "when the field is dynamic" do let(:dynamic) { true } - before do - allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate) - .with(dialog_field).and_return("2015-01-02") - end - - it "returns the values from the value processor" do - expect(dialog_field.value).to eq("01/02/2015 00:00") + it "returns tomorrow's date" do + Timecop.freeze(Time.utc(2015, 1, 2, 4, 30)) do + expect(dialog_field.value).to eq("01/03/2015 04:30") + end end end context "when the field is not dynamic" do let(:dynamic) { false } - before do - allow(described_class).to receive(:server_timezone).and_return("UTC") - end - it "returns tomorrow's date" do Timecop.freeze(Time.utc(2015, 1, 2, 4, 30)) do expect(dialog_field.value).to eq("01/03/2015 04:30") @@ -138,9 +137,10 @@ describe "#refresh_json_value" do let(:dialog_field) { described_class.new(:read_only => true) } + let(:server) { double("MiqServer", :server_timezone => "UTC") } before do - allow(described_class).to receive(:server_timezone).and_return("UTC") + allow(MiqServer).to receive(:my_server).and_return(server) allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate) .with(dialog_field).and_return("2015-02-03T18:50:00Z") end diff --git a/spec/models/dialog_field_serializer_spec.rb b/spec/models/dialog_field_serializer_spec.rb index 3ac470fcecb..ea7aeaa1480 100644 --- a/spec/models/dialog_field_serializer_spec.rb +++ b/spec/models/dialog_field_serializer_spec.rb @@ -51,7 +51,7 @@ let(:dynamic) { true } before do - allow(dialog_field).to receive(:trigger_automate_value_updates).and_return("dynamic values") + allow(dialog_field).to receive(:extract_dynamic_values).and_return("dynamic values") end context 'when wanting the excluded set of attributes' do diff --git a/spec/models/dialog_field_sorted_item_spec.rb b/spec/models/dialog_field_sorted_item_spec.rb index 89c40a16c7f..b13c03fbb8f 100644 --- a/spec/models/dialog_field_sorted_item_spec.rb +++ b/spec/models/dialog_field_sorted_item_spec.rb @@ -11,6 +11,11 @@ ) end let(:default_value) { "test2" } + let(:automate_values) { [%w(test1 test1), %w(test2 test2), %w(test3 test3)] } + + before do + allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).and_return(automate_values) + end context "when show_refresh_button is true" do let(:show_refresh_button) { true } @@ -18,30 +23,31 @@ context "when load_values_on_init is true" do let(:load_values_on_init) { true } - context "when the dialog values match the automate key name" do - let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + context "when the default_value is not included in the list of values" do + let(:default_value) { "test4" } - it "uses the values given" do + it "uses the first value as the default" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("dialog potato value") + expect(dialog_field.default_value).to eq("test1") end - end - context "when the dialog values match the dialog name" do - let(:dialog_values) { {"potato_name" => "potato value"} } - - it "uses the values given" do + it "sets the values to what should be returned from automate" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("potato value") + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end - context "when the values from the passed in dialog values do not match either the automate or dialog name" do - let(:dialog_values) { {"not_potato_name" => "not potato value"} } + context "when the default_value is included in the list of values" do + let(:default_value) { "test2" } - it "defaults to nil" do + it "uses the given value as the default" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq(nil) + expect(dialog_field.default_value).to eq("test2") + end + + it "sets the values to what should be returned from automate" do + dialog_field.initialize_value_context + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end end @@ -63,34 +69,31 @@ context "when load_values_on_init is true" do let(:load_values_on_init) { true } - context "when the dialog values match the automate key name" do - let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + context "when the default_value is not included in the list of values" do + let(:default_value) { "test4" } - it "uses the values given" do + it "uses the first value as the default" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("dialog potato value") + expect(dialog_field.default_value).to eq("test1") end - end - context "when the dialog values match the dialog name" do - let(:dialog_values) { {"potato_name" => "potato value"} } - - it "uses the values given" do + it "sets the values to what should be returned from automate" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("potato value") + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end - context "when the values from the passed in dialog values do not match either the automate or dialog name" do - let(:dialog_values) { {"not_potato_name" => "not potato value"} } + context "when the default_value is included in the list of values" do + let(:default_value) { "test2" } - context "when the default value does not exist in the list of options" do - let(:default_value) { "default value" } + it "uses the given value as the default" do + dialog_field.initialize_value_context + expect(dialog_field.default_value).to eq("test2") + end - it "defaults to nil" do - dialog_field.initialize_value_context - expect(dialog_field.value).to eq(nil) - end + it "sets the values to what should be returned from automate" do + dialog_field.initialize_value_context + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end end @@ -98,30 +101,31 @@ context "when load_values_on_init is false" do let(:load_values_on_init) { false } - context "when the dialog values match the automate key name" do - let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} } + context "when the default_value is not included in the list of values" do + let(:default_value) { "test4" } - it "uses the values given" do + it "uses the first value as the default" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("dialog potato value") + expect(dialog_field.default_value).to eq("test1") end - end - context "when the dialog values match the dialog name" do - let(:dialog_values) { {"potato_name" => "potato value"} } - - it "uses the values given" do + it "sets the values to what should be returned from automate" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq("potato value") + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end - context "when the values from the passed in dialog values do not match either the automate or dialog name" do - let(:dialog_values) { {"not_potato_name" => "not potato value"} } + context "when the default_value is included in the list of values" do + let(:default_value) { "test2" } - it "defaults to nil" do + it "uses the given value as the default" do dialog_field.initialize_value_context - expect(dialog_field.value).to eq(nil) + expect(dialog_field.default_value).to eq("test2") + end + + it "sets the values to what should be returned from automate" do + dialog_field.initialize_value_context + expect(dialog_field.extract_dynamic_values).to eq(automate_values) end end end diff --git a/spec/models/dialog_field_spec.rb b/spec/models/dialog_field_spec.rb index 3a1fbae0354..98f99f1421b 100644 --- a/spec/models/dialog_field_spec.rb +++ b/spec/models/dialog_field_spec.rb @@ -104,40 +104,6 @@ end end end - - describe "#initialize_with_values" do - it "uses #automate_key_name for extracting initial dialog values" do - dialog_value = "dummy dialog value" - df.initialize_with_values(df.automate_key_name => dialog_value) - expect(df.value).to eq(dialog_value) - end - - it "initializes to nil with no initial value and no default value" do - initial_dialog_values = {} - df.initialize_with_values(initial_dialog_values) - expect(df.value).to be_nil - end - - it "initializes to the default value with no initial value and a default value" do - initial_dialog_values = {} - df.default_value = "default_test" - df.initialize_with_values(initial_dialog_values) - expect(df.value).to eq("default_test") - end - - it "initializes to the dialog value with a dialog value and no default value" do - initial_dialog_values = {df.automate_key_name => "test"} - df.initialize_with_values(initial_dialog_values) - expect(df.value).to eq("test") - end - - it "initializes to the dialog value with a dialog value and a default value" do - initial_dialog_values = {df.automate_key_name => "test"} - df.default_value = "default_test" - df.initialize_with_values(initial_dialog_values) - expect(df.value).to eq("test") - end - end end describe "#initialize_value_context" do @@ -206,10 +172,14 @@ before do allow(DialogFieldSerializer).to receive(:serialize).with(dialog_field) + allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return( + "automate values" + ) end - it "serializes the dialog field" do - expect(DialogFieldSerializer).to receive(:serialize).with(dialog_field) + it "triggers an automate value update and then serializes the field" do + expect(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).ordered + expect(DialogFieldSerializer).to receive(:serialize).with(dialog_field).ordered dialog_field.update_and_serialize_values end end diff --git a/spec/models/resource_action_workflow_spec.rb b/spec/models/resource_action_workflow_spec.rb index 97458540489..a3803a55ea4 100644 --- a/spec/models/resource_action_workflow_spec.rb +++ b/spec/models/resource_action_workflow_spec.rb @@ -31,14 +31,6 @@ expect(@wf.dialog.id).to eq(@dialog.id) end - it "load default_value" do - @dialog_field.update_attribute(:default_value, "testing default") - @wf = ResourceActionWorkflow.new({}, admin, @resource_action) - expect(@wf.value(@dialog_field.name)).to eq("testing default") - df = @wf.dialog_field(@dialog_field.name) - expect(df.value).to eq("testing default") - end - it "field_name_exists?" do expect(@dialog.field_name_exist?('field_1')).to be_truthy expect(@dialog.field_name_exist?('field_11')).to be_falsey @@ -121,17 +113,17 @@ let(:resource_action) { instance_double("ResourceAction", :id => 123, :dialog => dialog) } let(:dialog) { instance_double("Dialog", :id => 321) } let(:values) { "the values" } - let(:options) { {:display_view_only => display_view_only} } before do allow(ResourceAction).to receive(:find).and_return(resource_action) - allow(dialog).to receive(:init_fields_with_values).with(values) + allow(dialog).to receive(:load_values_into_fields).with(values) + allow(dialog).to receive(:initialize_value_context).with(values) allow(dialog).to receive(:init_fields_with_values_for_request).with(values) allow(dialog).to receive(:target_resource=) end context "when the options set display_view_only to true" do - let(:display_view_only) { true } + let(:options) { {:display_view_only => true} } it "calls init_fields_with_values_for_request" do expect(dialog).to receive(:init_fields_with_values_for_request).with(values) @@ -139,11 +131,20 @@ end end - context "when the options set display_view_only to false" do - let(:display_view_only) { false } + 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) + ResourceActionWorkflow.new(values, nil, resource_action, options) + end + end + + context "when neither display_view_only nor refresh are true" do + let(:options) { {} } - it "calls init_fields_with_values" do - expect(dialog).to receive(:init_fields_with_values).with(values) + it "initializes the value context" do + expect(dialog).to receive(:initialize_value_context).with(values) ResourceActionWorkflow.new(values, nil, resource_action, options) end end