-
Notifications
You must be signed in to change notification settings - Fork 21
/
base.rb
98 lines (87 loc) · 3.11 KB
/
base.rb
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
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
module GOVUKDesignSystemFormBuilder
class Base
delegate :content_tag, :safe_join, :tag, :link_to, :capture, to: :@builder
delegate :config, to: GOVUKDesignSystemFormBuilder
def initialize(builder, object_name, attribute_name, &block)
@builder = builder
@object_name = object_name
@attribute_name = attribute_name
@block_content = capture { block.call } if block_given?
end
# objects that implement #to_s can be passed directly into #safe_join
def to_s
html || ''
end
# returns the id value used for the input
#
# @note field_id is overridden so that the error summary can link to the
# correct element.
#
# It's straightforward for inputs with a single element (like a textarea
# or text input) but the GOV.UK Design System requires that the error
# summary link to the first checkbox or radio in a list, so additional
# logic is requred
#
# @return [String] the element's +id+
# @see https://design-system.service.gov.uk/components/error-summary/#linking-from-the-error-summary-to-each-answer
# GOV.UK linking to elements from the error summary
def field_id(link_errors: false)
if link_errors && has_errors?
build_id('field-error', include_value: false)
else
build_id('field')
end
end
private
# returns the attributes bound to the object that are
# required to build all contained elements
#
# @return [GOVUKDesignSystemFormBuilder::FormBuilder, Symbol, Symbol] an array containing the
# builder, object name and attribute name
def bound
[@builder, @object_name, @attribute_name]
end
def brand(override = nil)
override || config.brand
end
def has_errors?
@builder.object.respond_to?(:errors) &&
@builder.object.errors.any? &&
@builder.object.errors.messages[@attribute_name].present?
end
def combine_references(*ids)
ids.flatten.compact
end
# Builds the values used for HTML id attributes throughout the builder
#
# @param id_type [String] a description of the id's type, eg +hint+, +error+
# @param delimiter [String] the characters used to 'split' the output
# @param replace [String] the targets to be replaced by the delimiter
# @param attribute_name [String] overrides the object's +@attribute_name+
# @param include_value [Boolean] controls whether or not the value will form part
# of the final id
#
# @return [String] the id composed of object, attribute, value and type
#
# @example
# build_id('hint') #=> "person-name-hint"
def build_id(id_type, delimiter = '-', replace = '_', attribute_name: nil, include_value: true)
attribute = attribute_name || @attribute_name
value = include_value && @value || nil
[
@object_name,
attribute,
value,
id_type
]
.compact
.join(delimiter)
.parameterize
.tr(replace, delimiter)
end
def warn(message)
return unless config.enable_logger
Rails.logger.warn(message)
end
end
end