Sha256: cda96ead2f0f3863291136314fba319b70b92d5c4070f3263ac191ad2b30b69b

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'rails/generators'
require 'rails/generators/active_record'

module ForeignKeyMigrations
  module Generators
    class MigrationGenerator < ::ActiveRecord::Generators::Base
      argument :name, :default => 'create_foreign_keys'

      def self.source_root
        File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
      end

      def create_migration_file
        set_local_assigns!
        migration_template 'migration.rb', "db/migrate/#{file_name}"
      end

      protected
      attr_reader :foreign_keys

      def set_local_assigns!
        @foreign_keys = determine_foreign_keys
      end

      def determine_foreign_keys
        foreign_keys = []
        connection = ::ActiveRecord::Base.connection
        connection.tables.each do |table_name|
          connection.columns(table_name).each do |column|
            references = ::ActiveRecord::Base.references(table_name, column.name)
            foreign_keys << ::RedhillonrailsCore::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(nil, table_name, column.name, references.first, references.last) if references
          end
        end
        foreign_keys
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aspgems-foreign_key_migrations-2.0.0.beta2 lib/generators/foreign_key_migrations/migration_generator.rb
aspgems-foreign_key_migrations-2.0.0.beta1 lib/generators/foreign_key_migrations/migration_generator.rb