Skip to content

Commit

Permalink
Adjust how default_value is calculated for multi-value drop downs
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed May 21, 2018
1 parent 85b25a1 commit b73e8f6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
22 changes: 22 additions & 0 deletions app/models/dialog_field_drop_down_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ def automate_key_name

private

def determine_selected_value
coerce_default_value_into_proper_format if dynamic? && force_multi_value

super
end

def use_first_value_as_default
self.default_value = if force_multi_value
[].to_json
else
sort_data(@raw_values).first.try(:first)
end
end

def default_value_included?(values_list)
if force_multi_value
return false if default_value.blank?
Expand All @@ -69,4 +83,12 @@ def default_value_included?(values_list)
super(values_list)
end
end

def coerce_default_value_into_proper_format
unless JSON.parse(default_value).kind_of?(Array)
self.default_value = Array.wrap(default_value).to_json
end
rescue JSON::ParserError
self.default_value = Array.wrap(default_value).to_json
end
end
3 changes: 0 additions & 3 deletions app/models/dialog_field_sorted_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def sort_data(data_to_sort)
end

def determine_selected_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
56 changes: 54 additions & 2 deletions spec/models/dialog_field_drop_down_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,63 @@

context "when the values returned contain a nil" do
before do
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return([[nil, "Choose something!"]])
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return(
[[nil, "Choose something!"], %w(1 one), %w(2 two), %w(abc def)]
)
end

context "when it is a multiselect" do
before do
dialog_field.force_multi_value = true
end

context "when the default value is included" do
before do
dialog_field.default_value = "[\"1\"]"
end

it "keeps the default" do
dialog_field.values
expect(dialog_field.default_value).to eq("[\"1\"]")
end
end

context "when the default value is included but is not an array" do
before do
dialog_field.default_value = "1"
end

it "keeps the default" do
dialog_field.values
expect(dialog_field.default_value).to eq("[\"1\"]")
end
end

context "when the default value is included but is not valid json" do
before do
dialog_field.default_value = "abc"
end

it "keeps the default" do
dialog_field.values
expect(dialog_field.default_value).to eq("[\"abc\"]")
end
end

context "when the default value is not included" do
before do
dialog_field.default_value = "[\"3\"]"
end

it "selects nothing" do
dialog_field.values
expect(dialog_field.default_value).to eq("[]")
end
end
end

it "returns the values from automate" do
expect(dialog_field.values).to eq([[nil, "Choose something!"]])
expect(dialog_field.values).to eq([[nil, "Choose something!"], %w(1 one), %w(2 two), %w(abc def)])
end
end
end
Expand Down
23 changes: 0 additions & 23 deletions spec/models/dialog_field_sorted_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,6 @@
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)
Expand Down

0 comments on commit b73e8f6

Please sign in to comment.