Sha256: 7b2be7ebb1e13719d81920e900b979f76fa88bfac0e45f885698c1fc47c4668f

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

if defined?(Rails)
  require 'rails/generators'
  require 'rails/generators/active_record'

  module Translateable
    class MigrationGenerator < ActiveRecord::Generators::Base
      desc 'Create sample migration with translateable field'
      source_root File.expand_path('../templates', __FILE__)

      argument :field_name, type: :string
      argument :locale, type: :string, default: I18n.default_locale

      # rubocop: disable Metrics/AbcSize
      def create_migration_file
        raise ArgumentError, "given locale #{locale} is not available, check I18n.available_locales" unless I18n.available_locales.include?(locale.to_sym)
        migration_template('migration.rb.erb', "db/migrate/migrate_translateable_#{name}_#{field_name}.rb",
                           migration_version: migration_version,
                           table_name: name,
                           field_name: field_name,
                           field_type: name.classify.constantize.columns_hash[field_name].type,
                           locale: locale)
      end
      # rubocop: enable Metrics/AbcSize

      private

      def rails5?
        Rails.version.start_with?('5')
      end

      def migration_version
        if rails5?
          "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
        else
          ''
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translateable-0.1.7 lib/generators/translateable/migration_generator.rb