-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcontact_details_input.rb
More file actions
73 lines (55 loc) · 2.35 KB
/
contact_details_input.rb
File metadata and controls
73 lines (55 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Forms::ContactDetailsInput < BaseInput
attr_accessor :form, :email, :phone, :link_text, :link_href, :contact_details_supplied, :current_user
EMAIL_REGEX = /.*@.*/
validates :email, presence: true, format: { with: EMAIL_REGEX, message: :invalid_email }, if: -> { supplied :supply_email }
validates :email, allowed_email_domain: true, if: -> { supplied :supply_email }
validates :phone, presence: true, length: { maximum: 500 }, if: -> { supplied :supply_phone }
validates :link_href, presence: true, url: true, length: { maximum: 120 }, if: -> { supplied :supply_link }
validates :link_text, presence: true, length: { maximum: 120 }, if: -> { supplied :supply_link }
def must_be_supply_contact_details
errors.add(:contact_details_supplied, :must_be_supply_contact_details) if contact_details_supplied.reject(&:blank?).empty?
end
def initialize(attrs = {})
attrs.deep_symbolize_keys!
super(attrs)
# Update the values for the checkboxes, so they are shown ticked in error
# state
@contact_details_supplied = []
@contact_details_supplied = attrs[:contact_details_supplied].compact.map(&:to_sym) if attrs.key?(:contact_details_supplied)
end
def submit
form.support_email = nil
form.support_phone = nil
form.support_url = nil
form.support_url_text = nil
return false if invalid?
form.support_email = email if supplied(:supply_email)
form.support_phone = phone if supplied(:supply_phone)
form.support_url = link_href if supplied(:supply_link)
form.support_url_text = link_text if supplied(:supply_link)
FormRepository.save!(form)
end
def assign_form_values
self.contact_details_supplied |= [ :supply_email ] if form.support_email.present?
self.contact_details_supplied |= [ :supply_phone ] if form.support_phone.present?
self.contact_details_supplied |= [ :supply_link ] if form.support_url.present?
self.email = form.support_email
self.phone = form.support_phone
self.link_href = form.support_url
self.link_text = form.support_url_text
self
end
def check_email?
email.present? || supplied(:supply_email)
end
def check_phone?
phone.present? || supplied(:supply_phone)
end
def check_link?
link_href.present? || supplied(:supply_link)
end
private
def supplied(field)
contact_details_supplied.map(&:to_sym).include? field
end
end