Skip to content

Commit

Permalink
Merge pull request #1006 from lgalis/display_second_value_for_dropdow…
Browse files Browse the repository at this point in the history
…n_in_dialog

Show the display value for the dropdown in the automation dialog
(cherry picked from commit 612d662)

https://bugzilla.redhat.com/show_bug.cgi?id=1442232
  • Loading branch information
Dan Clarizio authored and simaishi committed Apr 13, 2017
1 parent e19432a commit 04030c6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
16 changes: 16 additions & 0 deletions app/helpers/application_helper/dialogs.rb
Expand Up @@ -9,6 +9,22 @@ def dialog_dropdown_select_values(field)
values
end

def dialog_dropdown_selected_value(field)
if !field.values || field.values.empty?
return field.value
end

values = field.values.to_h
result = field.value.split(',').collect do |val|
if values.include?(val)
values.to_h[val]
else
val
end
end
result.join(',')
end

def category_tags(category_id)
classification = Classification.find_by(:id => category_id)
return [] if classification.nil?
Expand Down
Expand Up @@ -3,7 +3,7 @@
- selected ||= wf.value(field.name)
= select_tag(field.name, options_for_select(select_values,
selected),
drop_down_options(field, url))
drop_down_options(field, url))

:javascript
dialogFieldRefresh.initializeDialogSelectPicker(
Expand Down Expand Up @@ -35,6 +35,5 @@
dialogFieldRefresh.triggerAutoRefresh(JSON.parse('#{j(auto_refresh_options.to_json)}'));
});
});

- else
= h(field.value.nil? ? "<None>" : field.value)
= h(field.value.nil? ? "<None>" : dialog_dropdown_selected_value(field))
46 changes: 46 additions & 0 deletions spec/helpers/application_helper/dialogs_spec.rb
Expand Up @@ -33,6 +33,52 @@
end
end

describe "#dialog_dropdown_selected_value" do
let(:dialog_field) { instance_double("DialogFieldDropDownList", :values => values, :type => type, :value => value) }
let(:type) { "OneDropDown" }
let(:values) { [%w(key1 Val1), %w(key2 Val2)] }
let(:value) { nil }

context "when the the selected item exists in the values list" do
let(:value) { 'key2' }
it "returns the value for the selected item" do
expect(helper.dialog_dropdown_selected_value(dialog_field)).to eq('Val2')
end
end

context "when the the selected item does not exists in the values list" do
let(:value) { 'oldkey' }
it "returns the key value for the selected item" do
expect(helper.dialog_dropdown_selected_value(dialog_field)).to eq('oldkey')
end
end

context "shows a list of display values for multiselect" do
let(:values) { [%w(key1 Val1), %w(key2 Val2), %w(key3 Val3), %w(key4 Val4)] }
let(:value) { 'key2,key4' }
it "returns the key value for the selected item" do
expect(helper.dialog_dropdown_selected_value(dialog_field)).to eq('Val2,Val4')
end
end

context "shows a list of display values and keys for multiselect when some keys not in values" do
let(:values) { [%w(key1 Val1), %w(key2 Val2), %w(key3 Val3), %w(key4 Val4)] }
let(:value) { 'oldkey,key4' }
it "returns the key value for the selected item" do
expect(helper.dialog_dropdown_selected_value(dialog_field)).to eq('oldkey,Val4')
end
end

context "when the drop down option list is empty" do
let(:values) {}
let(:value) { 'key' }

it "returns the field value if the options list is empty" do
expect(helper.dialog_dropdown_selected_value(dialog_field)).to eq('key')
end
end
end

describe "#category_tags" do
before do
allow(Classification).to receive(:find_by).with(:id => 123).and_return(classification)
Expand Down
@@ -0,0 +1,18 @@
describe "shared/dialogs/dialog_field_drop_down_list.html.haml" do
context 'display field value' do
before do
@edit = false
attributes = {:values => [[nil, "<Choose>"], %w(Value1 Desc1), %w(Value2 Desc2)]}
value = 'Value2'
@field = DialogFieldDropDownList.new(:id => 1,
:name => 'Options List Field',
:type => 'DialogFieldDropDownList',
:attributes => attributes, :value => value)
end

it 'shows the display value for the dropdown field' do
render :partial => "/shared/dialogs/dialog_field_drop_down_list.html.haml", :locals => {:edit => @edit, :field => @field}
expect(rendered).to eq("Desc2\n")
end
end
end

0 comments on commit 04030c6

Please sign in to comment.