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:
Javi Martín
2019-10-03 02:25:14 +02:00
parent ae6fab9f5e
commit 4a19bb6b77
8 changed files with 39 additions and 70 deletions

View File

@@ -95,30 +95,8 @@ module TranslatableFormHelper
end
class TranslationsFieldsBuilder < ConsulFormBuilder
%i[text_field text_area cktext_area].each do |field|
define_method field do |attribute, options = {}|
custom_label(attribute, options[:label], options[:label_options]) +
help_text(options[:hint]) +
super(attribute, options.merge(label: false, hint: false))
end
end
def locale
@object.locale
end
def label(attribute, text = nil, options = {})
label_options = options.dup
hint = label_options.delete(:hint)
super(attribute, text, label_options) + help_text(hint)
end
private
def help_text(text)
if text
content_tag :span, text, class: "help-text"
end
end
end
end

View File

@@ -24,30 +24,20 @@
<div class="row">
<%= f.translatable_fields do |translations_form| %>
<div class="small-12 column">
<%= f.label :description, t("admin.budget_phases.edit.description") %>
<span class="help-text" id="phase-description-help-text">
<%= t("admin.budget_phases.edit.description_help_text") %>
</span>
<div class="ckeditor">
<%= translations_form.cktext_area :description,
maxlength: Budget::Phase::DESCRIPTION_MAX_LENGTH,
label: false %>
label: t("admin.budget_phases.edit.description"),
hint: t("admin.budget_phases.edit.description_help_text") %>
</div>
</div>
<div class="small-12 column">
<%= f.label :summary, t("admin.budget_phases.edit.summary") %>
<span class="help-text" id="phase-summary-help-text">
<%= t("admin.budget_phases.edit.summary_help_text") %>
</span>
<div class="ckeditor">
<%= translations_form.cktext_area :summary,
maxlength: Budget::Phase::SUMMARY_MAX_LENGTH,
label: false %>
label: t("admin.budget_phases.edit.summary"),
hint: t("admin.budget_phases.edit.summary_help_text") %>
</div>
</div>
<% end %>

View File

@@ -47,21 +47,18 @@
<div class="row expanded margin-top">
<div class="small-12 medium-4 column">
<%= f.label :day_offset %>
<p class="help-text"><%= t("admin.dashboard.actions.form.help_text") %></p>
<%= f.number_field :day_offset, label: false, step: 1, min: 0 %>
<%= f.number_field :day_offset, step: 1, min: 0,
hint: t("admin.dashboard.actions.form.help_text") %>
</div>
<div class="small-12 medium-4 column">
<%= f.label :required_supports %>
<p class="help-text"><%= t("admin.dashboard.actions.form.help_text") %></p>
<%= f.number_field :required_supports, label: false, step: 1, min: 0 %>
<%= f.number_field :required_supports, step: 1, min: 0,
hint: t("admin.dashboard.actions.form.help_text") %>
</div>
<div class="small-12 medium-4 column">
<%= f.label :order %>
<p class="help-text"><%= t("admin.dashboard.actions.form.help_text") %></p>
<%= f.number_field :order, label: false, step: 1, min: 0 %>
<%= f.number_field :order, step: 1, min: 0,
hint: t("admin.dashboard.actions.form.help_text") %>
</div>
</div>

View File

@@ -8,22 +8,16 @@
<div class="clear">
<div class="small-12 medium-6 large-4 column">
<%= f.label :census_code %>
<p class="help-text"><%= t("admin.geozones.geozone.code_help") %></p>
<%= f.text_field :census_code, label: false %>
<%= f.text_field :census_code, hint: t("admin.geozones.geozone.code_help") %>
</div>
<div class="small-12 medium-6 large-4 column end">
<%= f.label :external_code %>
<p class="help-text"><%= t("admin.geozones.geozone.code_help") %></p>
<%= f.text_field :external_code, label: false %>
<%= f.text_field :external_code, hint: t("admin.geozones.geozone.code_help") %>
</div>
</div>
<div class="small-12 large-8 column">
<%= f.label :html_map_coordinates %>
<p class="help-text"><%= t("admin.geozones.geozone.coordinates_help") %></p>
<%= f.text_field :html_map_coordinates, label: false %>
<%= f.text_field :html_map_coordinates, hint: t("admin.geozones.geozone.coordinates_help") %>
</div>
<div class="small-12 column">

View File

@@ -16,10 +16,9 @@
<% end %>
<div class="small-12 medium-8 column">
<%= f.label :custom_list, t("admin.legislation.proposals.form.custom_categories") %>
<span class="help-text"><%= t("admin.legislation.proposals.form.custom_categories_description") %></span>
<%= f.text_field :custom_list, value: @process.tag_list_on(:customs).to_s,
label: false,
label: t("admin.legislation.proposals.form.custom_categories"),
hint: t("admin.legislation.proposals.form.custom_categories_description"),
placeholder: t("admin.legislation.proposals.form.custom_categories_placeholder"),
class: "js-tag-list",
aria: { describedby: "tag-list-help-text" } %>

View File

@@ -14,10 +14,8 @@
<%= f.text_field :signable_id %>
</div>
<%= f.label :required_fields_to_verify %>
<p class="help-text" id="required-fields-to-verify-help-text"><%= required_fields_to_verify_text_help %></p>
<p class="help-text"> <%= example_text_help %></p>
<%= f.text_area :required_fields_to_verify, rows: "6", label: false, aria: { describedby: "required-fields-to-verify-help-text" } %>
<%= f.text_area :required_fields_to_verify, rows: "6",
hint: sanitize("#{required_fields_to_verify_text_help}<br/>#{example_text_help}") %>
<%= f.submit(class: "button", value: t("admin.signature_sheets.new.submit")) %>
<% end %>

View File

@@ -32,9 +32,8 @@
</div>
<div class="small-12 medium-6 column">
<%= f.label :slug %>
<p class="help-text"><%= t("admin.site_customization.pages.new.slug_help_html") %>
<%= f.text_field :slug, label: false, size: 80, maxlength: 80 %>
<%= f.text_field :slug, size: 80, maxlength: 80,
hint: t("admin.site_customization.pages.new.slug_help_html") %>
</div>
</div>

View File

@@ -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