From 2df2096d74d1f1066395be838492416d4c61f00a Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 19 Sep 2013 15:18:13 +1000 Subject: [PATCH] Sort payment and shipping methods by distributor name --- .../admin/payment_methods_controller_decorator.rb | 6 +++--- .../admin/shipping_methods_controller_decorator.rb | 13 +++++++++++++ app/models/spree/payment_method_decorator.rb | 6 ++++++ app/models/spree/shipping_method_decorator.rb | 6 ++++++ .../index/add_distributor_td.html.haml.deface | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb index d1d367fc47..db2579e054 100644 --- a/app/controllers/spree/admin/payment_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -1,14 +1,14 @@ Spree::Admin::PaymentMethodsController.class_eval do - # Only show payment methods that user has access to. + # Only show payment methods that user has access to and sort by distributor name # ! Redundant code copied from Spree::Admin::ResourceController with two added lines def collection return parent.send(controller_name) if parent_data.present? if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class) model_class.accessible_by(current_ability, action). - managed_by(spree_current_user) # this line added + managed_by(spree_current_user).by_distributor # this line added else model_class.scoped. - managed_by(spree_current_user) # this line added + managed_by(spree_current_user).by_distributor # this line added end end end \ No newline at end of file diff --git a/app/controllers/spree/admin/shipping_methods_controller_decorator.rb b/app/controllers/spree/admin/shipping_methods_controller_decorator.rb index 035f83e408..8dbd8eb3d0 100644 --- a/app/controllers/spree/admin/shipping_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/shipping_methods_controller_decorator.rb @@ -3,6 +3,19 @@ module Spree ShippingMethodsController.class_eval do before_filter :do_not_destroy_referenced_shipping_methods, :only => :destroy + # Sort shipping methods by distributor name + # ! Redundant code copied from Spree::Admin::ResourceController with two added lines + def collection + return parent.send(controller_name) if parent_data.present? + if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class) + model_class.accessible_by(current_ability, action). + by_distributor # this line added + else + model_class.scoped. + by_distributor # this line added + end + end + # This method was originally written because ProductDistributions referenced shipping # methods, and deleting a referenced shipping method would break all the reports that # queried it. diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index f01f4a63dd..865ec5bcd5 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -14,6 +14,12 @@ Spree::PaymentMethod.class_eval do end } + scope :by_distributor, lambda { + joins(:distributors). + order('enterprises.name, spree_payment_methods.name'). + select('enterprises.*, spree_payment_methods.*') + } + def has_distributor?(distributor) self.distributors.include?(distributor) end diff --git a/app/models/spree/shipping_method_decorator.rb b/app/models/spree/shipping_method_decorator.rb index e7419e3330..49f333b790 100644 --- a/app/models/spree/shipping_method_decorator.rb +++ b/app/models/spree/shipping_method_decorator.rb @@ -4,6 +4,12 @@ Spree::ShippingMethod.class_eval do validates_presence_of :distributor_id + scope :by_distributor, lambda { + joins(:distributor). + order('enterprises.name, spree_shipping_methods.name'). + select('enterprises.*, spree_shipping_methods.*') + } + def available_to_order_with_distributor_check?(order, display_on=nil) available_to_order_without_distributor_check?(order, display_on) && (order.distributor == self.distributor) diff --git a/app/overrides/spree/admin/payment_methods/index/add_distributor_td.html.haml.deface b/app/overrides/spree/admin/payment_methods/index/add_distributor_td.html.haml.deface index 3d824cafe2..e6ab908889 100644 --- a/app/overrides/spree/admin/payment_methods/index/add_distributor_td.html.haml.deface +++ b/app/overrides/spree/admin/payment_methods/index/add_distributor_td.html.haml.deface @@ -1,4 +1,4 @@ / insert_top "[data-hook='admin_payment_methods_index_rows']" %td.align-center - = method.distributors.map(&:name).join(', ') + = method.distributors.map(&:name).sort.join(', ')