Class: Spree::TranslationMigrations

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/translation_migrations.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_class, default_locale) ⇒ TranslationMigrations

Returns a new instance of TranslationMigrations.



3
4
5
6
7
8
9
# File 'lib/spree/translation_migrations.rb', line 3

def initialize(resource_class, default_locale)
  @resource_class = resource_class
  @translations_table = resource_class::Translation.table_name
  @translatable_fields = resource_class.translatable_fields.join(', ')
  @foreign_key = "#{resource_class.table_name.singularize}_id"
  @default_locale = default_locale
end

Instance Method Details

#revert_translation_data_transferObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spree/translation_migrations.rb', line 26

def revert_translation_data_transfer
  translation_table_fields = @resource_class.translatable_fields.map { |f| "#{@translations_table}.#{f}" }.join(', ')
  row_expression = @resource_class.translatable_fields.count == 1 ? 'ROW' : ''

  ActiveRecord::Base.connection.execute("
      UPDATE #{@resource_class.table_name}
      SET (#{@translatable_fields}) = #{row_expression}(#{translation_table_fields})
      FROM #{@translations_table}
      WHERE #{@translations_table}.#{@foreign_key} = #{@resource_class.table_name}.id
                                        ")

  ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{@translations_table}")
end

#transfer_translation_dataObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/spree/translation_migrations.rb', line 11

def transfer_translation_data
  nullify_translatable_fields = @resource_class.translatable_fields.map { |f| "#{f}=null" }.join(', ')

  unless @resource_class::Translation.exists?
    ActiveRecord::Base.connection.execute("
      INSERT INTO #{@translations_table} (#{@translatable_fields}, #{@foreign_key}, locale, created_at, updated_at)
      SELECT #{@translatable_fields}, id, '#{@default_locale}' as locale, created_at, updated_at FROM #{@resource_class.table_name};
                                          ")
    ActiveRecord::Base.connection.execute("
      UPDATE #{@resource_class.table_name}
      SET #{nullify_translatable_fields};
                                          ")
  end
end