Prefer a concern that set a variable instead of a global helper

Therefor, for the right controllers, simply implements:

```
include WhiteLabel

before_action :hide_ofn_navigation, only: [:show, :edit]
```

This is mort robust, since we're working in a controller level, not parsing URLs...
This commit is contained in:
Jean-Baptiste Bellet
2023-03-17 10:58:56 +01:00
committed by David Cook
parent 5279be4dc6
commit 4ad2a1a723
8 changed files with 33 additions and 23 deletions

View File

@@ -5,6 +5,7 @@ require 'open_food_network/address_finder'
class CheckoutController < ::BaseController
include OrderStockCheck
include OrderCompletion
include WhiteLabel
layout 'darkswarm'
@@ -27,6 +28,8 @@ class CheckoutController < ::BaseController
before_action :associate_user
before_action :check_authorization
before_action :hide_ofn_navigation, only: :edit
helper 'spree/orders'
def edit; end

View File

@@ -0,0 +1,22 @@
# frozen_string_literal: true
module WhiteLabel
extend ActiveSupport::Concern
include EnterprisesHelper
def hide_ofn_navigation
# if the distributor has the hide_ofn_navigation preference set to true
# then we should hide the OFN navigation
@hide_ofn_navigation = distributor.preferred_hide_ofn_navigation
end
private
def distributor
return current_distributor unless request.path.start_with?("/orders/")
# if we are on an order confirmation page,
# we need to get the distributor from the order, not the current one
Spree::Order.find_by(number: params[:id]).distributor
end
end

View File

@@ -7,6 +7,7 @@ class EnterprisesController < BaseController
helper Spree::ProductsHelper
include OrderCyclesHelper
include SerializerHelper
include WhiteLabel
protect_from_forgery except: :check_permalink
@@ -14,6 +15,7 @@ class EnterprisesController < BaseController
prepend_before_action :set_order_cycles, :require_distributor_chosen, :reset_order, only: :shop
before_action :clean_permalink, only: :check_permalink
before_action :hide_ofn_navigation, only: :shop
respond_to :js, only: :permalink_checker

View File

@@ -10,6 +10,7 @@ class SplitCheckoutController < ::BaseController
include CheckoutCallbacks
include OrderCompletion
include CablecarResponses
include WhiteLabel
helper 'terms_and_conditions'
helper 'checkout'
@@ -18,6 +19,7 @@ class SplitCheckoutController < ::BaseController
helper OrderHelper
before_action :set_checkout_redirect
before_action :hide_ofn_navigation, only: [:edit, :update]
def edit
redirect_to_step_based_on_order unless params[:step]

View File

@@ -5,6 +5,7 @@ module Spree
include OrderCyclesHelper
include Rails.application.routes.url_helpers
include CablecarResponses
include WhiteLabel
layout 'darkswarm'
@@ -22,6 +23,7 @@ module Spree
prepend_before_action :require_distributor_chosen, only: :edit
before_action :check_hub_ready_for_checkout, only: :edit
before_action :check_at_least_one_line_item, only: :update
before_action :hide_ofn_navigation, only: [:show, :edit]
def show
@order = Spree::Order.find_by!(number: params[:id])

View File

@@ -87,26 +87,4 @@ module EnterprisesHelper
main_app.producers_url
end
end
def hide_ofn_navigation?
# if we are not on a shopfront, a cart page, checkout page or the order confirmation page
# then we should show the OFN navigation
# whatever the current distributor has set for the hide_ofn_navigation preference
return false unless current_distributor && current_page?(main_app.enterprise_shop_path(current_distributor)) || # shopfront
request.path.start_with?(main_app.checkout_path) || # checkout
current_page?(main_app.cart_path) || # cart
request.path.start_with?("/orders/") # order confirmation
distributor = if request.path.start_with?("/orders/")
# if we are on an order confirmation page,
# we need to get the distributor from the order, not the current one
Spree::Order.find_by(number: params[:id]).distributor
else
current_distributor
end
# if the current distributor has the hide_ofn_navigation preference set to true
# then we should hide the OFN navigation
distributor.preferred_hide_ofn_navigation
end
end

View File

@@ -10,7 +10,7 @@
= t 'powered_by'
%a{href: '/'}
= t 'title'
- unless hide_ofn_navigation?
- unless @hide_ofn_navigation
%ul.nav-main-menu
- [*1..7].each do |menu_number|
- menu_name = "menu_#{menu_number}"

View File

@@ -91,6 +91,7 @@ describe Spree::OrdersController, type: :controller do
it "redirects to shop when order is empty" do
allow(controller).to receive(:current_distributor).and_return(distributor)
allow(distributor).to receive(:preferred_hide_ofn_navigation).and_return false
allow(controller).to receive(:current_order_cycle).and_return(order_cycle)
allow(controller).to receive(:current_order).and_return order
allow(order).to receive_message_chain(:line_items, :empty?).and_return true