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 =
{
  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)



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

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

#contact_nameObject (readonly)



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

validates :contact_name, presence: true

#emailObject (readonly)



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

validates :email, email_format: true

#phoneObject (readonly)



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

validates :phone, phone_format: true

Class Method Details

.get_address_type(address) ⇒ Object

--- Class Methods ---



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

def self.get_address_type(address)
  address.determine_address_type
end

.new_from_amazon_checkout_session(_session_id) ⇒ Object



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

def self.new_from_amazon_checkout_session(_session_id); end

.new_from_cart(cart) ⇒ Object



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

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

Instance Method Details

#billing_address=(value) ⇒ Object (readonly)



65
66
67
# File 'app/forms/checkout/checkout_form.rb', line 65

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

#homeowner?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'app/forms/checkout/checkout_form.rb', line 155

def homeowner?
  company_name.blank?
end

#persisted?Boolean

--- Instance Methods ---

Returns:

  • (Boolean)


143
144
145
# File 'app/forms/checkout/checkout_form.rb', line 143

def persisted?
  false
end

#profile_collection_for_selectObject



151
152
153
# File 'app/forms/checkout/checkout_form.rb', line 151

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

#retrieve_shipping_methodsObject



147
148
149
# File 'app/forms/checkout/checkout_form.rb', line 147

def retrieve_shipping_methods
  # Proxy for shipping method options
end

#saveObject

Backward compatible save method



160
161
162
# File 'app/forms/checkout/checkout_form.rb', line 160

def save
  save!
end

#save!Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/forms/checkout/checkout_form.rb', line 164

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) ⇒ Object



69
70
71
# File 'app/forms/checkout/checkout_form.rb', line 69

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