Extract translatable field logic to FormBuilder
This commit is contained in:
@@ -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
|
||||
|
||||
43
app/helpers/translatable_form_helper.rb
Normal file
43
app/helpers/translatable_form_helper.rb
Normal file
@@ -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
|
||||
@@ -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) %>
|
||||
|
||||
<div class="row">
|
||||
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
|
||||
<div class="small-12 medium-3 column">
|
||||
@@ -32,18 +30,9 @@
|
||||
<div class="row">
|
||||
<div class="small-12 medium-6 column">
|
||||
<%= 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"}%>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-6 column">
|
||||
@@ -57,17 +46,9 @@
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<%= 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"} %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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 @@
|
||||
</div>
|
||||
|
||||
<%= 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,
|
||||
|
||||
Reference in New Issue
Block a user