From 39ad62fbe76e17ae21e1670fa265f0bd5d7662c3 Mon Sep 17 00:00:00 2001 From: Greg McCullough Date: Mon, 3 Jul 2017 13:57:08 -0400 Subject: [PATCH] Merge pull request #15456 from eclarizio/BZ1462375 Fix for found as option in drop down service dialogs (cherry picked from commit 0cbb33deb054ac05f7107677323e9d0bbee008c0) https://bugzilla.redhat.com/show_bug.cgi?id=1478435 --- app/models/dialog_field_radio_button.rb | 2 +- app/models/dialog_field_sorted_item.rb | 23 ++++-- spec/models/dialog_field_sorted_item_spec.rb | 81 +++++++++++++------- 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/app/models/dialog_field_radio_button.rb b/app/models/dialog_field_radio_button.rb index 5fa26b880e55..dbb05053e4df 100644 --- a/app/models/dialog_field_radio_button.rb +++ b/app/models/dialog_field_radio_button.rb @@ -11,7 +11,7 @@ def initial_values def raw_values @raw_values ||= dynamic ? values_from_automate : static_raw_values - self.value ||= default_value if default_value_included_in_raw_values? + self.value ||= default_value if default_value_included?(@raw_values) @raw_values end diff --git a/app/models/dialog_field_sorted_item.rb b/app/models/dialog_field_sorted_item.rb index 18a2ede8fe83..f5036061eb1a 100644 --- a/app/models/dialog_field_sorted_item.rb +++ b/app/models/dialog_field_sorted_item.rb @@ -91,7 +91,7 @@ def sort_data(data_to_sort) def raw_values @raw_values ||= dynamic ? values_from_automate : static_raw_values - use_first_value_as_default unless default_value_included_in_raw_values? + use_first_value_as_default unless default_value_included?(@raw_values) self.value ||= default_value.send(value_modifier) @raw_values @@ -101,19 +101,32 @@ def use_first_value_as_default self.default_value = sort_data(@raw_values).first.try(:first) end - def default_value_included_in_raw_values? - @raw_values.collect { |value_pair| value_pair[0].send(value_modifier) }.include?(default_value.send(value_modifier)) + def default_value_included?(values_list) + values_list.collect { |value_pair| value_pair[0].send(value_modifier) }.include?(default_value.send(value_modifier)) end def static_raw_values - first_values = required? ? [[nil, ""]] : initial_values - first_values + self[:values].to_miq_a.reject { |value| value[0].nil? } + self[:values].to_miq_a.reject { |value| value[0].nil? }.unshift(nil_option).reject(&:empty?) end def initial_values [[nil, ""]] end + def initial_required_values + [nil, ""] + end + + def nil_option + if !required? + initial_values.flatten + elsif default_value.blank? || !default_value_included?(self[:values]) + initial_required_values + else + [] + end + end + def load_values_on_init? return true unless show_refresh_button load_values_on_init diff --git a/spec/models/dialog_field_sorted_item_spec.rb b/spec/models/dialog_field_sorted_item_spec.rb index e395b67947bf..54f9e7338b17 100644 --- a/spec/models/dialog_field_sorted_item_spec.rb +++ b/spec/models/dialog_field_sorted_item_spec.rb @@ -178,49 +178,76 @@ context "when the field is not dynamic" do let(:dynamic) { false } - let(:default_value) { "abc" } - context "when the field is required" do - let(:required) { true } + context "when the data type is not integer" do + context "when the default_value is set" do + let(:default_value) { "abc" } - it "returns the values with the first option being a nil 'Choose' option" do - expect(dialog_field.values).to eq([[nil, ""], %w(test test), %w(abc abc)]) + context "when the field is required" do + let(:required) { true } + + it "returns the values without the first option being a nil option" do + expect(dialog_field.values).to eq([%w(test test), %w(abc abc)]) + end + end + + context "when the field is not required" do + it "returns the values with the first option being a nil 'None' option" do + expect(dialog_field.values).to eq([[nil, ""], %w(test test), %w(abc abc)]) + end + end end end - context "when the field is not required" do - context "when the data type is an integer" do - let(:data_type) { "integer" } + context "when the data type is an integer" do + let(:data_type) { "integer" } + + before do + dialog_field.data_type = data_type + end + + context "when there is a default value that matches a value in the values list" do + let(:default_value) { "2" } + let(:values) { [%w(1 1), %w(2 2), %w(3 3)] } - before do - dialog_field.data_type = data_type + it "sets the default value to the matching value" do + dialog_field.values + expect(dialog_field.default_value).to eq("2") end - context "when there is a default value that matches a value in the values list" do - let(:default_value) { "2" } - let(:values) { [%w(1 1), %w(2 2), %w(3 3)] } + it "returns the values with the first option being a nil 'None' option" do + expect(dialog_field.values).to eq([[nil, ""], %w(1 1), %w(2 2), %w(3 3)]) + end + end - it "sets the default value to the matching value" do - dialog_field.values - expect(dialog_field.default_value).to eq("2") - end + context "when there is a default value that does not match a value in the values list" do + let(:default_value) { "4" } + let(:values) { [%w(1 1), %w(2 2), %w(3 3)] } - it "returns the values with the first option being a nil 'None' option" do - expect(dialog_field.values).to eq([[nil, ""], %w(1 1), %w(2 2), %w(3 3)]) - end + it "sets the default value to nil" do + dialog_field.values + expect(dialog_field.default_value).to eq(nil) end - context "when there is a default value that does not match a value in the values list" do - let(:default_value) { "4" } - let(:values) { [%w(1 1), %w(2 2), %w(3 3)] } + it "returns the values with the first option being a nil 'None' option" do + expect(dialog_field.values).to eq([[nil, ""], %w(1 1), %w(2 2), %w(3 3)]) + end + end + + context "when the default value is nil" do + let(:default_value) { nil } - it "sets the default value to nil" do - dialog_field.values - expect(dialog_field.default_value).to eq(nil) + context "when the field is required" do + let(:required) { true } + + it "returns the values with the first option being a nil 'Choose' option" do + expect(dialog_field.values).to eq([[nil, ""], %w(test test), %w(abc abc)]) end + end + context "when the field is not required" do it "returns the values with the first option being a nil 'None' option" do - expect(dialog_field.values).to eq([[nil, ""], %w(1 1), %w(2 2), %w(3 3)]) + expect(dialog_field.values).to eq([[nil, ""], %w(test test), %w(abc abc)]) end end end