Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move breadcrumbs setting from views to controllers #3191

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions backend/app/controllers/concerns/spree/admin/breadcrumbs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Spree
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
module Admin
module Breadcrumbs
def add_breadcrumb(name, path = nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this methods protected so they don't become actions?

@admin_breadcrumbs ||= []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a Set, so that we do not get potential duplicates and gain some performance?

@admin_breadcrumbs << [name, path]
end

# Shared breadcrumbs

def set_user_breadcrumbs
add_breadcrumb plural_resource_name(Spree::LegacyUser), spree.admin_users_path
add_breadcrumb @user.email, edit_admin_user_url(@user) if @user && !@user.new_record?
end

def set_order_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Order), spree.admin_orders_path
add_breadcrumb "##{@order.number}", spree.edit_admin_order_path(@order) if @order && !@order.new_record?
end

def set_product_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Product), spree.admin_products_path
add_breadcrumb @product.name, spree.admin_product_path(@product) if @product && !@product.new_record?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
module Spree
module Admin
class AdjustmentReasonsController < ResourceController
before_action :set_breadcrumbs

private

def set_breadcrumbs
add_breadcrumb t('spree.settings')
add_breadcrumb t('spree.admin.tab.checkout')
add_breadcrumb plural_resource_name(Spree::AdjustmentReason), spree.admin_adjustment_reasons_path
add_breadcrumb @adjustment_reason.name if params[:id].present?
add_breadcrumb t('spree.new_adjustment_reason') if action_name == 'new'
end
end
end
end
8 changes: 8 additions & 0 deletions backend/app/controllers/spree/admin/adjustments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AdjustmentsController < ResourceController
skip_before_action :load_resource, only: [:toggle_state, :edit, :update, :destroy]

before_action :find_adjustment, only: [:destroy, :edit, :update]
before_action :set_breadcrumbs

helper_method :reasons_for

Expand Down Expand Up @@ -42,6 +43,13 @@ def reasons_for(_adjustment)
@adjustment.adjustment_reason
].flatten.compact.uniq.sort_by { |r| r.name.downcase }
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Adjustment), spree.admin_order_adjustments_path(@order)
add_breadcrumb t('spree.new_adjustment') if action_name == 'new'
add_breadcrumb "#{t('spree.actions.edit')} #{Spree::Adjustment.model_name.human}" if action_name == 'edit'
end
end
end
end
2 changes: 2 additions & 0 deletions backend/app/controllers/spree/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Spree
module Admin
class BaseController < Spree::BaseController
include Spree::Admin::Breadcrumbs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the before_action :set_breadcumbs here and add an empty method that gets called if the child class does not implement set_breadcrumbs? We might even have a "defaut breadcrumb" (build from the resource) for controllers. We could remove a lot of code then.

helper 'spree/admin/navigation'
layout '/spree/layouts/admin'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Spree
module Admin
class CancellationsController < Spree::Admin::BaseController
before_action :load_order, only: [:index, :short_ship]
before_action :set_breadcrumbs

def index
@inventory_units = @order.inventory_units.cancelable
Expand Down Expand Up @@ -41,6 +42,11 @@ def load_order
def model_class
Spree::OrderCancellations
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb t('spree.cancel_inventory')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CustomerReturnsController < ResourceController
before_action :parent # ensure order gets loaded to support our pseudo parent-child relationship
before_action :load_form_data, only: [:new, :edit]
before_action :build_return_items_from_params, only: [:create]
before_action :set_breadcrumbs
Copy link
Member

@tvdeyen tvdeyen May 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this before_action into the spree/admin/resource_controller

create.fails :load_form_data

def edit
Expand Down Expand Up @@ -71,6 +72,13 @@ def build_return_items_from_params
return_item
end.compact
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb plural_resource_name(Spree::CustomerReturn), spree.admin_order_customer_returns_url(@order)
add_breadcrumb @customer_return.number if params[:id].present?
add_breadcrumb t('spree.new_customer_return') if action_name == 'new'
end
end
end
end
7 changes: 7 additions & 0 deletions backend/app/controllers/spree/admin/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Spree
module Admin
class ImagesController < ResourceController
before_action :load_data
before_action :set_breadcrumbs

create.before :set_viewable
update.before :set_viewable
Expand All @@ -30,6 +31,12 @@ def set_viewable
@image.viewable_type = 'Spree::Variant'
@image.viewable_id = params[:image][:viewable_id]
end

def set_breadcrumbs
set_product_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Image), admin_product_images_path(@product)
add_breadcrumb @image.filename if params[:id].present?
end
end
end
end
7 changes: 7 additions & 0 deletions backend/app/controllers/spree/admin/log_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Spree
module Admin
class LogEntriesController < Spree::Admin::BaseController
before_action :find_order_and_payment
before_action :set_breadcrumbs

def index
Spree::Deprecation.warn 'Using a dedicated route for payment log entries ' \
Expand All @@ -18,6 +19,12 @@ def find_order_and_payment
@order = Spree::Order.where(number: params[:order_id]).first!
@payment = @order.payments.find(params[:payment_id])
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb Spree::Payment.model_name.human
add_breadcrumb plural_resource_name(Spree::LogEntry)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Spree
module Admin
class OptionTypesController < ResourceController
before_action :setup_new_option_value, only: :edit
before_action :set_breadcrumb

def update_values_positions
params[:positions].each do |id, index|
Expand Down Expand Up @@ -36,6 +37,12 @@ def set_available_option_types
Spree::OptionType.all
end
end

def set_breadcrumb
add_breadcrumb plural_resource_name(Spree::Product), spree.admin_products_path
add_breadcrumb plural_resource_name(Spree::OptionType), spree.admin_option_types_path
add_breadcrumb @option_type.name if params[:id].present?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CustomerDetailsController < Spree::Admin::BaseController
rescue_from Spree::Order::InsufficientStock, with: :insufficient_stock_error

before_action :load_order
before_action :set_breadcrumbs

def show
edit
Expand Down Expand Up @@ -75,6 +76,11 @@ def insufficient_stock_error
flash[:error] = t('spree.insufficient_stock_for_order')
redirect_to edit_admin_order_customer_url(@order)
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb t('spree.customer_details')
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions backend/app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class OrdersController < Spree::Admin::BaseController
before_action :initialize_order_events
before_action :load_order, only: [:edit, :update, :complete, :advance, :cancel, :resume, :approve, :resend, :unfinalize_adjustments, :finalize_adjustments, :cart, :confirm]
around_action :lock_order, only: [:update, :advance, :complete, :confirm, :cancel, :resume, :approve, :resend]
before_action :set_breadcrumbs

rescue_from Spree::Order::InsufficientStock, with: :insufficient_stock_error

Expand Down Expand Up @@ -186,6 +187,13 @@ def require_ship_address
redirect_to edit_admin_order_customer_url(@order)
end
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb t('spree.cart') if action_name == 'cart'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the breadcrumb into the cart action instead of this if

add_breadcrumb t('spree.confirm_order') if action_name == 'confirm'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, why not add the breadcrumb only in the confirm action?

add_breadcrumb plural_resource_name(Spree::Shipment) if action_name == 'edit'
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PaymentMethodsController < ResourceController
skip_before_action :load_resource, only: :create
before_action :load_payment_method_types, except: [:index]
before_action :validate_payment_method_type, only: [:create, :update]
before_action :set_breadcrumbs

respond_to :html

Expand Down Expand Up @@ -81,6 +82,14 @@ def validate_payment_method_type
def payment_method_params
params.require(:payment_method).permit!
end

