我需要重构代码的帮助。我最好尝试使用以下代码。有什么我可以做的
class OrdersController < ApplicationController
before_action :get_cart
before_action :set_credit_details, only: [:create]
# process order
def create
@order = Order.new(order_params)
# Add items from cart to order's ordered_items association
@cart.ordered_items.each do |item|
@order.ordered_items << item
end
# Add shipping and tax to order total
@order.total = case params[:order][:shipping_method]
when 'ground'
(@order.taxed_total).round(2)
when 'two-day'
@order.taxed_total + (15.75).round(2)
when "overnight"
@order.taxed_total + (25).round(2)
end
# Process credit card
# Check if card is valid
if @credit_card.valid?
billing_address = {
name: "#{params[:billing_first_name]} # .
{params[:billing_last_name]}",
address1: params[:billing_address_line_1],
city: params[:billing_city], state: params[:billing_state],
country: 'US',zip: params[:billing_zip],
phone: params[:billing_phone]
}
options = { address: {}, billing_address: billing_address }
# Make the purchase through ActiveMerchant
charge_amount = (@order.total.to_f * 100).to_i
response = ActiveMerchant::Billing::AuthorizeNetGateway.new(
login: ENV["AUTHORIZE_LOGIN"],
password: ENV["AUTHORIZE_PASSWORD"]
).purchase(charge_amount, @credit_card, options)
unless response.success?
@order.errors.add(:error, "We couldn't process your credit
card")
end
else
@order.errors.add(:error, "Your credit card seems to be invalid")
flash[:error] = "There was a problem processing your order. Please try again."
render :new && return
end
@order.order_status = 'processed'
if @order.save
# get rid of cart
Cart.destroy(session[:cart_id])
# send order confirmation email
OrderMailer.order_confirmation(order_params[:billing_email], session[:order_id]).deliver
flash[:success] = "You successfully ordered!"
redirect_to confirmation_orders_path
else
flash[:error] = "There was a problem processing your order. Please try again."
render :new
end
end
private
def order_params
params.require(:order).permit!
end
def get_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
end
def set_credit_details
# Get credit card object from ActiveMerchant
@credit_card = ActiveMerchant::Billing::CreditCard.new(
number: params[:card_info][:card_number],
month: params[:card_info][:card_expiration_month],
year: params[:card_info][:card_expiration_year],
verification_value: params[:card_info][:cvv],
first_name: params[:card_info][:card_first_name],
last_name: params[:card_info][:card_last_name],
type: get_card_type # Get the card type
)
end
def get_card_type
length, number = params[:card_info][:card_number].size, params[:card_info][:card_number]
case
when length == 15 && number =~ /^(34|37)/
"AMEX"
when length == 16 && number =~ /^6011/
"Discover"
when length == 16 && number =~ /^5[1-5]/
"MasterCard"
when (length == 13 || length == 16) && number =~ /^4/
"Visa"
else
"Unknown"
end
end
end
具有价格属性的产品。我们的购物车通过OrderedItems联接表具有许多产品。 OrderedItem属于购物车和产品。它具有数量属性,可以跟踪订购的产品数量。
OrderedItem也属于某个订单
我想知道它是否可以进一步重构。
答案 0 :(得分:1)
首先,您应该将所有业务逻辑从控制器移至模型和服务(OrderProcessService,PaymentService)。控制器的所有私有方法都属于PaymentService
。
将代码拆分为较小的方法。
如果在模型级别执行此操作,则在阅读代码时会想到以下几点:
@order.add_items_from_cart(@cart)
@order.add_shipping_and_tax(shipping_method)
订单应先保存(保存在DB中),然后再处理(购买以更改其状态)。
@order.save
在成功付款后可能会失败,因此客户将赔钱,而无法获得订单。还有很多其他事情要考虑。您有很多工作要做。