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 ⇒ CreditApplicationForm
constructor
A new instance of CreditApplicationForm.
-
#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) ⇒ Object
------------------------------------------------------------------ create action: build models from submitted params ------------------------------------------------------------------.
-
#prepare_for_new(customer:, existing_contacts: [], same_addresses: false) ⇒ Object
------------------------------------------------------------------ new action: build empty models for the view ------------------------------------------------------------------.
-
#prepare_for_redisplay ⇒ Object
Re-prepare the form for redisplay after a failed save.
- #reference_number ⇒ Object
-
#save ⇒ Object
------------------------------------------------------------------ save: validate all models, then persist in a transaction ------------------------------------------------------------------.
Constructor Details
#initialize ⇒ CreditApplicationForm
Returns a new instance of CreditApplicationForm.
27 28 29 30 31 |
# File 'app/forms/credit_application_form.rb', line 27 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.
33 34 35 |
# File 'app/forms/credit_application_form.rb', line 33 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) ⇒ Object
create action: build models from submitted params
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/forms/credit_application_form.rb', line 54 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) ⇒ Object
new action: build empty models for the view
39 40 41 42 43 44 45 46 47 48 |
# File 'app/forms/credit_application_form.rb', line 39 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 ⇒ Object
Re-prepare the form for redisplay after a failed save
118 119 120 121 |
# File 'app/forms/credit_application_form.rb', line 118 def prepare_for_redisplay build_default_contact_points reset_use_primary_ids end |
#reference_number ⇒ Object
113 114 115 |
# File 'app/forms/credit_application_form.rb', line 113 def reference_number @credit_application&.reference_number end |
#save ⇒ Object
save: validate all models, then persist in a transaction
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/forms/credit_application_form.rb', line 90 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 |