Skip to content

Commit

Permalink
Merge pull request #17272 from romanblanco/bz1559030-v2
Browse files Browse the repository at this point in the history
Forcing default_value to an array, if the dynamic dropdown is multiselect
  • Loading branch information
gmcculloug authored Apr 17, 2018
2 parents 9f7f72c + 2cde07d commit 41f514d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/dialog_field_radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def initial_values
[[nil, "<None>"]]
end

def force_multi_value
false
end

private

def raw_values
Expand Down
3 changes: 3 additions & 0 deletions app/models/dialog_field_sorted_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def sort_data(data_to_sort)
end

def determine_selected_default_value
if dynamic? && force_multi_value && !default_value.kind_of?(Array)
self.default_value = Array.wrap(default_value)
end
use_first_value_as_default unless default_value_included?(@raw_values)
self.value ||= default_value.nil? && data_type == "integer" ? nil : default_value.send(value_modifier)
end
Expand Down
48 changes: 46 additions & 2 deletions spec/models/dialog_field_sorted_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
let(:default_value) { "test2" }

context "when show_refresh_button is true" do
before(:each) do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
let(:show_refresh_button) { true }

context "when load_values_on_init is true" do
Expand Down Expand Up @@ -59,6 +62,9 @@

context "when show_refresh_button is false" do
let(:show_refresh_button) { false }
before(:each) do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end

context "when load_values_on_init is true" do
let(:load_values_on_init) { true }
Expand Down Expand Up @@ -148,13 +154,47 @@
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return(values)
end

context "when the default_value is included in the list of returned values" do
context "when the force_multi_value is set to false" do
let(:default_value) { "abc" }
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end

it "sets the default value" do
it "sets the default_value" do
dialog_field.values
expect(dialog_field.default_value).to eq("abc")
end
end

context "when the force_multi_value is set to true" do
let(:default_value) { "abc" }
before do
allow(dialog_field).to receive(:force_multi_value).and_return(true)
# FIXME (rblanco): this is a workaround for the tested class being
# DialogFieldSortedItem and not DialogFieldDropDownList as it is
# in real usecase
#
# The test should be moved to dialog_field_drop_down_list_spec.rb, as
# https://github.com/ManageIQ/manageiq/pull/17272#discussion_r180468970
# says.
allow(dialog_field).to receive(:default_value_included?).and_return(true)
end

it "sets the default_value" do
dialog_field.values
# while reproducing the BZ, the method
# `determine_selected_default_value` is called twice,
# not just once - could be a difference here
expect(dialog_field.default_value).to eq("[\"abc\"]")
end
end

context "when the default_value is included in the list of returned values" do
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end

let(:default_value) { "abc" }

it "sets the value to the default value" do
dialog_field.values
Expand All @@ -163,6 +203,10 @@
end

context "when the default_value is not included in the list of returned values" do
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end

let(:default_value) { "123" }

it "sets the default value to the first value" do
Expand Down

0 comments on commit 41f514d

Please sign in to comment.