Skip to content

Commit

Permalink
Merge pull request ManageIQ#15456 from eclarizio/BZ1462375
Browse files Browse the repository at this point in the history
Fix for <Choose> found as option in drop down service dialogs
(cherry picked from commit 0cbb33d)

https://bugzilla.redhat.com/show_bug.cgi?id=1478435
  • Loading branch information
gmcculloug authored and d-m-u committed Jun 6, 2018
1 parent 79be8c5 commit 39ad62f
Showing 3 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/models/dialog_field_radio_button.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 18 additions & 5 deletions app/models/dialog_field_sorted_item.rb
Original file line number Diff line number Diff line change
@@ -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, "<Choose>"]] : 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, "<None>"]]
end

def initial_required_values
[nil, "<Choose>"]
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
81 changes: 54 additions & 27 deletions spec/models/dialog_field_sorted_item_spec.rb
Original file line number Diff line number Diff line change
@@ -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, "<Choose>"], %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, "<None>"], %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, "<None>"], %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, "<None>"], %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, "<None>"], %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, "<Choose>"], %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, "<None>"], %w(1 1), %w(2 2), %w(3 3)])
expect(dialog_field.values).to eq([[nil, "<None>"], %w(test test), %w(abc abc)])
end
end
end

0 comments on commit 39ad62f

Please sign in to comment.