Sha256: ac165730495d954de42d04ec7c926271757872900d5e8ba086e714fb087b3022

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

require 'generators/data_migrate'
require 'rails/generators'
require 'rails/generators/migration'

module DataMigrate
  module Generators
    class DataMigrationGenerator < Rails::Generators::NamedBase
      namespace "data_migration"
      include Rails::Generators::Migration

      argument :attributes, type: :array, default: [], banner: 'field:type field:type'

      def create_data_migration
        set_local_assigns!
        migration_template 'data_migration.rb', "db/data/#{file_name}.rb"
      end

      protected

      attr_reader :migration_action

      def self.next_migration_number(dirname)
        if ActiveRecord::Base.timestamped_migrations
          Time.new.utc.strftime("%Y%m%d%H%M%S")
        else
          "%.3d" % (current_migration_number(dirname) + 1)
        end
      end

      def set_local_assigns!
        if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
          @migration_action = $1
          @table_name       = $2.pluralize
        end
      end

      def migration_base_class_name
        if ActiveRecord.version >= Gem::Version.new('5.0')
          "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
        else
          'ActiveRecord::Migration'
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
data_migrate-3.4.0 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.3.1 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.3.0 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.2.2 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.2.1 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.2.0 lib/generators/data_migration/data_migration_generator.rb
data_migrate-3.1.0 lib/generators/data_migration/data_migration_generator.rb
salsify-data_migrate-3.1.0 lib/generators/data_migration/data_migration_generator.rb
salsify-data_migrate-3.0.1 lib/generators/data_migration/data_migration_generator.rb