From 37c8f4224449599105a0369db3685298a908a39d Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 20 Sep 2019 15:56:24 +0100 Subject: [PATCH 1/4] Bring shipping methods and categories controllers from spree_backend --- .../admin/shipping_categories_controller.rb | 6 +++ .../admin/shipping_methods_controller.rb | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 app/controllers/spree/admin/shipping_categories_controller.rb create mode 100644 app/controllers/spree/admin/shipping_methods_controller.rb diff --git a/app/controllers/spree/admin/shipping_categories_controller.rb b/app/controllers/spree/admin/shipping_categories_controller.rb new file mode 100644 index 0000000000..e9d1677027 --- /dev/null +++ b/app/controllers/spree/admin/shipping_categories_controller.rb @@ -0,0 +1,6 @@ +module Spree + module Admin + class ShippingCategoriesController < ResourceController + end + end +end diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb new file mode 100644 index 0000000000..13ab00f118 --- /dev/null +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -0,0 +1,45 @@ +module Spree + module Admin + class ShippingMethodsController < ResourceController + before_filter :load_data, :except => [:index] + before_filter :set_shipping_category, :only => [:create, :update] + before_filter :set_zones, :only => [:create, :update] + + def destroy + @object.touch :deleted_at + + flash[:success] = flash_message_for(@object, :successfully_removed) + + respond_with(@object) do |format| + format.html { redirect_to collection_url } + format.js { render_js_for_destroy } + end + end + + private + + def set_shipping_category + return true if params["shipping_method"][:shipping_categories] == "" + @shipping_method.shipping_categories = Spree::ShippingCategory.where(:id => params["shipping_method"][:shipping_categories]) + @shipping_method.save + params[:shipping_method].delete(:shipping_categories) + end + + def set_zones + return true if params["shipping_method"][:zones] == "" + @shipping_method.zones = Spree::Zone.where(:id => params["shipping_method"][:zones]) + @shipping_method.save + params[:shipping_method].delete(:zones) + end + + def location_after_save + edit_admin_shipping_method_path(@shipping_method) + end + + def load_data + @available_zones = Zone.order(:name) + @calculators = ShippingMethod.calculators.sort_by(&:name) + end + end + end +end From a5103c737d7900b78e6c3546cc7d16dd063f60ec Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 20 Sep 2019 15:58:05 +0100 Subject: [PATCH 2/4] Fix rubocop issues in ship methods controller from spree --- .../spree/admin/shipping_methods_controller.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index 13ab00f118..41939927ea 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -1,9 +1,9 @@ module Spree module Admin class ShippingMethodsController < ResourceController - before_filter :load_data, :except => [:index] - before_filter :set_shipping_category, :only => [:create, :update] - before_filter :set_zones, :only => [:create, :update] + before_filter :load_data, except: [:index] + before_filter :set_shipping_category, only: [:create, :update] + before_filter :set_zones, only: [:create, :update] def destroy @object.touch :deleted_at @@ -12,7 +12,7 @@ module Spree respond_with(@object) do |format| format.html { redirect_to collection_url } - format.js { render_js_for_destroy } + format.js { render_js_for_destroy } end end @@ -20,14 +20,15 @@ module Spree def set_shipping_category return true if params["shipping_method"][:shipping_categories] == "" - @shipping_method.shipping_categories = Spree::ShippingCategory.where(:id => params["shipping_method"][:shipping_categories]) + @shipping_method.shipping_categories = + Spree::ShippingCategory.where(id: params["shipping_method"][:shipping_categories]) @shipping_method.save params[:shipping_method].delete(:shipping_categories) end def set_zones return true if params["shipping_method"][:zones] == "" - @shipping_method.zones = Spree::Zone.where(:id => params["shipping_method"][:zones]) + @shipping_method.zones = Spree::Zone.where(id: params["shipping_method"][:zones]) @shipping_method.save params[:shipping_method].delete(:zones) end From 8e33437fbb1f15a855b5a88b5813270bb2e3f8ae Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 20 Sep 2019 15:59:24 +0100 Subject: [PATCH 3/4] 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 From fc433ff8f00af83e263d113f3b90365b0be9ffda Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 20 Sep 2019 16:23:18 +0100 Subject: [PATCH 4/4] Refactor ship methods controller destroy action: remove single action before_filter --- .../admin/shipping_methods_controller.rb | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index 5434e829f3..d4c9c29898 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -4,7 +4,6 @@ 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 @@ -20,30 +19,30 @@ module Spree 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 + # Our reports are not adapted to soft deleted shipping_methods so here we prevent + # the deletion (even soft) of shipping_methods that are referenced in orders + if order = order_referenced_by_shipping_method + flash[:error] = I18n.t(:shipping_method_destroy_error, number: order.number) + redirect_to(collection_url) && return + end + @object.touch :deleted_at flash[:success] = flash_message_for(@object, :successfully_removed) respond_with(@object) do |format| format.html { redirect_to collection_url } - format.js { render_js_for_destroy } end end private + def order_referenced_by_shipping_method + Order.joins(shipments: :shipping_rates) + .where( spree_shipping_rates: { shipping_method_id: @object } ) + .first + end + def load_hubs # rubocop:disable Style/TernaryParentheses @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by! do |d|