Files
nairobi/app/helpers/translatable_form_helper.rb
Javi Martín 5cdda12902 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`.
2018-10-22 15:43:28 +02:00

56 lines
1.9 KiB
Ruby

module TranslatableFormHelper
def translatable_form_for(record, options = {})
form_for(record, options.merge(builder: TranslatableFormBuilder)) do |f|
yield(f)
end
end
def merge_translatable_field_options(options, locale)
options.merge(
class: "#{options[:class]} js-globalize-attribute".strip,
style: "#{options[:style]} #{display_translation?(locale)}".strip,
data: options.fetch(:data, {}).merge(locale: locale),
label_options: {
class: "#{options.dig(:label_options, :class)} js-globalize-attribute".strip,
style: "#{options.dig(:label_options, :style)} #{display_translation?(locale)}".strip,
data: (options.dig(:label_options, :data) || {}) .merge(locale: locale)
}
)
end
class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder
def translatable_fields(&block)
@object.globalize_locales.map do |locale|
Globalize.with_locale(locale) do
fields_for(:translations, @object.translations.where(locale: locale).first_or_initialize, builder: TranslationsFieldsBuilder) do |translations_form|
@template.concat translations_form.hidden_field(
:_destroy,
value: !@template.enable_locale?(@object, locale),
class: "destroy_locale",
data: { locale: locale })
@template.concat translations_form.hidden_field(:locale, value: locale)
yield translations_form
end
end
end.join.html_safe
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
def translations_options(options)
@template.merge_translatable_field_options(options, @object.locale)
end
end
end