Skip to content

Commit

Permalink
Add restrictions on when to add a 'nil' option for sorted item fields
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Jul 3, 2017
1 parent d3e8e83 commit b42c110
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions app/models/dialog_field_sorted_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
81 changes: 54 additions & 27 deletions spec/models/dialog_field_sorted_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b42c110

Please sign in to comment.