Files
openfoodnetwork/app/services/order_available_payment_methods.rb
Maikel Linke b5f43b3c1c Rename method because it's not changing the input
A common Ruby convention is to to use the bang naming `!` to mark method
which change the given parameter instead of returning a copy.
2023-03-21 14:30:44 +11:00

42 lines
1.0 KiB
Ruby

# frozen_string_literal: true
class OrderAvailablePaymentMethods
attr_reader :order, :customer
delegate :distributor,
:order_cycle,
to: :order
def initialize(order, customer = nil)
@order, @customer = order, customer
end
def to_a
return [] if distributor.blank?
payment_methods = payment_methods_before_tag_rules_applied
applicator = OpenFoodNetwork::TagRuleApplicator.new(distributor,
"FilterPaymentMethods", customer&.tag_list)
applicator.filter(payment_methods)
end
private
def payment_methods_before_tag_rules_applied
if order_cycle.nil? || order_cycle.simple?
distributor.payment_methods
else
distributor.payment_methods.where(
id: available_distributor_payment_methods_ids
)
end.available.select(&:configured?).uniq
end
def available_distributor_payment_methods_ids
order_cycle.distributor_payment_methods
.where(distributor_id: distributor.id)
.select(:payment_method_id)
end
end