From 8e33437fbb1f15a855b5a88b5813270bb2e3f8ae Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 20 Sep 2019 15:59:24 +0100 Subject: [PATCH] MErge ship method controller decorator with the controller that came from spree --- .../admin/shipping_methods_controller.rb | 34 +++++++++++++++ .../shipping_methods_controller_decorator.rb | 42 ------------------- 2 files changed, 34 insertions(+), 42 deletions(-) delete mode 100644 app/controllers/spree/admin/shipping_methods_controller_decorator.rb diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index 41939927ea..5434e829f3 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -4,6 +4,32 @@ module Spree before_filter :load_data, except: [:index] before_filter :set_shipping_category, only: [:create, :update] before_filter :set_zones, only: [:create, :update] + before_filter :do_not_destroy_referenced_shipping_methods, only: :destroy + before_filter :load_hubs, only: [:new, :edit, :create, :update] + + # Sort shipping methods by distributor name + def collection + collection = super + collection = collection.managed_by(spree_current_user).by_name + + if params.key? :enterprise_id + distributor = Enterprise.find params[:enterprise_id] + collection = collection.for_distributor(distributor) + end + + collection + end + + # Spree allows soft deletes of shipping_methods but our reports are not adapted to that + # Here we prevent the deletion (even soft) of shipping_methods that are referenced in orders + def do_not_destroy_referenced_shipping_methods + order = Order.joins(shipments: :shipping_rates) + .where( spree_shipping_rates: { shipping_method_id: @object } ) + .first + return unless order + flash[:error] = I18n.t(:shipping_method_destroy_error, number: order.number) + redirect_to(collection_url) && return + end def destroy @object.touch :deleted_at @@ -18,6 +44,14 @@ module Spree private + def load_hubs + # rubocop:disable Style/TernaryParentheses + @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by! do |d| + [(@shipping_method.has_distributor? d) ? 0 : 1, d.name] + end + # rubocop:enable Style/TernaryParentheses + end + def set_shipping_category return true if params["shipping_method"][:shipping_categories] == "" @shipping_method.shipping_categories = diff --git a/app/controllers/spree/admin/shipping_methods_controller_decorator.rb b/app/controllers/spree/admin/shipping_methods_controller_decorator.rb deleted file mode 100644 index 866d1805ba..0000000000 --- a/app/controllers/spree/admin/shipping_methods_controller_decorator.rb +++ /dev/null @@ -1,42 +0,0 @@ -module Spree - module Admin - ShippingMethodsController.class_eval do - before_filter :do_not_destroy_referenced_shipping_methods, only: :destroy - before_filter :load_hubs, only: [:new, :edit, :create, :update] - - # Sort shipping methods by distributor name - def collection - collection = super - collection = collection.managed_by(spree_current_user).by_name - - if params.key? :enterprise_id - distributor = Enterprise.find params[:enterprise_id] - collection = collection.for_distributor(distributor) - end - - collection - end - - # Spree allows soft deletes of shipping_methods but our reports are not adapted to that - # Here we prevent the deletion (even soft) of shipping_methods that are referenced in orders - def do_not_destroy_referenced_shipping_methods - order = Order.joins(shipments: :shipping_rates) - .where( spree_shipping_rates: { shipping_method_id: @object } ) - .first - return unless order - flash[:error] = I18n.t(:shipping_method_destroy_error, number: order.number) - redirect_to(collection_url) && return - end - - private - - def load_hubs - # rubocop:disable Style/TernaryParentheses - @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by! do |d| - [(@shipping_method.has_distributor? d) ? 0 : 1, d.name] - end - # rubocop:enable Style/TernaryParentheses - end - end - end -end