Sha256: b83fb9849fc3e8941feddc25c80530f1d5ae5a2d841c55820b24d7c99eeac8fd

Contents?: true

Size: 1.52 KB

Versions: 14

Compression:

Stored size: 1.52 KB

Contents

module Globalize
  module ActiveRecord
    module Migration
      def create_translation_table!(fields)
        translated_attribute_names.each do |f|
          raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
        end

        fields.each do |name, type|
          if translated_attribute_names.include?(name) && ![:string, :text].include?(type)
            raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
          end
        end

        self.connection.create_table(translation_table_name) do |t|
          t.references table_name.sub(/^#{table_name_prefix}/, "").singularize
          t.string :locale
          fields.each do |name, type|
            t.column name, type
          end
          t.timestamps
        end

        self.connection.add_index(
          translation_table_name, 
          "#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id",
          :name => translation_index_name
        )
      end

      def translation_index_name
        require 'digest/sha1'
        # FIXME what's the max size of an index name?
        index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id"
        index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
      end

      def drop_translation_table!
        self.connection.remove_index(translation_table_name, :name => translation_index_name) rescue nil
        self.connection.drop_table(translation_table_name)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
radiant-globalize2-extension-0.3.2 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.3.1 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.3.0 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.8 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.7 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.6 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.5 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.4 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.3 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.2 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.1 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
radiant-globalize2-extension-0.2.0 vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
globalize3-0.0.1 lib/globalize/active_record/migration.rb
globalize2-0.2.1 lib/globalize/active_record/migration.rb