Class: Checkout::CheckoutForm
- Inherits:
-
Object
- Object
- Checkout::CheckoutForm
- 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
-
#billing_address ⇒ Object
readonly
Requires billing address, phone, and email to be present.
-
#contact_name ⇒ Object
readonly
Requires a contact name.
-
#email ⇒ Object
readonly
Validates the email address format.
-
#phone ⇒ Object
readonly
Validates the phone number format.
Class Method Summary collapse
-
.get_address_type(address) ⇒ String
Resolves the address-type key for a persisted address.
-
.new_from_amazon_checkout_session(_session_id) ⇒ nil
Placeholder for building a form from an Amazon checkout session.
-
.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.
Instance Method Summary collapse
-
#billing_address(value) ⇒ void
readonly
Assigns the billing address, wrapping a raw params Hash in a StreetAddress value object first.
-
#homeowner? ⇒ Boolean
Whether the checkout is for a homeowner (no company name given).
-
#persisted? ⇒ false
Form object is never backed by a persisted record.
-
#profile_collection_for_select ⇒ Array<Array(String, Integer)>
Profiles for the trade select dropdown on the checkout form.
-
#retrieve_shipping_methods ⇒ nil
Placeholder proxy for shipping method options.
-
#save ⇒ Boolean
Backward compatible save method.
-
#save! ⇒ Boolean
Validates and persists the checkout: updates the customer, validates and saves addresses, and finalizes the cart, all inside a transaction.
-
#shipping_address=(value) ⇒ void
Assigns the shipping address, wrapping a raw params Hash in a StreetAddress value object first.
Instance Attribute Details
#billing_address ⇒ Object (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_name ⇒ Object (readonly)
Requires a contact name.
Validations:
92 |
# File 'app/forms/checkout/checkout_form.rb', line 92 validates :contact_name, presence: true |
#email ⇒ Object (readonly)
Validates the email address format.
Validations:
- Email_format
- Presence
90 |
# File 'app/forms/checkout/checkout_form.rb', line 90 validates :email, email_format: true |
#phone ⇒ Object (readonly)
Validates the phone number format.
Validations:
- Phone_format
- Presence
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.
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.
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.
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.
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).
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.
191 192 193 |
# File 'app/forms/checkout/checkout_form.rb', line 191 def persisted? false end |
#profile_collection_for_select ⇒ Array<Array(String, Integer)>
Profiles for the trade select dropdown on the checkout form.
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_methods ⇒ nil
Placeholder proxy for shipping method options. Returns nil.
198 199 200 |
# File 'app/forms/checkout/checkout_form.rb', line 198 def retrieve_shipping_methods # Proxy for shipping method options end |
#save ⇒ Boolean
Backward compatible save method.
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.
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..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.
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 |