Simplify passing the locale to translatable fields

Creating a new form builder might be too much. My idea was so the view
uses more or less the same syntax it would use with Rails' default
builder, and so we can use `text_field` instead of
`translatable_text_field`.
This commit is contained in:
Javi Martín
2018-10-07 22:13:34 +02:00
parent 71601bd3f8
commit 5cdda12902
2 changed files with 16 additions and 24 deletions

View File

@@ -19,23 +19,10 @@ module TranslatableFormHelper
end end
class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder
def translatable_text_field(method, options = {})
translatable_field(:text_field, method, options)
end
def translatable_text_area(method, options = {})
translatable_field(:text_area, method, options)
end
def translatable_cktext_area(method, options = {})
translatable_field(:cktext_area, method, options)
end
def translatable_fields(&block) def translatable_fields(&block)
@object.globalize_locales.map do |locale| @object.globalize_locales.map do |locale|
Globalize.with_locale(locale) do Globalize.with_locale(locale) do
fields_for(:translations, @object.translations.where(locale: locale).first_or_initialize) do |translations_form| fields_for(:translations, @object.translations.where(locale: locale).first_or_initialize, builder: TranslationsFieldsBuilder) do |translations_form|
@template.concat translations_form.hidden_field( @template.concat translations_form.hidden_field(
:_destroy, :_destroy,
value: !@template.enable_locale?(@object, locale), value: !@template.enable_locale?(@object, locale),
@@ -44,18 +31,25 @@ module TranslatableFormHelper
@template.concat translations_form.hidden_field(:locale, value: locale) @template.concat translations_form.hidden_field(:locale, value: locale)
yield translations_form, locale yield translations_form
end end
end end
end.join.html_safe end.join.html_safe
end end
end
class TranslationsFieldsBuilder < FoundationRailsHelper::FormBuilder
%i[text_field text_area cktext_area].each do |field|
define_method field do |attribute, options = {}|
super attribute, translations_options(options)
end
end
private private
def translatable_field(field_type, method, options = {}) def translations_options(options)
locale = options.delete(:locale) @template.merge_translatable_field_options(options, @object.locale)
final_options = @template.merge_translatable_field_options(options, locale)
send(field_type, method, final_options)
end end
end end
end end

View File

@@ -28,18 +28,16 @@
</div> </div>
<div class="row"> <div class="row">
<%= f.translatable_fields do |translations_form, locale| %> <%= f.translatable_fields do |translations_form| %>
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= translations_form.translatable_text_field :title, <%= translations_form.text_field :title,
locale: locale,
placeholder: t("admin.banners.banner.title"), placeholder: t("admin.banners.banner.title"),
data: {js_banner_title: "js_banner_title"}, data: {js_banner_title: "js_banner_title"},
label: t("admin.banners.banner.title") %> label: t("admin.banners.banner.title") %>
</div> </div>
<div class="small-12 column"> <div class="small-12 column">
<%= translations_form.translatable_text_field :description, <%= translations_form.text_field :description,
locale: locale,
placeholder: t("admin.banners.banner.description"), placeholder: t("admin.banners.banner.description"),
data: {js_banner_description: "js_banner_description"}, data: {js_banner_description: "js_banner_description"},
label: t("admin.banners.banner.description") %> label: t("admin.banners.banner.description") %>