From b3c56e6823c5804543f0ce199d076f4ee3cba511 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 13 Oct 2018 23:44:43 +0100 Subject: [PATCH 1/2] Duplicated payment method distributors association to gateway. This fixes the inheritance problem found in stripe gateway See PR 2780 for more details --- app/models/spree/gateway_decorator.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/spree/gateway_decorator.rb b/app/models/spree/gateway_decorator.rb index bc4312961e..75e17ffcaf 100644 --- a/app/models/spree/gateway_decorator.rb +++ b/app/models/spree/gateway_decorator.rb @@ -2,4 +2,7 @@ Spree::Gateway.class_eval do # Default to live preference :server, :string, :default => 'live' preference :test_mode, :boolean, :default => false + + attr_accessible :distributor_ids + has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', foreign_key: 'payment_method_id', association_foreign_key: 'distributor_id' end From 98161daa90a3eed20b7c0ac2120d5722b6ef9d6e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 14 Oct 2018 00:57:09 +0100 Subject: [PATCH 2/2] Refactor duplicated distributors association in payment_method and gateway to a concern --- .../spree/concerns/payment_method_distributors.rb | 15 +++++++++++++++ app/models/spree/gateway_decorator.rb | 7 ++++--- app/models/spree/payment_method_decorator.rb | 6 ++++-- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 app/models/spree/concerns/payment_method_distributors.rb diff --git a/app/models/spree/concerns/payment_method_distributors.rb b/app/models/spree/concerns/payment_method_distributors.rb new file mode 100644 index 0000000000..0913889a8e --- /dev/null +++ b/app/models/spree/concerns/payment_method_distributors.rb @@ -0,0 +1,15 @@ +require 'active_support/concern' + +# This concern is used to duplicate the associations distributors and distributor_ids +# across payment method and gateway +# this fixes the inheritance problem https://github.com/openfoodfoundation/openfoodnetwork/issues/2781 +module Spree::PaymentMethodDistributors + extend ActiveSupport::Concern + + def self.included(base) + base.class_eval do + attr_accessible :distributor_ids + has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', class_name: 'Enterprise', foreign_key: 'payment_method_id', association_foreign_key: 'distributor_id' + end + end +end diff --git a/app/models/spree/gateway_decorator.rb b/app/models/spree/gateway_decorator.rb index 75e17ffcaf..b87ef71621 100644 --- a/app/models/spree/gateway_decorator.rb +++ b/app/models/spree/gateway_decorator.rb @@ -1,8 +1,9 @@ +require 'spree/concerns/payment_method_distributors' + Spree::Gateway.class_eval do + include Spree::PaymentMethodDistributors + # Default to live preference :server, :string, :default => 'live' preference :test_mode, :boolean, :default => false - - attr_accessible :distributor_ids - has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', foreign_key: 'payment_method_id', association_foreign_key: 'distributor_id' end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 6eb845895f..77b17e0f60 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -1,12 +1,14 @@ +require 'spree/concerns/payment_method_distributors' + Spree::PaymentMethod.class_eval do include Spree::Core::CalculatedAdjustments + include Spree::PaymentMethodDistributors acts_as_taggable - has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', association_foreign_key: 'distributor_id' has_many :credit_cards, class_name: "Spree::CreditCard" # from Spree v.2.3.0 d470b31798f37 - attr_accessible :distributor_ids, :tag_list + attr_accessible :tag_list after_initialize :init