Extract filters for available PMs into service object

This commit is contained in:
Rob Harrington
2017-09-13 09:01:33 +10:00
parent 0bbaafbd41
commit 1cd1e9dc37
2 changed files with 21 additions and 10 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/available_payment_method_filter'
module EnterprisesHelper
def current_distributor
@current_distributor ||= current_order(false).andand.distributor
@@ -22,7 +24,8 @@ module EnterprisesHelper
return [] unless current_distributor.present?
payment_methods = current_distributor.payment_methods.available(:front_end).all
apply_stripe_filters_to(payment_methods)
filter = OpenFoodNetwork::AvailablePaymentMethodFilter.new
filter.filter!(payment_methods)
applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterPaymentMethods", current_customer.andand.tag_list)
applicator.filter!(payment_methods)
@@ -98,13 +101,4 @@ module EnterprisesHelper
def show_bought_items?
order_changes_allowed? && current_order.finalised_line_items.present?
end
def apply_stripe_filters_to(payment_methods)
stripe_enabled = Spree::Config.stripe_connect_enabled && Stripe.publishable_key
if stripe_enabled
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && p.preferred_enterprise_id == 0 }
else
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") }
end
end
end

View File

@@ -0,0 +1,17 @@
module OpenFoodNetwork
class AvailablePaymentMethodFilter
def filter!(payment_methods)
if stripe_enabled?
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && p.preferred_enterprise_id.zero? }
else
payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") }
end
end
private
def stripe_enabled?
Spree::Config.stripe_connect_enabled && Stripe.publishable_key
end
end
end