Created
March 28, 2010 12:35
-
-
Save sspross/346740 to your computer and use it in GitHub Desktop.
migration helper to migrate existing data to a target language after adding globalize2 to an existing project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Globalize2MigrationHelper | |
def migrate_existing_data_to_language(model, target_language) | |
say "migrating translated attributes of model '#{model}' to language '#{target_language}'" | |
# set language to use | |
I18n.locale = target_language | |
# iterate over all records | |
for object in model.all | |
# iterate over translated attributes | |
for attribute in model.translated_attribute_names | |
# fill in original value | |
# say "setting '#{model}.#{attribute}' to '#{object[attribute]}'" | |
object.send(:"#{attribute}=", object[attribute]) | |
end | |
# and save the object without validation | |
object.save_with_validation(false) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'globalize2_migration_helper' | |
class AddGlobalize2TranslationTablesToModels < ActiveRecord::Migration | |
extend Globalize2MigrationHelper | |
def self.up | |
Book.create_translation_table! :name => :string | |
migrate_existing_data_to_language(Book, :de) | |
end | |
def self.down | |
Book.drop_translation_table! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment