Gateway providers inherit from decorated Gateway and PaymentMethod classes in production

Achieved by requiring payment method and gateway decorators in Spree initializer
This commit is contained in:
Rob Harrington
2016-08-31 12:43:22 +10:00
parent 1059c170d1
commit b2e14711ac
4 changed files with 16 additions and 42 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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