Class: CreditApplicationForm

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializevoid

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_contactObject (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_addressObject (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_applicationObject (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

#customerObject (readonly)

Returns the value of attribute customer.



23
24
25
# File 'app/forms/credit_application_form.rb', line 23

def customer
  @customer
end

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'app/forms/credit_application_form.rb', line 23

def errors
  @errors
end

#existing_contactsObject (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_addressObject (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

#ownerObject (readonly)

Returns the value of attribute owner.



23
24
25
# File 'app/forms/credit_application_form.rb', line 23

def owner
  @owner
end

#primary_contactObject (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_agentObject (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_addressesObject (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.

Parameters:

  • credit_application_params (Hash)

    permitted params for the CreditApplication

  • customer_params (Hash)

    permitted params for the Customer (full_name, profile_id)

  • mailing_address_params (Hash)

    permitted params for the mailing Address

  • billing_address_params (Hash)

    permitted params for the billing Address

  • primary_contact_params (Hash, nil)

    permitted params for the primary Contact

  • owner_params (Hash, nil)

    permitted params for the owner Contact

  • purchasing_agent_params (Hash, nil)

    permitted params for the purchasing agent Contact

  • accounts_payable_contact_params (Hash, nil)

    permitted params for the accounts payable Contact

  • customer (Customer)

    the customer applying for credit

  • existing_contacts (Array<Contact>) (defaults to: [])

    contacts already attached to the customer

  • same_addresses (Boolean) (defaults to: false)

    whether billing and mailing addresses are the same

Returns:



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.

Parameters:

  • customer (Customer)

    the customer applying for credit

  • existing_contacts (Array<Contact>) (defaults to: [])

    contacts already attached to the customer

  • same_addresses (Boolean) (defaults to: false)

    whether billing and mailing addresses are the same

Returns:



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_redisplayvoid

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_numberString?

Returns the reference number of the credit application, if built.

Returns:



140
141
142
# File 'app/forms/credit_application_form.rb', line 140

def reference_number
  @credit_application&.reference_number
end

#saveBoolean

Validates all models, then persists the customer and credit application
in a transaction.

Returns:

  • (Boolean)

    true when both records saved, false when validation or
    persistence failed (errors are merged into #errors)



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.message)
  false
end