Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Migrate globalize data #2986

Merged
merged 7 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate custom pages data to their locale
  • Loading branch information
javierm committed Oct 23, 2018
commit a84a0f2b7dd455342a171a3f3aec617e04646494
10 changes: 8 additions & 2 deletions lib/tasks/globalize.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 43 additions & 0 deletions spec/lib/tasks/globalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,48 @@
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