diff --git a/lib/tasks/globalize.rake b/lib/tasks/globalize.rake index 37acb06cb..69e10508e 100644 --- a/lib/tasks/globalize.rake +++ b/lib/tasks/globalize.rake @@ -22,8 +22,14 @@ namespace :globalize do model_class.find_each do |record| fields.each do |field| - if record.send(:"#{field}_#{I18n.locale}").blank? - record.send(:"#{field}_#{I18n.locale}=", record.untranslated_attributes[field.to_s]) + locale = if model_class == SiteCustomization::Page && record.locale.present? + record.locale + else + I18n.locale + end + + if record.send(:"#{field}_#{locale}").blank? + record.send(:"#{field}_#{locale}=", record.untranslated_attributes[field.to_s]) end end diff --git a/spec/lib/tasks/globalize_spec.rb b/spec/lib/tasks/globalize_spec.rb index 2b00353d3..7d3dbbba5 100644 --- a/spec/lib/tasks/globalize_spec.rb +++ b/spec/lib/tasks/globalize_spec.rb @@ -72,5 +72,48 @@ describe "Globalize tasks" do expect(notification.send(:"title_#{I18n.locale}")).to eq("Translated") end end + + context "Custom page with a different locale and no translations" do + let(:page) do + create(:site_customization_page, locale: :fr).tap do |page| + page.translations.delete_all + page.update_column(:title, "en Français") + page.reload + end + end + + it "copies the original data to both the page's locale" do + expect(page.title).to eq("en Français") + expect(page.title_fr).to be nil + expect(page.send(:"title_#{I18n.locale}")).to be nil + + run_rake_task + page.reload + + expect(page.title).to eq("en Français") + expect(page.title_fr).to eq("en Français") + expect(page.send(:"title_#{I18n.locale}")).to be nil + end + end + + context "Custom page with a different locale and existing translations" do + let(:page) do + create(:site_customization_page, title: "In English", locale: :fr).tap do |page| + page.update_column(:title, "en Français") + end + end + + it "copies the original data to the page's locale" do + expect(page.title_fr).to be nil + expect(page.title).to eq("In English") + + run_rake_task + page.reload + + expect(page.title).to eq("In English") + expect(page.title_fr).to eq("en Français") + expect(page.send(:"title_#{I18n.locale}")).to eq("In English") + end + end end end