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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add task to migrate data to translation tables
We forgot to do it when we created the translation tables, and so now we
need to make sure we don't overwrite existing translations.
  • Loading branch information
javierm committed Oct 23, 2018
commit ef7be4fc5521ae439b076c446a9686c3ec4a93fc
34 changes: 34 additions & 0 deletions lib/tasks/globalize.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace :globalize do
desc "Migrates existing data to translation tables"
task migrate_data: :environment do
[
AdminNotification,
Banner,
Budget::Investment::Milestone,
I18nContent,
Legislation::DraftVersion,
Legislation::Process,
Legislation::Question,
Legislation::QuestionOption,
Poll,
Poll::Question,
Poll::Question::Answer,
SiteCustomization::Page,
Widget::Card
].each do |model_class|
Logger.new(STDOUT).info "Migrating #{model_class} data"

fields = model_class.translated_attribute_names

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])
end
end

record.save!
end
end
end
end
76 changes: 76 additions & 0 deletions spec/lib/tasks/globalize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require "rails_helper"
require "rake"

describe "Globalize tasks" do

describe "#migrate_data" do

before do
Rake.application.rake_require "tasks/globalize"
Rake::Task.define_task(:environment)
end

let :run_rake_task do
Rake::Task["globalize:migrate_data"].reenable
Rake.application.invoke_task "globalize:migrate_data"
end

context "Original data with no translated data" do
let(:poll) do
create(:poll).tap do |poll|
poll.translations.delete_all
poll.update_column(:name, "Original")
poll.reload
end
end

it "copies the original data" do
expect(poll.send(:"name_#{I18n.locale}")).to be nil
expect(poll.name).to eq("Original")

run_rake_task
poll.reload

expect(poll.name).to eq("Original")
expect(poll.send(:"name_#{I18n.locale}")).to eq("Original")
end
end

context "Original data with blank translated data" do
let(:banner) do
create(:banner).tap do |banner|
banner.update_column(:title, "Original")
banner.translations.first.update_column(:title, "")
end
end

it "copies the original data" do
expect(banner.title).to eq("")

run_rake_task
banner.reload

expect(banner.title).to eq("Original")
expect(banner.send(:"title_#{I18n.locale}")).to eq("Original")
end
end

context "Original data with translated data" do
let(:notification) do
create(:admin_notification, title: "Translated").tap do |notification|
notification.update_column(:title, "Original")
end
end

it "maintains the translated data" do
expect(notification.title).to eq("Translated")

run_rake_task
notification.reload

expect(notification.title).to eq("Translated")
expect(notification.send(:"title_#{I18n.locale}")).to eq("Translated")
end
end
end
end