Sha256: 9808676fdb2b23afffd9012ea54e0d017c40e8f3c851ee3fefbf3d438579ce65

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module ShoppingCart
  class ValidateStep < Rectify::Command
    def initialize(step, order)
      @step = step
      @order = order
    end

    def call
      return broadcast(:invalid) unless @step && @order
      all_steps_valid? ? broadcast(:ok) :  broadcast(:invalid)
    end

    private

    def all_steps_valid?
      steps_to_validate.all? do |step|
        order_has_data_for? step
      end
    end

    def steps_to_validate
      steps = ShoppingCart.checkout_steps
      steps[0...steps.index(@step)]
    end

    def order_has_data_for? step
      case step
        when :address  then has_address?  ? true : false
        when :delivery then has_delivery? ? true : false
        when :payment  then has_payment?  ? true : false
        when :confirm  then confirmed?    ? true : false
        else custom_validation(step)      ? true : false
      end
    end

    def has_address?
      @order.billing.try(:valid?) && @order.shipping.try(:valid?)
    end

    def has_delivery?
      @order.delivery.try(:valid?)
    end

    def has_payment?
      @order.credit_card.try(:valid?)
    end

    def confirmed?
      @order.processing?
    end

    def custom_validation step
      @order.try(step).try(:valid?)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopping-cart-0.1.1 app/commands/shopping_cart/validate_step.rb
shopping-cart-0.1.0 app/commands/shopping_cart/validate_step.rb