diff --git a/app/helpers/globalize_helper.rb b/app/helpers/globalize_helper.rb
index d82fe2e43..07cf220b1 100644
--- a/app/helpers/globalize_helper.rb
+++ b/app/helpers/globalize_helper.rb
@@ -10,12 +10,17 @@ module GlobalizeHelper
end
end
- def display_translation?(locale)
- locale == I18n.locale
+ def display_translation?(resource, locale)
+ if !resource || resource.translations.blank? ||
+ resource.translations.map(&:locale).include?(I18n.locale)
+ locale == I18n.locale
+ else
+ locale == resource.translations.first.locale
+ end
end
- def display_translation_style(locale)
- "display: none;" unless display_translation?(locale)
+ def display_translation_style(resource, locale)
+ "display: none;" unless display_translation?(resource, locale)
end
def translation_enabled_tag(locale, enabled)
@@ -29,11 +34,12 @@ module GlobalizeHelper
def enable_locale?(resource, locale)
# Use `map` instead of `pluck` in order to keep the `params` sent
# by the browser when there's invalid data
- resource.translations.reject(&:_destroy).map(&:locale).include?(locale) || locale == I18n.locale
+ (resource.translations.blank? && locale == I18n.locale) ||
+ resource.translations.reject(&:_destroy).map(&:locale).include?(locale)
end
- def highlight_class(locale)
- "is-active" if display_translation?(locale)
+ def highlight_class(resource, locale)
+ "is-active" if display_translation?(resource, locale)
end
def globalize(locale, &block)
diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb
index 136c518ce..77568fcb6 100644
--- a/app/helpers/translatable_form_helper.rb
+++ b/app/helpers/translatable_form_helper.rb
@@ -74,7 +74,7 @@ module TranslatableFormHelper
end
def display_style
- @template.display_translation_style(locale)
+ @template.display_translation_style(@object.globalized_model, locale)
end
private
diff --git a/app/views/admin/shared/_globalize_locales.html.erb b/app/views/admin/shared/_globalize_locales.html.erb
index 90315a11b..ccce00524 100644
--- a/app/views/admin/shared/_globalize_locales.html.erb
+++ b/app/views/admin/shared/_globalize_locales.html.erb
@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}",
- style: display_translation_style(locale),
+ style: display_translation_style(resource, locale),
class: 'float-right delete js-delete-language',
data: { locale: locale } %>
@@ -12,7 +12,7 @@
<%= link_to name_for_locale(locale), "#",
style: enable_translation_style(resource, locale),
- class: "js-globalize-locale-link #{highlight_class(locale)}",
+ class: "js-globalize-locale-link #{highlight_class(resource, locale)}",
data: { locale: locale },
remote: true %>
diff --git a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
index dcfd9e395..722e34d13 100644
--- a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
+++ b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}",
- style: display_translation_style(locale),
+ style: site_customization_display_translation_style(locale),
class: 'float-right delete js-delete-language',
data: { locale: locale } %>
@@ -12,7 +12,7 @@
<%= link_to name_for_locale(locale), "#",
style: site_customization_display_translation_style(locale),
- class: "js-globalize-locale-link #{highlight_class(locale)}",
+ class: "js-globalize-locale-link #{highlight_class(nil, locale)}",
data: { locale: locale },
remote: true %>
diff --git a/config/initializers/globalize.rb b/config/initializers/globalize.rb
new file mode 100644
index 000000000..d0836d0cc
--- /dev/null
+++ b/config/initializers/globalize.rb
@@ -0,0 +1,13 @@
+module Globalize
+ module ActiveRecord
+ module InstanceMethods
+ def save(*)
+ # Credit for this code belongs to Jack Tomaszewski:
+ # https://github.com/globalize/globalize/pull/578
+ Globalize.with_locale(Globalize.locale || I18n.default_locale) do
+ super
+ end
+ end
+ end
+ end
+end
diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb
index 7a0e7d76e..9c581e03b 100644
--- a/spec/features/admin/banners_spec.rb
+++ b/spec/features/admin/banners_spec.rb
@@ -110,6 +110,33 @@ feature 'Admin banners magement' do
expect(page).to have_link 'Such banner many text wow link', href: 'https://www.url.com'
end
+ scenario "Publish a banner with a translation different than the current locale", :js do
+ visit new_admin_banner_path
+
+ expect(page).to have_link "English"
+
+ click_link "Remove language"
+ select "Français", from: "translation_locale"
+
+ fill_in "Title", with: "En Français"
+ fill_in "Description", with: "Link en Français"
+
+ fill_in "Link", with: "https://www.url.com"
+
+ last_week = Time.current - 1.week
+ next_week = Time.current + 1.week
+
+ fill_in "Post started at", with: last_week.strftime("%d/%m/%Y")
+ fill_in "Post ended at", with: next_week.strftime("%d/%m/%Y")
+
+ click_button "Save changes"
+ click_link "Edit banner"
+
+ expect(page).to have_link "Français"
+ expect(page).not_to have_link "English"
+ expect(page).to have_field "Title", with: "En Français"
+ end
+
scenario "Update banner color when changing from color picker or text_field", :js do
visit new_admin_banner_path