mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Extract params handling to service
This commit is contained in:
@@ -56,7 +56,7 @@ class SplitCheckoutController < ::BaseController
|
||||
end
|
||||
|
||||
def update_order
|
||||
return unless params[:order]
|
||||
return if @order.errors.any?
|
||||
|
||||
@order.update(order_params) && advance_order_state
|
||||
end
|
||||
@@ -79,37 +79,7 @@ class SplitCheckoutController < ::BaseController
|
||||
end
|
||||
|
||||
def order_params
|
||||
return @order_params unless @order_params.nil?
|
||||
|
||||
@order_params = params.require(:order).permit(
|
||||
:email, :shipping_method_id, :special_instructions,
|
||||
bill_address_attributes: PermittedAttributes::Address.attributes,
|
||||
ship_address_attributes: PermittedAttributes::Address.attributes,
|
||||
payments_attributes: [:payment_method_id]
|
||||
)
|
||||
|
||||
set_address_details
|
||||
set_payment_amount
|
||||
|
||||
@order_params
|
||||
end
|
||||
|
||||
def set_address_details
|
||||
return unless @order_params[:ship_address_attributes] && @order_params[:bill_address_attributes]
|
||||
|
||||
if params[:ship_address_same_as_billing]
|
||||
@order_params[:ship_address_attributes] = @order_params[:bill_address_attributes]
|
||||
else
|
||||
@order_params[:ship_address_attributes][:firstname] = @order_params[:bill_address_attributes][:firstname]
|
||||
@order_params[:ship_address_attributes][:lastname] = @order_params[:bill_address_attributes][:lastname]
|
||||
@order_params[:ship_address_attributes][:phone] = @order_params[:bill_address_attributes][:phone]
|
||||
end
|
||||
end
|
||||
|
||||
def set_payment_amount
|
||||
return unless @order_params[:payments_attributes]
|
||||
|
||||
@order_params[:payments_attributes].first[:amount] = @order.total
|
||||
@order_params ||= Checkout::Params.new(@order, params).call
|
||||
end
|
||||
|
||||
def redirect_to_step
|
||||
|
||||
63
app/services/checkout/params.rb
Normal file
63
app/services/checkout/params.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Checkout
|
||||
class Params
|
||||
def initialize(order, params)
|
||||
@params = params
|
||||
@order = order
|
||||
end
|
||||
|
||||
def call
|
||||
return {} unless params[:order]
|
||||
|
||||
apply_strong_parameters
|
||||
set_address_details
|
||||
set_payment_amount
|
||||
|
||||
@order_params
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :params, :order
|
||||
|
||||
def apply_strong_parameters
|
||||
@order_params = params.require(:order).permit(
|
||||
:email, :shipping_method_id, :special_instructions,
|
||||
bill_address_attributes: ::PermittedAttributes::Address.attributes,
|
||||
ship_address_attributes: ::PermittedAttributes::Address.attributes,
|
||||
payments_attributes: [:payment_method_id]
|
||||
)
|
||||
end
|
||||
|
||||
def set_address_details
|
||||
return unless addresses_present?
|
||||
|
||||
if params[:ship_address_same_as_billing]
|
||||
set_ship_address_from_bill_address
|
||||
else
|
||||
set_basic_details
|
||||
end
|
||||
end
|
||||
|
||||
def set_payment_amount
|
||||
return unless @order_params[:payments_attributes]
|
||||
|
||||
@order_params[:payments_attributes].first[:amount] = order.total
|
||||
end
|
||||
|
||||
def addresses_present?
|
||||
@order_params[:ship_address_attributes] && @order_params[:bill_address_attributes]
|
||||
end
|
||||
|
||||
def set_ship_address_from_bill_address
|
||||
@order_params[:ship_address_attributes] = @order_params[:bill_address_attributes]
|
||||
end
|
||||
|
||||
def set_basic_details
|
||||
[:firstname, :lastname, :phone].each do |attr|
|
||||
@order_params[:ship_address_attributes][attr] = @order_params[:bill_address_attributes][attr]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user