我正在努力清理我的应用程序以获得更好的性能。而不是有大约5行的东西,我想缩短东西。我的控制器中有电流;
# this pretty much talks with Stripe and grabs our customer token or account id
customer = Stripe::Customer.retrieve(current_user.stripe_customer_token)
# this grabs the stripeToken card info and creates the credit card
customer.cards.create(:card => params[:stripeToken])
customer.subscriptions.create(:plan => 'subscriber')
customer.save
我想缩短内容,不确定这是不是一个好主意;
customer = Stripe::Customer.retrieve(current_user.stripe_customer_token)
customer.cards.create(:card => params[:stripeToken]) unless customer.cards.present? customer.subscriptions.create(:plan => 'subscriber').save
上述工作会起作用吗?我有错误,也许我错过了什么。
答案 0 :(得分:2)
这在技术上是相同的行数,但在我看来更简洁(未经测试):
Stripe::Customer.retrieve(current_user.stripe_customer_token).tap do |customer|
customer.cards.create(card: params[:stripeToken])
customer.subscriptions.create(plan: :subscriber)
end.save