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.
This commit is contained in:
Javi Martín
2018-10-05 14:15:24 +02:00
parent 7646b9f166
commit ef7be4fc55
2 changed files with 110 additions and 0 deletions

34
lib/tasks/globalize.rake Normal file
View File

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