Sha256: 6dba0385e034153c45f58e36d2c3e5cec045e84ed094e760a245ed0e11d22199

Contents?: true

Size: 1.62 KB

Versions: 62

Compression:

Stored size: 1.62 KB

Contents

# This class is responsible for consolidating
# auto completion data for a given checkout. It's
# params method is passed to {Checkout#update} to complete
# as much of checkout as possible.
#
module Workarea
  class Checkout::AutoComplete
    attr_reader :order, :payment, :user

    def initialize(order, payment, user)
      @order = order
      @payment = payment
      @user = user
    end

    # The params for the most autocomplete info that is
    # present for the current user. Passed to {Checkout#update}
    # to autocomplete the order.
    #
    # @return [Hash]
    #
    def params
      result = {
        email: user.email,
        payment: credit_card.try(:id)
      }

      if billing_address.present?
        result[:billing_address] = billing_address.attributes.slice(
            *address_attr_keys
          )
      end

      if shipping_address.present?
        result[:shipping_address] = shipping_address.attributes.slice(
            *address_attr_keys
          )
      end

      result
    end

    # The billing address used to autocomplete the checkout.
    #
    # @return [Address, nil]
    #
    def billing_address
      user.default_billing_address
    end


    # The shipping address used to autocomplete the checkout.
    #
    # @return [Address, nil]
    #
    def shipping_address
      user.default_shipping_address
    end

    # The credit card used to autocomplete the checkout.
    #
    # @return [Payment::SavedCreditCard, nil]
    #
    def credit_card
      payment.default_credit_card
    end

    private

    def address_attr_keys
      Workarea.config.address_attributes.map(&:to_s)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.26 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.45 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.25 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.23 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.44 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.22 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.43 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.21 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.42 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.20 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.41 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.19 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.40 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.18 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.39 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.17 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.38 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.5.16 app/models/workarea/checkout/auto_complete.rb
workarea-core-3.4.37 app/models/workarea/checkout/auto_complete.rb