app/controllers/comable/orders_controller.rb in comable_frontend-0.3.4 vs app/controllers/comable/orders_controller.rb in comable_frontend-0.4.0
- old
+ new
@@ -2,10 +2,11 @@
class OrdersController < Comable::ApplicationController
# TODO: Change the method name to load_order_with_params
before_filter :load_order
before_filter :ensure_cart_not_empty
before_filter :ensure_saleable_stocks
+ before_filter :ensure_correct_flow, only: :create
prepend Comable::SigninAction
prepend Comable::ShipmentAction
prepend Comable::PaymentAction
include Comable::PermittedAttributes
@@ -25,17 +26,17 @@
render @order.state
end
end
def create
- if @order.state?(:confirm) && @order.next_state
- flash.now[:notice] = Comable.t('orders.success')
- send_order_complete_mail
- else
- flash[:alert] = Comable.t('orders.failure')
- redirect_to next_order_path
- end
+ @order.next_state!
+
+ flash.now[:notice] = Comable.t('orders.success')
+ send_order_complete_mail
+ rescue ActiveRecord::RecordInvalid
+ flash[:alert] = @order.errors.full_messages.join
+ redirect_to next_order_path
end
private
def send_order_complete_mail
@@ -46,18 +47,23 @@
Comable::OrderMailer.complete(@order).deliver
end
end
def ensure_cart_not_empty
- return if current_customer.cart.any?
+ return if current_comable_user.cart.any?
flash[:alert] = Comable.t('carts.empty')
redirect_to comable.cart_path
end
def ensure_saleable_stocks
return if current_order.stocked_items.empty?
flash[:alert] = Comable.t('errors.messages.out_of_stocks')
redirect_to comable.cart_path
+ end
+
+ def ensure_correct_flow
+ return if @order.state?(:confirm)
+ redirect_to next_order_path
end
def load_order
@order = current_order
@order.attributes = order_params if order_params