Extract params handling to service

This commit is contained in:
Matt-Yorkley
2021-09-01 12:36:27 +01:00
parent 57504f42d8
commit 4cff185b4b
2 changed files with 65 additions and 32 deletions

View File

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

View 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