Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXPERIMENTAL] Allow to render translations fields separately #62

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions app/helpers/translatable_form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def highlight_translation_html_class
class TranslatableFormBuilder < ConsulFormBuilder
attr_accessor :translations

def initialize(*args)
@emitted_hidden_fields = {}
super(*args)
end

def translatable_fields(&block)
@translations = {}
visible_locales.map do |locale|
Expand All @@ -35,21 +40,26 @@ def translatable_fields(&block)
def fields_for_locale(locale)
fields_for_translation(@translations[locale]) do |translations_form|
@template.tag.div translations_options(translations_form.object, locale) do
@template.concat translations_form.hidden_field(
:_destroy,
value: [email protected]_locale?(translations_form.object.globalized_model, locale),
data: { locale: locale }
)

@template.concat translations_form.hidden_field(:locale, value: locale)
translation_hidden_fields(translations_form, locale) unless @emitted_hidden_fields[locale]

yield translations_form
end
end
end

def translation_hidden_fields(form, locale)
@template.concat form.hidden_field(
:_destroy,
value: [email protected]_locale?(form.object.globalized_model, locale),
data: { locale: locale }
)
@template.concat form.hidden_field(:locale, value: locale)
@emitted_hidden_fields[locale] = true
end

def fields_for_translation(translation)
fields_for(:translations, translation, builder: TranslationsFieldsBuilder) do |f|
index = I18n.available_locales.find_index(translation.locale)
fields_for(:translations, translation, builder: TranslationsFieldsBuilder, child_index: index) do |f|
yield f
end
end
Expand Down