diff --git a/app/helpers/globalize_helper.rb b/app/helpers/globalize_helper.rb index 3dcd9520d..94fb26365 100644 --- a/app/helpers/globalize_helper.rb +++ b/app/helpers/globalize_helper.rb @@ -14,14 +14,6 @@ module GlobalizeHelper same_locale?(neutral_locale(I18n.locale), neutral_locale(locale)) ? "" : "display: none" end - def render_translations_to_delete(resource) - capture do - resource.globalize_locales.each do |locale| - concat translation_enabled_tag(locale, enable_locale?(resource, locale)) - end - end - end - def translation_enabled_tag(locale, enabled) hidden_field_tag("enabled_translations[#{locale}]", (enabled ? 1 : 0)) end diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb new file mode 100644 index 000000000..fed33070e --- /dev/null +++ b/app/helpers/translatable_form_helper.rb @@ -0,0 +1,43 @@ +module TranslatableFormHelper + def translatable_form_for(record, options = {}) + object = record.is_a?(Array) ? record.last : record + + form_for(record, options.merge(builder: TranslatableFormBuilder)) do |f| + + object.globalize_locales.each do |locale| + concat translation_enabled_tag(locale, enable_locale?(object, locale)) + end + + yield(f) + end + end +end + +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 + + private + + def translatable_field(field_type, method, options = {}) + @template.capture do + @object.globalize_locales.each do |locale| + Globalize.with_locale(locale) do + final_options = options.merge( + class: (options.fetch(:class, "") + " js-globalize-attribute"), + style: @template.display_translation?(locale), + data: options.fetch(:data, {}).merge(locale: locale), + label: false + ) + + @template.concat send(field_type, "#{method}_#{locale}", final_options) + end + end + end + end +end diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 105a872fb..b14d9280b 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -1,11 +1,9 @@ <%= render "admin/shared/globalize_locales", resource: @banner %> -<%= form_for [:admin, @banner] do |f| %> +<%= translatable_form_for [:admin, @banner] do |f| %> <%= render 'errors' %> - <%= render_translations_to_delete(@banner) %> -
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
@@ -32,18 +30,9 @@
<%= f.label :title, t("admin.banners.banner.title") %> - - <% @banner.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_field "title_#{locale}", - placeholder: t("admin.banners.banner.title"), - class: "js-globalize-attribute", - data: {js_banner_title: "js_banner_title", - locale: locale}, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_field :title, + placeholder: t("admin.banners.banner.title"), + data: {js_banner_title: "js_banner_title"}%>
@@ -57,17 +46,9 @@
<%= f.label :description, t("admin.banners.banner.description") %> - <% @banner.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_field "description_#{locale}", - placeholder: t("admin.banners.banner.description"), - class: "js-globalize-attribute", - data: {js_banner_description: "js_banner_description", - locale: locale}, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_field :description, + placeholder: t("admin.banners.banner.description"), + data: {js_banner_description: "js_banner_description"} %>
diff --git a/app/views/admin/budget_investment_milestones/_form.html.erb b/app/views/admin/budget_investment_milestones/_form.html.erb index 5598a51cd..790d50780 100644 --- a/app/views/admin/budget_investment_milestones/_form.html.erb +++ b/app/views/admin/budget_investment_milestones/_form.html.erb @@ -1,8 +1,6 @@ <%= render "admin/shared/globalize_locales", resource: @milestone %> -<%= form_for [:admin, @investment.budget, @investment, @milestone] do |f| %> - - <%= render_translations_to_delete(@milestone) %> +<%= translatable_form_for [:admin, @investment.budget, @investment, @milestone] do |f| %> <%= f.hidden_field :title, value: l(Time.current, format: :datetime), maxlength: Budget::Investment::Milestone.title_max_length %> @@ -17,15 +15,7 @@
<%= f.label :description, t("admin.milestones.new.description") %> - <% @milestone.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_area "description_#{locale}", rows: 5, - class: "js-globalize-attribute", - data: { locale: locale }, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_field :description, rows: 5 %> <%= f.label :publication_date, t("admin.milestones.new.date") %> <%= f.text_field :publication_date,