From 2d305b59d950fe1d1bf7c5757d6f9ad3f94d24c5 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 19 Aug 2013 12:43:45 +1000 Subject: [PATCH] Only show payment methods for the distributor of the order --- .../admin/payments_controller_decorator.rb | 24 ++++++++++++------- app/models/spree/payment_method_decorator.rb | 6 ++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/controllers/spree/admin/payments_controller_decorator.rb b/app/controllers/spree/admin/payments_controller_decorator.rb index 4a58dbfe14..35237d3e76 100644 --- a/app/controllers/spree/admin/payments_controller_decorator.rb +++ b/app/controllers/spree/admin/payments_controller_decorator.rb @@ -1,14 +1,20 @@ -# When a user fires an event, take them back to where they came from -# Responder: http://guides.spreecommerce.com/developer/logic.html#overriding-controller-action-responses - -# For some strange reason, adding PaymentsController.class_eval will cause gems/spree/app/controllers/spree/admin/payments_controller.rb:37 to error: -# payments_url not defined. -# This could be fixed by replacing line 37 with: -# respond_with(@payment, location: admin_order_payments_url) { |format| format.html { redirect_to admin_order_payments_path(@order) } } - - Spree::Admin::PaymentsController.class_eval do + # When a user fires an event, take them back to where they came from + # Responder: http://guides.spreecommerce.com/developer/logic.html#overriding-controller-action-responses + + # For some strange reason, adding PaymentsController.class_eval will cause gems/spree/app/controllers/spree/admin/payments_controller.rb:37 to error: + # payments_url not defined. + # This could be fixed by replacing line 37 with: + # respond_with(@payment, location: admin_order_payments_url) { |format| format.html { redirect_to admin_order_payments_path(@order) } } respond_override :fire => { :html => { :success => lambda { redirect_to request.referer # Keeps any filter and sort prefs } } } + + append_before_filter :filter_payment_methods + + # Only show payments for the order's distributor + def filter_payment_methods + @payment_methods = @payment_methods.select{ |pm| pm.has_distributor? @order.distributor} + end + end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 095b6b5dfc..1f19d9498c 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -1,6 +1,6 @@ Spree::PaymentMethod.class_eval do belongs_to :distributor, :class_name => 'Enterprise' - + validates_presence_of :distributor_id attr_accessible :distributor_id @@ -17,6 +17,10 @@ Spree::PaymentMethod.class_eval do } end +def has_distributor?(distributor) + self.distributor == distributor +end + # Ensure that all derived classes also allow distributor_id Spree::Gateway.providers.each do |p| p.attr_accessible :distributor_id