Simplify generating fields with hints
We were already using this code in translatable forms. Using it on every form means we can reduce the code we need to generate a field with a hint.
This commit is contained in:
@@ -1,10 +1,4 @@
|
||||
class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
|
||||
def cktext_area(attribute, options)
|
||||
field(attribute, options) do |opts|
|
||||
super(attribute, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def enum_select(attribute, options = {}, html_options = {})
|
||||
choices = object.class.send(attribute.to_s.pluralize).keys.map do |name|
|
||||
[object.class.human_attribute_name("#{attribute}.#{name}"), name]
|
||||
@@ -12,4 +6,24 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
|
||||
|
||||
select attribute, choices, options, html_options
|
||||
end
|
||||
|
||||
%i[text_field text_area cktext_area number_field password_field email_field].each do |field|
|
||||
define_method field do |attribute, options = {}|
|
||||
label_with_hint(attribute, options) +
|
||||
super(attribute, options.merge(label: false, hint: false))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def label_with_hint(attribute, options)
|
||||
custom_label(attribute, options[:label], options[:label_options]) +
|
||||
help_text(options[:hint])
|
||||
end
|
||||
|
||||
def help_text(text)
|
||||
if text
|
||||
content_tag :span, text, class: "help-text"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user