From c45a3c2303bb1cf483b30be5221db03eb95f1fa9 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Mon, 6 Jan 2020 00:08:52 -0300 Subject: [PATCH] remove group tabs from shop view when there are no groups --- app/helpers/shop_helper.rb | 25 +++++++++++++++---- spec/helpers/shop_helper_spec.rb | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/app/helpers/shop_helper.rb b/app/helpers/shop_helper.rb index 0ca473e44f..96e4a58857 100644 --- a/app/helpers/shop_helper.rb +++ b/app/helpers/shop_helper.rb @@ -20,12 +20,27 @@ module ShopHelper ) end - def shop_tabs + def base_shop_tabs(column_sizes) [ - { name: 'about', title: t(:shopping_tabs_about, distributor: current_distributor.name), cols: 6 }, - { name: 'producers', title: t(:label_producers), cols: 2 }, - { name: 'contact', title: t(:shopping_tabs_contact), cols: 2 }, - { name: 'groups', title: t(:label_groups), cols: 2 }, + { name: 'about', cols: column_sizes[0], + title: t(:shopping_tabs_about, distributor: current_distributor.name) }, + { name: 'producers', cols: column_sizes[1], + title: t(:label_producers) }, + { name: 'contact', cols: column_sizes[2], + title: t(:shopping_tabs_contact) } ] end + + def tabs_with_groups + tabs = base_shop_tabs([6, 2, 2]) + tabs << { name: 'groups', title: t(:label_groups), cols: 2 } + end + + def tabs_without_groups + base_shop_tabs([4, 4, 4]) + end + + def shop_tabs + current_distributor.groups.present? ? tabs_with_groups : tabs_without_groups + end end diff --git a/spec/helpers/shop_helper_spec.rb b/spec/helpers/shop_helper_spec.rb index 0cc4f546d3..fcd6bf7d65 100644 --- a/spec/helpers/shop_helper_spec.rb +++ b/spec/helpers/shop_helper_spec.rb @@ -7,4 +7,46 @@ describe ShopHelper, type: :helper do expect(helper.order_cycles_name_and_pickup_times([o1])).to eq([[helper.pickup_time(o1), o1.id]]) end + + describe "shop_tabs" do + context "distributor with groups" do + let(:group) { create(:enterprise_group) } + let(:distributor) { create(:distributor_enterprise, groups: [group]) } + let(:expectation) { + [ + { name: 'about', title: t(:shopping_tabs_about, distributor: distributor.name), cols: 6 }, + { name: 'producers', title: t(:label_producers), cols: 2 }, + { name: 'contact', title: t(:shopping_tabs_contact), cols: 2 }, + { name: 'groups', title: t(:label_groups), cols: 2 } + ] + } + + before do + allow(helper).to receive(:current_distributor).and_return distributor + end + + it "should return the groups tab" do + expect(helper.shop_tabs).to eq(expectation) + end + end + + context "distributor without groups" do + let(:distributor) { create(:distributor_enterprise) } + let(:expectation) { + [ + { name: 'about', title: t(:shopping_tabs_about, distributor: distributor.name), cols: 4 }, + { name: 'producers', title: t(:label_producers), cols: 4 }, + { name: 'contact', title: t(:shopping_tabs_contact), cols: 4 } + ] + } + + before do + allow(helper).to receive(:current_distributor).and_return distributor + end + + it "should not return the groups tab" do + expect(helper.shop_tabs).to eq(expectation) + end + end + end end