Class: CreditApplicationForm
- Inherits:
-
Object
- Object
- CreditApplicationForm
- Defined in:
- app/forms/credit_application_form.rb
Overview
Form object for creating a CreditApplication along with its related
models: Customer, mailing + billing Addresses, and up to 4 Contacts
(primary, owner, purchasing agent, accounts payable).
Usage in CreditApplicationsController:
new action — build empty form for the view
@form = CreditApplicationForm.new
@form.prepare_for_new(customer: @customer, ...)
create action — build from params, validate, save
@form = CreditApplicationForm.new
@form.prepare_for_create(params: ..., customer: ..., ...)
if @form.save
redirect_to ...
else
@form.prepare_for_redisplay
render :new
end
Instance Attribute Summary collapse
-
#accounts_payable_contact ⇒ Object
readonly
Returns the value of attribute accounts_payable_contact.
-
#billing_address ⇒ Object
readonly
Returns the value of attribute billing_address.
-
#credit_application ⇒ Object
readonly
Returns the value of attribute credit_application.
-
#customer ⇒ Object
readonly
Returns the value of attribute customer.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#existing_contacts ⇒ Object
readonly
Returns the value of attribute existing_contacts.
-
#mailing_address ⇒ Object
readonly
Returns the value of attribute mailing_address.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#primary_contact ⇒ Object
readonly
Returns the value of attribute primary_contact.
-
#purchasing_agent ⇒ Object
readonly
Returns the value of attribute purchasing_agent.
-
#same_addresses ⇒ Object
readonly
Returns the value of attribute same_addresses.
Instance Method Summary collapse
-
#initialize ⇒ void
constructor
Initializes an empty form with no contacts and a fresh errors object.
-
#prepare_for_create(credit_application_params:, customer_params:, mailing_address_params:, billing_address_params:, primary_contact_params:, owner_params:, purchasing_agent_params:, accounts_payable_contact_params:, customer:, existing_contacts: [], same_addresses: false) ⇒ CreditApplicationForm
Builds models from the submitted params for the
createaction. -
#prepare_for_new(customer:, existing_contacts: [], same_addresses: false) ⇒ CreditApplicationForm
Builds empty models for the
newaction's view. -
#prepare_for_redisplay ⇒ void
Re-prepare the form for redisplay after a failed save.
-
#reference_number ⇒ String?
Returns the reference number of the credit application, if built.
-
#save ⇒ Boolean
Validates all models, then persists the customer and credit application in a transaction.
Constructor Details
#initialize ⇒ void
Initializes an empty form with no contacts and a fresh errors object.
28 29 30 31 32 |
# File 'app/forms/credit_application_form.rb', line 28 def initialize @existing_contacts = [] @same_addresses = false @errors = ActiveModel::Errors.new(self) end |
Instance Attribute Details
#accounts_payable_contact ⇒ Object (readonly)
Returns the value of attribute accounts_payable_contact.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def accounts_payable_contact @accounts_payable_contact end |
#billing_address ⇒ Object (readonly)
Returns the value of attribute billing_address.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def billing_address @billing_address end |
#credit_application ⇒ Object (readonly)
Returns the value of attribute credit_application.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def credit_application @credit_application end |
#customer ⇒ Object (readonly)
Returns the value of attribute customer.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def customer @customer end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def errors @errors end |
#existing_contacts ⇒ Object (readonly)
Returns the value of attribute existing_contacts.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def existing_contacts @existing_contacts end |
#mailing_address ⇒ Object (readonly)
Returns the value of attribute mailing_address.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def mailing_address @mailing_address end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def owner @owner end |
#primary_contact ⇒ Object (readonly)
Returns the value of attribute primary_contact.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def primary_contact @primary_contact end |
#purchasing_agent ⇒ Object (readonly)
Returns the value of attribute purchasing_agent.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def purchasing_agent @purchasing_agent end |
#same_addresses ⇒ Object (readonly)
Returns the value of attribute same_addresses.
23 24 25 |
# File 'app/forms/credit_application_form.rb', line 23 def same_addresses @same_addresses end |
Instance Method Details
#prepare_for_create(credit_application_params:, customer_params:, mailing_address_params:, billing_address_params:, primary_contact_params:, owner_params:, purchasing_agent_params:, accounts_payable_contact_params:, customer:, existing_contacts: [], same_addresses: false) ⇒ CreditApplicationForm
Builds models from the submitted params for the create action.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/forms/credit_application_form.rb', line 73 def prepare_for_create(credit_application_params:, customer_params:, mailing_address_params:, billing_address_params:, primary_contact_params:, owner_params:, purchasing_agent_params:, accounts_payable_contact_params:, customer:, existing_contacts: [], same_addresses: false) @customer = customer @existing_contacts = existing_contacts @same_addresses = same_addresses @raw_ca_params = credit_application_params @raw_mailing_params = mailing_address_params @raw_billing_params = billing_address_params @credit_application = CreditApplication.new(credit_application_params) build_contacts_from_params( primary_contact_params:, owner_params:, purchasing_agent_params:, accounts_payable_contact_params: ) @mailing_address = Address.new(mailing_address_params) @billing_address = Address.new(billing_address_params) @customer.full_name = customer_params[:full_name] @customer.profile_id = customer_params[:profile_id] @customer.mailing_address = @mailing_address if @customer.mailing_address.nil? @customer.email = @primary_contact.email if @customer.email.nil? @credit_application.customer = @customer self end |
#prepare_for_new(customer:, existing_contacts: [], same_addresses: false) ⇒ CreditApplicationForm
Builds empty models for the new action's view.
44 45 46 47 48 49 50 51 52 53 |
# File 'app/forms/credit_application_form.rb', line 44 def prepare_for_new(customer:, existing_contacts: [], same_addresses: false) @customer = customer @existing_contacts = existing_contacts @same_addresses = same_addresses @credit_application = CreditApplication.new build_empty_contacts build_empty_addresses build_default_contact_points self end |
#prepare_for_redisplay ⇒ void
This method returns an undefined value.
Re-prepare the form for redisplay after a failed save
147 148 149 150 |
# File 'app/forms/credit_application_form.rb', line 147 def prepare_for_redisplay build_default_contact_points reset_use_primary_ids end |
#reference_number ⇒ String?
Returns the reference number of the credit application, if built.
140 141 142 |
# File 'app/forms/credit_application_form.rb', line 140 def reference_number @credit_application&.reference_number end |
#save ⇒ Boolean
Validates all models, then persists the customer and credit application
in a transaction.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/forms/credit_application_form.rb', line 114 def save @customer.valid? @credit_application.valid? @primary_contact.valid? if @credit_application.primary_contact_id.blank? unless @customer.valid? && @credit_application.valid? merge_errors_from(@customer, @credit_application) return false end resolve_addresses resolve_contacts ApplicationRecord.transaction do @customer.save! @credit_application.save! end true rescue ActiveRecord::RecordInvalid => e errors.add(:base, e.) false end |