Skip to content

Commit

Permalink
Use correct comparison for multi-value default value inclusion rules
Browse files Browse the repository at this point in the history
https://bugzilla.redhat.com/show_bug.cgi?id=1531316

Since the default value for a multi-value drop-down is a string,
comparing directly to the list of values in a drop-down did not work. It
needs to be converted to an array and then compare the overlap to see if
the default value is appropriate.
  • Loading branch information
eclarizio committed Feb 8, 2018
1 parent 58a6e56 commit e145bc3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
14 changes: 14 additions & 0 deletions app/models/dialog_field_drop_down_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ def automate_key_name
return super unless force_multi_value
MiqAeEngine.create_automation_attribute_array_key(super)
end

private

def default_value_included?(values_list)
if force_multi_value
return false unless default_value.present?
converted_values_list = values_list.collect { |value_pair| value_pair[0].send(value_modifier) }
converted_default_values = JSON.parse(default_value).collect { |value| value.send(value_modifier) }
overlap = converted_values_list & converted_default_values
return !overlap.empty?
else
super(values_list)
end
end
end
64 changes: 48 additions & 16 deletions spec/models/dialog_field_drop_down_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,65 @@
end
end

context "#initialize_with_values" do
before(:each) do
@df.values = [%w(3 X), %w(2 Y), %w(1 Z)]
@df.load_values_on_init = true
end
it "#automate_key_name" do
expect(@df.automate_key_name).to eq("dialog_drop_down_list")
end
end

describe "#initialize_with_values" do
let(:dialog_field) { described_class.new(:options => {:force_multi_value => force_multi_value}) }

before do
dialog_field.values = [%w(3 X), %w(2 Y), %w(1 Z)]
dialog_field.load_values_on_init = true
end

context "when force_multi_value is not true" do
let(:force_multi_value) { false }

it "uses the nil as the default value" do
@df.default_value = nil
@df.initialize_with_values({})
expect(@df.value).to eq(nil)
dialog_field.default_value = nil
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end

it "with default value" do
@df.default_value = "1"
@df.initialize_with_values({})
expect(@df.value).to eq("1")
dialog_field.default_value = "1"
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq("1")
end

it "uses the nil when there is a non-matching default value" do
@df.default_value = "4"
@df.initialize_with_values({})
expect(@df.value).to eq(nil)
dialog_field.default_value = "4"
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end
end

it "#automate_key_name" do
expect(@df.automate_key_name).to eq("dialog_drop_down_list")
context "when force_multi_value is true" do
let(:force_multi_value) { true }

context "when the default values are included in the value list" do
before do
dialog_field.default_value = "[\"3\", \"2\"]"
end

it "uses the default value" do
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq("[\"3\", \"2\"]")
end
end

context "when the default values are not included in the value list" do
before do
dialog_field.default_value = "[\"4\"]"
end

it "uses nil" do
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end
end
end
end

Expand Down

0 comments on commit e145bc3

Please sign in to comment.