Sha256: c9d3f5aa9a08772dffce1f1d5c356b430027c9ce5b2601aadaa2ff1421a91035

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

require 'rails/generators'
require 'rails/generators/migration'
require 'migration_assist/helper/file_name'

module RailsAssist::Migration
  include Rails::Generators::Migration
  include FileNameHelper
    
  def reverse_migration_name name
    name.gsub(/^add_/, 'remove_').gsub(/^create_/, 'drop_')
  end

  def reverse_migration! migration_path
    reverse_class_names! migration_path        
    reverse_up_down_methods! migration_path
  end

  def reverse_class_names! migration_path
    # Change class name
    gsub_file migration_path, /class Add/, 'class Remove'
    gsub_file migration_path, /class Create/, 'class Drop'
  end

  def reverse_up_down_methods! migration_path
    # swap up and down methods
    gsub_file migration_path, /up/, 'dwn'
    gsub_file migration_path, /down/, 'up'
    gsub_file migration_path, /dwn/, 'down'        
  end

  def latest_migration_file dir, name
    self.class.latest_migration_file dir, name
  end
  
  def migration name, template_name=nil
    migration_template("#{template_name || name}.erb", migration_file_name(name))
  end  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
migration_assist-0.4.0 lib/migration_assist/implementation.rb
migration_assist-0.3.1 lib/migration_assist/implementation.rb
migration_assist-0.3.0 lib/migration_assist/implementation.rb
migration_assist-0.2.0 lib/migration_assist/implementation.rb