Sha256: b4db9965e1e77096cc76d0ae298b75e1a680902d0a35837d62397d17aafc0604

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

module SimpleCart
  class CheckoutController < SimpleCart::ApplicationController

    include Wicked::Wizard
    # before_action :authenticate_user!
    before_action :set_order

    steps :billing, :shipping, :delivery, :payment, :confirm

    def show
      # authorize! :edit, @order
      case step
      when :delivery
        jump_to(:billing) if @order.shipping_address.nil?
      when :payment
        jump_to(:delivery) if @order.delivery.nil?
      when :confirm
        jump_to(:payment) if @order.credit_card.nil?
      end
      @order.build_or_find_billing_address
      @order.build_or_find_shipping_address
      @order.build_or_find_credit_card
      render_wizard 
    end

    def update
      case step
      when :billing
        if @order.update(order_params) && !params[:shipping].nil?
          @order.build_shipping_address.attributes = @order.billing_address.attributes.except("id", "type", "user_id")
          jump_to(:delivery)
        end
      when :shipping
        @order.update(order_params)
      when :delivery
        @order.delivery = Delivery.find(params[:order][:delivery_id])
      when :payment
        @order.update(order_params)
      end
      render_wizard @order
    end

    private

    def set_order
      @order = Order.find(params[:order_id])
    end

    def order_params
      params.require(:order).permit(:billing_address_attributes  => [:firstname, :lastname, :address, :zipcode, :city, :phone, :country],
                                    :shipping_address_attributes => [:firstname, :lastname, :address, :zipcode, :city, :phone, :country],
                                    :credit_card_attributes      => [:number, :expiration_month, :expiration_year, :cvv, :firstname, :lastname],
                                    :order                       => [:delivery_id])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_cart-0.0.3 app/controllers/simple_cart/checkout_controller.rb
simple_cart-0.0.2 app/controllers/simple_cart/checkout_controller.rb