From 6582a875c6e02f3ea569478c55f18097921ace56 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 10 Aug 2023 11:04:48 +0200 Subject: [PATCH 1/2] Do not display "Producers" selector if only one is possible Fix #11274 Co-Authored-By: David Cook --- app/views/admin/products_v3/_filters.html.haml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index e83f0587fc..76539fbc29 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -2,9 +2,10 @@ .query .search-input = text_field_tag :search_term, search_term, placeholder: t('.search_products') - .producers - .label= t('.producers.label') - = select_tag :producer_id, options_for_select(producer_options, producer_id), include_blank: t('.all_producers'), "data-controller": "tom-select", "data-tom-select-options-value": '{ "plugins": [] }', class: "fullwidth" + - if producer_options.any? + .producers + .label= t('.producers.label') + = select_tag :producer_id, options_for_select(producer_options, producer_id), include_blank: t('.all_producers'), "data-controller": "tom-select", "data-tom-select-options-value": '{ "plugins": [] }', class: "fullwidth" .categories .label= t('.categories.label') = select_tag :category_id, options_for_select(category_options, category_id), include_blank: t('.all_categories'), "data-controller": "tom-select", "data-tom-select-options-value": '{ "plugins": [] }', class: "fullwidth" From 31d87ca08785829ad89ecb50912c92c0e421478f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 14 Aug 2023 09:47:57 +1000 Subject: [PATCH 2/2] Show producer filter only when multiple options Also introducing a view spec here because the code was incorrectly refactored before. The execution of the view spec takes only 100ms which is much faster than a system spec. --- .../admin/products_v3/_filters.html.haml | 2 +- .../products_v3/_filters.html.haml_spec.rb | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 spec/views/admin/products_v3/_filters.html.haml_spec.rb diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index 76539fbc29..9f6947a8ca 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -2,7 +2,7 @@ .query .search-input = text_field_tag :search_term, search_term, placeholder: t('.search_products') - - if producer_options.any? + - if producer_options.many? .producers .label= t('.producers.label') = select_tag :producer_id, options_for_select(producer_options, producer_id), include_blank: t('.all_producers'), "data-controller": "tom-select", "data-tom-select-options-value": '{ "plugins": [] }', class: "fullwidth" diff --git a/spec/views/admin/products_v3/_filters.html.haml_spec.rb b/spec/views/admin/products_v3/_filters.html.haml_spec.rb new file mode 100644 index 0000000000..fa2943502a --- /dev/null +++ b/spec/views/admin/products_v3/_filters.html.haml_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe "admin/products_v3/_filters.html.haml" do + subject { render } + + let(:locals) do + { + search_term: "", + producer_options: [], + producer_id: nil, + category_options: [], + category_id: nil, + } + end + + it "shows the producer filter when there are options" do + allow(view).to receive_messages locals.merge( + producer_options: [ + ["Ada's Apples", 1], + ["Ben's Bananas", 2], + ], + ) + + is_expected.to have_content "Producers" + is_expected.to have_select "producer_id", options: [ + "All producers", + "Ada's Apples", + "Ben's Bananas", + ], selected: nil + end + + it "doesn't show the producer filter when there's only one option" do + allow(view).to receive_messages locals.merge( + producer_options: [ + ["Ada's Apples", 1], + ], + ) + + is_expected.to have_no_content "Producers" + is_expected.to have_no_select "producer_id" + end +end