From b2e14711acc43ef80928416289da4296a341b319 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 31 Aug 2016 12:43:22 +1000 Subject: [PATCH] Gateway providers inherit from decorated Gateway and PaymentMethod classes in production Achieved by requiring payment method and gateway decorators in Spree initializer --- .../gateway/pay_pal_express_decorator.rb | 9 ----- app/models/spree/gateway_decorator.rb | 13 -------- app/models/spree/payment_method_decorator.rb | 33 ++++++++----------- config/initializers/spree.rb | 3 ++ 4 files changed, 16 insertions(+), 42 deletions(-) delete mode 100644 app/models/spree/gateway/pay_pal_express_decorator.rb diff --git a/app/models/spree/gateway/pay_pal_express_decorator.rb b/app/models/spree/gateway/pay_pal_express_decorator.rb deleted file mode 100644 index f83151e617..0000000000 --- a/app/models/spree/gateway/pay_pal_express_decorator.rb +++ /dev/null @@ -1,9 +0,0 @@ -require_relative '../gateway_decorator' - -module Spree - class Gateway::PayPalExpress < Gateway - # Something odd is happening with class inheritance here, this class (defined in spree_paypal_express gem) - # doesn't seem to pick up attr_accessible from the Gateway class, so we redefine the attrs we need here - attr_accessible :tag_list - end -end diff --git a/app/models/spree/gateway_decorator.rb b/app/models/spree/gateway_decorator.rb index 9125ef1d2d..bc4312961e 100644 --- a/app/models/spree/gateway_decorator.rb +++ b/app/models/spree/gateway_decorator.rb @@ -1,18 +1,5 @@ Spree::Gateway.class_eval do - acts_as_taggable - - # Due to class load order, when config.cache_classes is enabled (ie. staging and production - # environments), this association isn't inherited from PaymentMethod. As a result, creating - # payment methods using payment gateways results in: - # undefined method `association_class' for nil:NilClass - # To avoid that, we redefine this association here. - - has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', foreign_key: 'payment_method_id', association_foreign_key: 'distributor_id' - - # Default to live preference :server, :string, :default => 'live' preference :test_mode, :boolean, :default => false - - attr_accessible :tag_list end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 77e22c591f..4a718dfc16 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -1,7 +1,6 @@ Spree::PaymentMethod.class_eval do acts_as_taggable - # See gateway_decorator.rb when modifying this association has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', association_foreign_key: 'distributor_id' attr_accessible :distributor_ids, :tag_list @@ -45,26 +44,20 @@ Spree::PaymentMethod.class_eval do def has_distributor?(distributor) self.distributors.include?(distributor) end -end -# Ensure that all derived classes also allow distributor_ids -Spree::Gateway.providers.each do |p| - p.attr_accessible :distributor_ids - p.instance_eval do - def clean_name - case name - when "Spree::PaymentMethod::Check" - "Cash/EFT/etc. (payments for which automatic validation is not required)" - when "Spree::Gateway::Migs" - "MasterCard Internet Gateway Service (MIGS)" - when "Spree::Gateway::Pin" - "Pin Payments" - when "Spree::Gateway::PayPalExpress" - "PayPal Express" - else - i = name.rindex('::') + 2 - name[i..-1] - end + def self.clean_name + case name + when "Spree::PaymentMethod::Check" + "Cash/EFT/etc. (payments for which automatic validation is not required)" + when "Spree::Gateway::Migs" + "MasterCard Internet Gateway Service (MIGS)" + when "Spree::Gateway::Pin" + "Pin Payments" + when "Spree::Gateway::PayPalExpress" + "PayPal Express" + else + i = name.rindex('::') + 2 + name[i..-1] end end end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 92ba3db249..30182ef63a 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -9,6 +9,9 @@ require 'spree/product_filters' +require "#{Rails.root}/app/models/spree/payment_method_decorator" +require "#{Rails.root}/app/models/spree/gateway_decorator" + Spree.config do |config| config.shipping_instructions = true config.address_requires_state = true