Class: Checkout::CheckoutForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Models::ValidationErrorReporting, StripAttributes
Defined in:
app/forms/checkout/checkout_form.rb

Overview

Form object for handling checkout data validation and persistence.
Manages customer info, billing/shipping addresses, and order updates.

Defined Under Namespace

Classes: CheckoutValidationError

Constant Summary collapse

ADDRESS_TYPE_FIELDS =

Address type field mappings for freight requirements

{
  'residential' => {
    is_residential: true, requires_liftgate: true, has_loading_dock: false,
    is_construction_site: false, requires_inside_delivery: false, limited_access: false, requires_appointment: true
  },
  'commercial_building_with_receiving_dock' => {
    is_residential: false, requires_liftgate: false, has_loading_dock: true,
    is_construction_site: false, requires_inside_delivery: false, limited_access: false, requires_appointment: false
  },
  'commercial_building_without_receiving_dock' => {
    is_residential: false, requires_liftgate: true, has_loading_dock: false,
    is_construction_site: false, requires_inside_delivery: false, limited_access: false, requires_appointment: true
  },
  'construction_site' => {
    is_residential: false, requires_liftgate: true, has_loading_dock: false,
    is_construction_site: true, requires_inside_delivery: false, limited_access: true, requires_appointment: true
  }
}.freeze
DEFAULT_ADDRESS_FIELDS =

Default address fields.

{
  is_residential: false, requires_liftgate: false, has_loading_dock: false,
  is_construction_site: false, requires_inside_delivery: false, limited_access: false, requires_appointment: true
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#billing_addressObject (readonly)

Requires billing address, phone, and email to be present.

Validations:



94
# File 'app/forms/checkout/checkout_form.rb', line 94

validates :billing_address, :phone, :email, presence: true

#contact_nameObject (readonly)

Requires a contact name.

Validations:



92
# File 'app/forms/checkout/checkout_form.rb', line 92

validates :contact_name, presence: true

#emailObject (readonly)

Validates the email address format.

Validations:



90
# File 'app/forms/checkout/checkout_form.rb', line 90

validates :email, email_format: true

#phoneObject (readonly)

Validates the phone number format.

Validations:



88
# File 'app/forms/checkout/checkout_form.rb', line 88

validates :phone, phone_format: true

Class Method Details

.get_address_type(address) ⇒ String

Resolves the address-type key for a persisted address.

Parameters:

  • address (Address)

    the address to classify

Returns:

  • (String)

    address-type key from ADDRESS_TYPE_FIELDS



105
106
107
# File 'app/forms/checkout/checkout_form.rb', line 105

def self.get_address_type(address)
  address.determine_address_type
end

.new_from_amazon_checkout_session(_session_id) ⇒ nil

Placeholder for building a form from an Amazon checkout session.
Not yet implemented; returns nil.

Parameters:

  • _session_id (String)

    the Amazon checkout session id (unused)

Returns:

  • (nil)


124
# File 'app/forms/checkout/checkout_form.rb', line 124

def self.new_from_amazon_checkout_session(_session_id); end

.new_from_cart(cart) ⇒ CheckoutForm

Builds a form pre-populated from the cart's customer, ensuring the
customer has at least one contact and a billing address shell first.

Parameters:

  • cart (Order)

    the cart being checked out

Returns:



114
115
116
117
# File 'app/forms/checkout/checkout_form.rb', line 114

def self.new_from_cart(cart)
  prepare_customer_for_checkout(cart)
  build_from_cart(cart)
end

Instance Method Details

#billing_address=(value) ⇒ void (readonly)

This method returns an undefined value.

Assigns the billing address, wrapping a raw params Hash in a
StreetAddress value object first.

Parameters:



74
75
76
# File 'app/forms/checkout/checkout_form.rb', line 74

def billing_address=(value)
  super(value.is_a?(Hash) ? StreetAddress.new(street_address_params(value)) : value)
end

#homeowner?Boolean

Whether the checkout is for a homeowner (no company name given).

Returns:

  • (Boolean)


212
213
214
# File 'app/forms/checkout/checkout_form.rb', line 212

def homeowner?
  company_name.blank?
end

#persisted?false

Form object is never backed by a persisted record.

Returns:

  • (false)


191
192
193
# File 'app/forms/checkout/checkout_form.rb', line 191

def persisted?
  false
end

#profile_collection_for_selectArray<Array(String, Integer)>

Profiles for the trade select dropdown on the checkout form.

Returns:

  • (Array<Array(String, Integer)>)

    [name, id] pairs ordered by name



205
206
207
# File 'app/forms/checkout/checkout_form.rb', line 205

def profile_collection_for_select
  Profile.trades.order(:name).pluck(:name, :id)
end

#retrieve_shipping_methodsnil

Placeholder proxy for shipping method options. Returns nil.

Returns:

  • (nil)


198
199
200
# File 'app/forms/checkout/checkout_form.rb', line 198

def retrieve_shipping_methods
  # Proxy for shipping method options
end

#saveBoolean

Backward compatible save method.

Returns:

  • (Boolean)

    true when checkout completed without errors



219
220
221
# File 'app/forms/checkout/checkout_form.rb', line 219

def save
  save!
end

#save!Boolean

Validates and persists the checkout: updates the customer, validates and
saves addresses, and finalizes the cart, all inside a transaction.

Returns:

  • (Boolean)

    true when the cart has no errors after saving

Raises:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/forms/checkout/checkout_form.rb', line 229

def save!
  self.cart = Order.find(order_id)
  raise CheckoutValidationError, errors.full_messages.join(', ') unless valid?
  raise CheckoutValidationError, 'Spam detected' if email.include?('johnsmithstore')

  Order.transaction do
    self.customer = update_customer
    validate_customer!
    validate_and_save_addresses!
    finalize_cart!
  end

  cart.errors.blank?
rescue CheckoutValidationError
  false
end

#shipping_address=(value) ⇒ void

This method returns an undefined value.

Assigns the shipping address, wrapping a raw params Hash in a
StreetAddress value object first.

Parameters:



83
84
85
# File 'app/forms/checkout/checkout_form.rb', line 83

def shipping_address=(value)
  super(value.is_a?(Hash) ? StreetAddress.new(street_address_params(value)) : value)
end