From 0cc450e81583b2da47c69f8824927ca52a1a4945 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 25 Aug 2023 09:31:38 +1000 Subject: [PATCH] Allow Vouchers feature per enterprise I also kept the per-user check to be backwards-compatible. Some instances have that enabled already. And for early testing, you may want to test with only one user first before all customer see it. --- app/views/admin/enterprises/_form.html.haml | 2 +- app/views/admin/shared/_side_menu.html.haml | 2 +- app/views/split_checkout/_payment.html.haml | 2 +- lib/open_food_network/feature_toggle.rb | 1 + spec/lib/open_food_network/feature_toggle_spec.rb | 9 +++++++++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/views/admin/enterprises/_form.html.haml b/app/views/admin/enterprises/_form.html.haml index 87aeaa2d29..acc2ec9c16 100644 --- a/app/views/admin/enterprises/_form.html.haml +++ b/app/views/admin/enterprises/_form.html.haml @@ -10,7 +10,7 @@ %legend= t(".#{ item[:name] }.legend") - when 'vouchers' - - if feature?(:vouchers, spree_current_user) + - if feature?(:vouchers, spree_current_user) || feature?(:vouchers, @enterprise) %fieldset.alpha.no-border-bottom{ id: "#{item[:name]}_panel", data: { "tabs-and-panels-target": "panel" }} %legend= t(".#{ item[:form_name] || item[:name] }.legend") = render "admin/enterprises/form/#{ item[:form_name] || item[:name] }", f: f diff --git a/app/views/admin/shared/_side_menu.html.haml b/app/views/admin/shared/_side_menu.html.haml index f9f38610bd..f0e811a4a3 100644 --- a/app/views/admin/shared/_side_menu.html.haml +++ b/app/views/admin/shared/_side_menu.html.haml @@ -1,7 +1,7 @@ .side_menu#side_menu - if @enterprise - enterprise_side_menu_items(@enterprise).each do |item| - - next if !item[:show] || (item[:name] == 'vouchers' && !feature?(:vouchers, spree_current_user)) + - next if !item[:show] || (item[:name] == 'vouchers' && !(feature?(:vouchers, spree_current_user) || feature?(:vouchers, @enterprise))) %a.menu_item{ href: item[:href] || "##{item[:name]}_panel", id: item[:name], data: { action: "tabs-and-panels#changeActivePanel tabs-and-panels#changeActiveTab", "tabs-and-panels-target": "tab" }, class: item[:selected] } %i{ class: item[:icon_class] } %span= t(".enterprise.#{item[:name] }") diff --git a/app/views/split_checkout/_payment.html.haml b/app/views/split_checkout/_payment.html.haml index 675fbfd820..18eea01bd0 100644 --- a/app/views/split_checkout/_payment.html.haml +++ b/app/views/split_checkout/_payment.html.haml @@ -1,5 +1,5 @@ .medium-6#checkout-payment-methods - - if feature?(:vouchers, spree_current_user) && @order.distributor.vouchers.present? + - if (feature?(:vouchers, spree_current_user) || feature?(:vouchers, @order.distributor)) && @order.distributor.vouchers.present? %div.checkout-substep = render partial: "split_checkout/voucher_section", locals: { order: @order, voucher_adjustment: @order.voucher_adjustments.first } diff --git a/lib/open_food_network/feature_toggle.rb b/lib/open_food_network/feature_toggle.rb index 9b5b5f1865..a718ef58eb 100644 --- a/lib/open_food_network/feature_toggle.rb +++ b/lib/open_food_network/feature_toggle.rb @@ -31,6 +31,7 @@ module OpenFoodNetwork DESC "vouchers" => <<~DESC, Add voucher functionality. Voucher can be managed via Enterprise settings. + This is activated per enterprise. Enter actors as Enterprise;1234. DESC "invoices" => <<~DESC, Preserve the state of generated invoices and enable multiple invoice numbers instead of only one live-updating invoice. diff --git a/spec/lib/open_food_network/feature_toggle_spec.rb b/spec/lib/open_food_network/feature_toggle_spec.rb index 4d88d63079..8260bda0c0 100644 --- a/spec/lib/open_food_network/feature_toggle_spec.rb +++ b/spec/lib/open_food_network/feature_toggle_spec.rb @@ -14,6 +14,15 @@ describe OpenFoodNetwork::FeatureToggle do Flipper.enable(:foo) expect(feature_toggle.enabled?(:foo)).to be true end + + it "can be activated per enterprise" do + enterprise = Enterprise.new(id: 5) + + Flipper.enable(:foo, enterprise) + + expect(feature_toggle.enabled?(:foo)).to eq false + expect(feature_toggle.enabled?(:foo, enterprise)).to eq true + end end describe ".setup!" do