From 714f1d4729fc5c1f4d5d101967e1809f7ded3baa Mon Sep 17 00:00:00 2001 From: Harpreet Kataria Date: Thu, 16 Mar 2017 18:49:41 +0530 Subject: [PATCH] Merge pull request #694 from eclarizio/BZ1428133-continuation Fix for TagControl issues after previous BZ1428133 fix (cherry picked from commit 91643a2dee299ebe0180e4b3d45a1cfbeecb9094) https://bugzilla.redhat.com/show_bug.cgi?id=1430937 --- app/helpers/application_helper/dialogs.rb | 18 +++++++-- .../_dialog_sample.html.haml | 10 ++--- .../_tag_field_values.html.haml | 2 +- .../shared/dialogs/_dialog_field.html.haml | 14 ++----- .../application_helper/dialogs_spec.rb | 37 +++++++++++++++++-- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/app/helpers/application_helper/dialogs.rb b/app/helpers/application_helper/dialogs.rb index a93b4473701..29e4777f219 100644 --- a/app/helpers/application_helper/dialogs.rb +++ b/app/helpers/application_helper/dialogs.rb @@ -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] diff --git a/app/views/miq_ae_customization/_dialog_sample.html.haml b/app/views/miq_ae_customization/_dialog_sample.html.haml index 1d36df91c68..e9c5db7fe42 100644 --- a/app/views/miq_ae_customization/_dialog_sample.html.haml +++ b/app/views/miq_ae_customization/_dialog_sample.html.haml @@ -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"); diff --git a/app/views/miq_ae_customization/_tag_field_values.html.haml b/app/views/miq_ae_customization/_tag_field_values.html.haml index 1a47940d95b..1ff0fdc17fa 100644 --- a/app/views/miq_ae_customization/_tag_field_values.html.haml +++ b/app/views/miq_ae_customization/_tag_field_values.html.haml @@ -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] diff --git a/app/views/shared/dialogs/_dialog_field.html.haml b/app/views/shared/dialogs/_dialog_field.html.haml index 4c4f8d53c25..2dd17ee1e31 100644 --- a/app/views/shared/dialogs/_dialog_field.html.haml +++ b/app/views/shared/dialogs/_dialog_field.html.haml @@ -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)}')); diff --git a/spec/helpers/application_helper/dialogs_spec.rb b/spec/helpers/application_helper/dialogs_spec.rb index eeebe743566..037ad9d10fe 100644 --- a/spec/helpers/application_helper/dialogs_spec.rb +++ b/spec/helpers/application_helper/dialogs_spec.rb @@ -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