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

#initializeCreditApplicationForm

Returns a new instance of CreditApplicationForm.



25
26
27
28
29
# File 'app/forms/credit_application_form.rb', line 25

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


create action: build models from submitted params



50
51
52
53
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
# File 'app/forms/credit_application_form.rb', line 50

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



35
36
37
38
39
40
41
42
43
44
# File 'app/forms/credit_application_form.rb', line 35

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_redisplayObject

Re-prepare the form for redisplay after a failed save



114
115
116
117
# File 'app/forms/credit_application_form.rb', line 114

def prepare_for_redisplay
  build_default_contact_points
  reset_use_primary_ids
end

#reference_numberObject



109
110
111
# File 'app/forms/credit_application_form.rb', line 109

def reference_number
  @credit_application&.reference_number
end

#saveObject


save: validate all models, then persist in a transaction



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/forms/credit_application_form.rb', line 86

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