Do not load order twice

The controller already does so, then, we can pass it to the service and
avoid that extra round-trip to the DB and save some memory. Spree::Order
is a rather bulky object (God object code smell perhaps) and it'll
surely make a difference.
This commit is contained in:
Pau Perez
2021-02-11 10:53:16 +01:00
parent 891874995b
commit e2853b9afb
2 changed files with 7 additions and 5 deletions

View File

@@ -25,8 +25,8 @@ module Spree
before_action :check_at_least_one_line_item, only: :update
def show
ProcessPaymentIntent.new(params["payment_intent"], params[:id]).call!
@order = Spree::Order.find_by!(number: params[:id])
ProcessPaymentIntent.new(params["payment_intent"], @order).call!
end
def empty

View File

@@ -1,10 +1,10 @@
# frozen_string_literal: true
class ProcessPaymentIntent
def initialize(payment_intent, order_number)
def initialize(payment_intent, order)
@payment_intent = payment_intent
@order = Spree::Order.find_by!(number: order_number)
@last_payment = OrderPaymentFinder.new(@order).last_payment
@order = order
@last_payment = OrderPaymentFinder.new(order).last_payment
end
def call!
@@ -16,8 +16,10 @@ class ProcessPaymentIntent
private
attr_reader :order
def valid?
@order.present? && valid_intent_string? && matches_last_payment?
order.present? && valid_intent_string? && matches_last_payment?
end
def valid_intent_string?