Class: Anchor::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/anchor/form_builder.rb

Constant Summary collapse

DEFAULT_MAX_DATE =
"9999-12-31".freeze

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder.



7
8
9
10
11
12
13
14
15
# File 'app/helpers/anchor/form_builder.rb', line 7

def initialize(object_name, object, template, options, &)
  super(
    object_name,
    object,
    view_component?(template) ? template.helpers : template,
    options,
    &
  )
end

Instance Method Details

#autocomplete_field(attribute, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/helpers/anchor/form_builder.rb', line 17

def autocomplete_field(attribute, options = {})
  render AutocompleteComponent.new(
    form_builder: self,
    name: attribute,
    src: options.delete(:src),
    input_options: options.delete(:input_options) || {},
    list_options: options.delete(:list_options) || {}
  )
end

#check_box(attribute, options = {}, *args) ⇒ Object

The following methods are pending to be replaced with ViewComponents.



183
184
185
186
187
188
189
190
191
# File 'app/helpers/anchor/form_builder.rb', line 183

def check_box(attribute, options = {}, *args)
  super(
    attribute,
    options.deep_merge(
      data: { testid: "checkbox-#{attribute.to_s.dasherize}" }
    ),
    *args
  )
end

#collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



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
# File 'app/helpers/anchor/form_builder.rb', line 57

def collection_check_boxes(
  attribute,
  collection,
  value_method,
  text_method,
  options = {},
  html_options = {}
)
  render CheckBoxCollectionComponent.new(
    form_builder: self,
    attribute:,
    collection:,
    value_method:,
    text_method:,
    **options,
    **html_options
  ) do |component|
    super(
      attribute,
      collection,
      value_method,
      text_method,
      options,
      component.options.merge(html_options),
    ) do |check_box|
      render component.check_box(check_box:)
    end
  end
end

#collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



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
# File 'app/helpers/anchor/form_builder.rb', line 27

def collection_radio_buttons(
  attribute,
  collection,
  value_method,
  text_method,
  options = {},
  html_options = {}
)
  render RadioButtonCollectionComponent.new(
    form_builder: self,
    attribute:,
    collection:,
    value_method:,
    text_method:,
    **options,
    **html_options
  ) do |component|
    super(
      attribute,
      collection,
      value_method,
      text_method,
      options,
      component.options.merge(html_options),
    ) do |radio|
      render component.radio(radio:)
    end
  end
end

#date_field(attribute, options = {}) ⇒ Object



193
194
195
196
197
198
# File 'app/helpers/anchor/form_builder.rb', line 193

def date_field(attribute, options = {})
  super(attribute, options.merge(
    class: Anchor::InputComponent::INPUT_CLASSES,
    max: DEFAULT_MAX_DATE
  ))
end

#datetime_field(attribute, options = {}) ⇒ Object



200
201
202
203
204
205
# File 'app/helpers/anchor/form_builder.rb', line 200

def datetime_field(attribute, options = {})
  super(attribute, options.merge(
    class: Anchor::InputComponent::INPUT_CLASSES,
    max: DEFAULT_MAX_DATE
  ))
end

#email_field(attribute, options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/anchor/form_builder.rb', line 87

def email_field(attribute, options = {})
  render InputComponent.new(
    form_builder: self,
    attribute:,
    type: :email,
    starting_icon: options.delete(:starting_icon),
    ending_icon: options.delete(:ending_icon)
  ) do |component|
    super(attribute, component.options.merge(options))
  end
end

#error_message_for(attribute, options = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'app/helpers/anchor/form_builder.rb', line 99

def error_message_for(attribute, options = {})
  render ErrorMessageComponent.new(
    form_builder: self,
    attribute:,
    **options
  )
end

#fieldset(attribute, options = {}, &block) ⇒ Object



229
230
231
232
233
234
# File 'app/helpers/anchor/form_builder.rb', line 229

