Sha256: a0634fe21af5fa8d00c4a5863d5bc54004cd883e7df8cd1138b2516b004e94d7

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

class RemoveTranslationsFromSpreeTables < SolidusSupport::Migration[4.2]
  def up
    # Don't migrate if we still use Globalize, i.e. through spree_globalize Gem
    return if defined?(Globalize)

    %w(
      OptionType
      OptionValue
      ProductProperty
      Product
      Promotion
      Property
      Store
      Taxon
      Taxonomy
    ).each do |class_name|
      migrate_translation_data!(class_name)
    end
  end

  def down
    return if defined?(Globalize)
    raise ActiveRecord::IrreversibleMigration
  end

  private

  def current_locale
    I18n.default_locale || 'en'
  end

  def migrate_translation_data!(class_name)
    klass = "Spree::#{class_name}".constantize
    table_name = klass.table_name
    singular_table_name = table_name.singularize

    return if !table_exists?(table_name) || !table_exists?("#{singular_table_name}_translations")

    # We can't rely on Globalize drop_translation_table! here,
    # because the Gem has been already removed, so we need to run custom SQL
    records = exec_query("SELECT * FROM #{singular_table_name}_translations WHERE locale = '#{current_locale}';")

    records.each do |record|
      id = record["#{singular_table_name}_id"]
      attributes = record.except(
        'id',
        "#{singular_table_name}_id",
        'locale',
        'deleted_at',
        'created_at',
        'updated_at'
      )
      object = if klass.respond_to?(:with_deleted)
                 klass.with_deleted.find(id)
               else
                 klass.find(id)
               end
      object.update_columns(attributes)
    end

    say "Migrated #{current_locale} translation for #{class_name} back into original table."

    drop_table "#{singular_table_name}_translations"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_i18n-1.3.0 db/migrate/20150609154031_remove_translations_from_spree_tables.rb
solidus_i18n-1.2.0 db/migrate/20150609154031_remove_translations_from_spree_tables.rb