Skip to content

Commit

Permalink
Merge pull request ManageIQ#694 from eclarizio/BZ1428133-continuation
Browse files Browse the repository at this point in the history
Fix for TagControl issues after previous BZ1428133 fix
(cherry picked from commit 91643a2)

https://bugzilla.redhat.com/show_bug.cgi?id=1430937
  • Loading branch information
h-kataria authored and simaishi committed Mar 16, 2017
1 parent 600e87c commit 714f1d4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
18 changes: 14 additions & 4 deletions app/helpers/application_helper/dialogs.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
module ApplicationHelper::Dialogs
def dialog_dropdown_select_values(field, _selected_value, category_tags = nil)
values = []
def dialog_dropdown_select_values(field)
values = field.values
if field.type.include?("DropDown")
values += field.values.collect(&:reverse)
values.collect!(&:reverse)
elsif field.type.include?("TagControl")
values += category_tags
values.map! { |category| [category[:description], category[:id]] }
end
values
end

def category_tags(category_id)
classification = Classification.find_by(:id => category_id)
return [] if classification.nil?

available_tags = classification.entries.collect do |category|
{:name => category.name, :description => category.description}
end
available_tags
end

def disable_check_box?
category = DialogFieldTagControl.allowed_tag_categories.detect { |cat| cat[:id].to_s == @edit[:field_category] }
category && category[:single_value]
Expand Down
10 changes: 3 additions & 7 deletions app/views/miq_ae_customization/_dialog_sample.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@
- when "DialogFieldButton"
= button_tag(_('Save'), :disabled => true)
- when "DialogFieldTagControl"
- category_tags = DialogFieldTagControl.category_tags(field.category).map do |cat|
- [cat[:description], cat[:id]]
- if field.single_value?
= select_tag('field.id', options_for_select(category_tags),
:prompt => field.required? ? "<#{_('Choose')}>" : "<#{_('None')}>")
- else
= select_tag('field.id', options_for_select(category_tags), :multiple => true)
- multiple = field.single_value? ? {} : {:multiple => true}
= select_tag('field.id', options_for_select(dialog_dropdown_select_values(field)), multiple)

:javascript
miq_tabs_init("#dialog_tabs");
2 changes: 1 addition & 1 deletion app/views/miq_ae_customization/_tag_field_values.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%th= _('Value')
%th= _('Description')
%tbody
- DialogFieldTagControl.category_tags(@edit[:field_category]).each do |cat|
- category_tags(@edit[:field_category]).each do |cat|
%tr
%td= cat[:name]
%td= cat[:description]
14 changes: 4 additions & 10 deletions app/views/shared/dialogs/_dialog_field.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,11 @@
= button_tag(_("Save"), :class => edit ? 'btn btn-primary' : 'btn btn-primary disabled')

- when 'DialogFieldTagControl'
- category_tags = DialogFieldTagControl.category_tags(field.category).map { |cat| [cat[:description], cat[:id]] }
- if edit
- if field.single_value?
- select_values = dialog_dropdown_select_values(field, wf.value(field.name), category_tags)
= select_tag(field.name,
options_for_select(select_values, wf.value(field.name)),
drop_down_options(field, url))
- else
= select_tag(field.name,
options_for_select(category_tags, wf.value(field.name)),
drop_down_options(field, url).merge(:multiple => true))
- multiple = field.single_value? ? {} : {:multiple => true}
= select_tag(field.name,
options_for_select(dialog_dropdown_select_values(field), wf.value(field.name)),
drop_down_options(field, url).merge(multiple))

:javascript
dialogFieldRefresh.initializeDialogSelectPicker('#{field.name}', undefined, '#{url}', JSON.parse('#{j(auto_refresh_options.to_json)}'));
Expand Down
37 changes: 33 additions & 4 deletions spec/helpers/application_helper/dialogs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,50 @@

describe "#dialog_dropdown_select_values" do
let(:dialog_field) { instance_double("DialogFieldDropDownList", :values => values, :type => type) }
let(:values) { [%w(cat Cat), %w(dog Dog)] }

context "when the field type includes drop down" do
let(:type) { "BananaDropDown" }
let(:values) { [%w(cat Cat), %w(dog Dog)] }

it "returns the values collected and reversed" do
expect(helper.dialog_dropdown_select_values(dialog_field, nil)).to eq(values.collect(&:reverse))
reversed_values = values.collect(&:reverse)
expect(helper.dialog_dropdown_select_values(dialog_field)).to eq(reversed_values)
end
end

context "when the field type includes tag control" do
let(:type) { "BananaTagControl" }
let(:values) { [{:description => "Cat", :id => 123}, {:description => "Dog", :id => 321}] }

it "returns the values with the passed in category tags" do
expect(helper.dialog_dropdown_select_values(dialog_field, nil, %w(category tags))).to eq(%w(category tags))
it "returns the values mapped to a description and id" do
expect(helper.dialog_dropdown_select_values(dialog_field)).to eq([["Cat", 123], ["Dog", 321]])
end
end
end

describe "#category_tags" do
before do
allow(Classification).to receive(:find_by).with(:id => 123).and_return(classification)
end

context "when the category exists with the given id" do
let(:classification) { instance_double("Classification", :entries => [entry1, entry2]) }
let(:entry1) { instance_double("Classification", :name => "cat", :description => "Cat") }
let(:entry2) { instance_double("Classification", :name => "dog", :description => "Dog") }

it "returns a list of entries by name and description" do
expect(helper.category_tags(123)).to eq([
{:name => "cat", :description => "Cat"},
{:name => "dog", :description => "Dog"}
])
end
end

context "when the category does not exist by the given id" do
let(:classification) { nil }

it "returns an empty array" do
expect(helper.category_tags(123)).to eq([])
end
end
end
Expand Down

0 comments on commit 714f1d4

Please sign in to comment.