Migrate custom pages data to their locale

This commit is contained in:
Javi Martín
2018-10-05 14:19:44 +02:00
committed by decabeza
parent c774ae67a0
commit 232d5b0edd
2 changed files with 51 additions and 2 deletions

View File

@@ -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

View File

@@ -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