def set_breadcrumbs
add_breadcrumb t('spree.settings')
add_breadcrumb plural_resource_name(Spree::Payment)
add_breadcrumb plural_resource_name(Spree::PaymentMethod), spree.admin_payment_methods_path
add_breadcrumb t('spree.new_payment_method') if action_name == 'new'
add_breadcrumb @payment_method.name if action_name == 'edit'
end
end
end
end
8 changes: 8 additions & 0 deletions backend/app/controllers/spree/admin/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PaymentsController < Spree::Admin::BaseController
before_action :load_payment_for_fire, only: :fire
before_action :load_data
before_action :require_bill_address, only: [:index]
before_action :set_breadcrumbs

respond_to :html

Expand Down Expand Up @@ -118,6 +119,13 @@ def insufficient_stock_error
flash[:error] = t('spree.insufficient_stock_for_order')
redirect_to new_admin_order_payment_url(@order)
end

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Payment), spree.admin_order_payments_path(@order)
add_breadcrumb t('spree.new_payment') if action_name == 'new'
add_breadcrumb @payment.payment_method.name if action_name == 'show'
end
end
end
end
11 changes: 11 additions & 0 deletions backend/app/controllers/spree/admin/prices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Admin
class PricesController < ResourceController
belongs_to 'spree/product', find_by: :slug

before_action :set_breadcrumbs

def index
params[:q] ||= {}

Expand All @@ -23,6 +25,15 @@ def index

def edit
end

private

def set_breadcrumbs
set_product_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Price), spree.admin_product_prices_url(@product)
add_breadcrumb t('spree.actions.edit') if action_name == 'edit'
add_breadcrumb t('spree.actions.new') if action_name == 'new'
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProductPropertiesController < ResourceController
before_action :find_properties
before_action :setup_property, only: :index, if: -> { can?(:create, model_class) }
before_action :setup_variant_property_rules, only: :index
before_action :set_breadcrumbs

private

Expand All @@ -24,6 +25,11 @@ def setup_variant_property_rules
@variant_property_rule = @product.find_variant_property_rule(@option_value_ids) || @product.variant_property_rules.build
@variant_property_rule.values.build if can?(:create, Spree::VariantPropertyRuleValue)
end

def set_breadcrumbs
set_product_breadcrumbs
add_breadcrumb plural_resource_name(Spree::ProductProperty)
end
end
end
end
7 changes: 7 additions & 0 deletions backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ProductsController < ResourceController
update.before :update_before
helper_method :clone_object_url
before_action :split_params, only: [:create, :update]
before_action :set_breadcrumbs

def show
redirect_to action: :edit
Expand Down Expand Up @@ -138,6 +139,12 @@ def variant_scope
def updating_variant_property_rules?
params[:product][:variant_property_rules_attributes].present?
end

def set_breadcrumbs
set_product_breadcrumbs
add_breadcrumb t('spree.product_details') if action_name == 'edit'
add_breadcrumb t('spree.new_product') if action_name == 'new'
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
module Spree
module Admin
class PromotionCategoriesController < ResourceController
before_action :set_breadcrumbs

private

def set_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Promotion), spree.admin_promotions_path
add_breadcrumb plural_resource_name(Spree::PromotionCategory), spree.admin_promotion_categories_path
add_breadcrumb @promotion_category.name if action_name == 'edit'
add_breadcrumb t('spree.new_promotion_category') if action_name == 'new'
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Admin
class PromotionCodeBatchesController < ResourceController
belongs_to 'spree/promotion'

before_action :set_breadcrumbs
create.after :build_promotion_code_batch

def download
Expand All @@ -25,6 +26,13 @@ def download
def build_promotion_code_batch
@promotion_code_batch.process
end

def set_breadcrumbs
add_breadcrumb plural_resource_name(Spree::Promotion), spree.admin_promotions_path
add_breadcrumb @promotion.name, spree.edit_admin_promotion_path(@promotion.id) if action_name == 'index'
add_breadcrumb @promotion.name, spree.admin_promotion_path(@promotion.id) if action_name == 'new'
add_breadcrumb plural_resource_name(Spree::PromotionCodeBatch)
end
end
end
end
Loading