def fieldset(attribute, options = {}, &block)
  output = tag(:fieldset, options, true)
  output.safe_concat(legend(attribute))
  output.concat(capture(&block)) if block
  output.safe_concat("</fieldset>")
end

#file_field(attribute, options = {}) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/helpers/anchor/form_builder.rb', line 207

def file_field(attribute, options = {})
   :div, class: "mt-2" do
     :label, class: "btn" do
      concat I18n.t("actions.attach")
      concat super(
        attribute,
        {
          class: "hidden",
          accept: "application/pdf",
          direct_upload: true,
        }.merge(options)
      )
    end
  end
end

#hint(text) ⇒ Object



223
224
225
226
227
# File 'app/helpers/anchor/form_builder.rb', line 223

def hint(text)
   :p, class: "mt-2 text-secondary text-sm" do
    text
  end
end

#label(attribute, text = nil, options = {}, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/anchor/form_builder.rb', line 107

def label(attribute, text = nil, options = {}, &block)
  render LabelComponent.new(
    form_builder: self,
    attribute:,
    text:,
    **options
  ) do |component|
    if block.present?
      super
    else
      super(attribute, text, component.options.merge(options)) do |builder|
        capture do
          concat text || builder.translation
          concat component.optional_suffix
        end
      end
    end
  end
end

#legend(attribute, _options = {}) ⇒ Object



236
237
238
239
240
# File 'app/helpers/anchor/form_builder.rb', line 236

def legend(attribute, _options = {})
   :legend, class: %w(font-semibold text-base text-primary) do
    i18n_label attribute
  end
end

#number_field(attribute, options = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'app/helpers/anchor/form_builder.rb', line 127

def number_field(attribute, options = {})
  render InputComponent.new(
    form_builder: self,
    attribute:,
    type: :number,
    starting_icon: options.delete(:starting_icon),
    ending_icon: options.delete(:ending_icon)
  ) do |component|
    super(attribute, component.options.merge(options))
  end
end

#search_field(attribute, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'app/helpers/anchor/form_builder.rb', line 139

def search_field(attribute, options = {})
  render InputComponent.new(
    form_builder: self,
    attribute:,
    type: :search,
    starting_icon: options.delete(:starting_icon),
    ending_icon: options.delete(:ending_icon)
  ) do |component|
    super(attribute, component.options.merge(options))
  end
end

#select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/anchor/form_builder.rb', line 151

def select(attribute, choices = nil, options = {}, html_options = {}, &)
  render SelectComponent.new(
    form_builder: self,
    attribute:,
    choices:,
    show_marker: options.fetch(:show_marker, true),
    &
  ) do |component|
    super(
      attribute,
      choices,
      options,
      component.html_options.deep_merge(html_options),
      &
    )
  end
end

#text_area(attribute, options = {}) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'app/helpers/anchor/form_builder.rb', line 261

def text_area(attribute, options = {})
  super(
    attribute,
    options.merge(
      class: Anchor::InputComponent::INPUT_CLASSES + ["h-[120px]"],
      data: { testid: "text-area-#{attribute.to_s.dasherize}" }
    )
  )
end

#text_field(attribute, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'app/helpers/anchor/form_builder.rb', line 169

def text_field(attribute, options = {})
  render InputComponent.new(
    form_builder: self,
    attribute:,
    type: :text,
    starting_icon: options.delete(:starting_icon),
    ending_icon: options.delete(:ending_icon)
  ) do |component|
    super(attribute, component.options.merge(options))
  end
end

#typeahead_select(attribute, choices = nil, options = {}, html_options = {}) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/helpers/anchor/form_builder.rb', line 242

def typeahead_select(
  attribute,
  choices = nil,
  options = {},
  html_options = {},
  &
)
  html_options = html_options
    .deep_merge(data: { controller: "typeahead-select" })

  select(
    attribute,
    choices,
    options.merge(show_marker: false),
    html_options,
    &
  )
end