Class: ActiveRecord::Generators::MigrationGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/modular_migration/active_record/generators/migration_generator.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create_migration_fileObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/modular_migration/active_record/generators/migration_generator.rb', line 5

def create_migration_file
  set_local_assigns!
  validate_file_name! if self.respond_to?(:validate_file_name)
  if migration_action && migration_action.match(/(add|remove)/)
    table = table_name.singularize
    modular = table.gsub(/_/, '/').camelize.safe_constantize
    table = table.split('_')
    (table.size-1).times do |i|
      result = []
      table.each_with_index do |m, index|
        result << m
        result << (index == i ? '_' : '/') if index < table.size-1
        modular = result.join.camelize.safe_constantize if index == table.size-1
      end
      break if modular
    end unless modular
    migration = modular ? modular.to_s.underscore : []
  end
  migration = join_tables.map(&:singularize).join('_') if migration_action && migration_action.match(/join/)
  FileUtils.mkdir_p(File.join("db", "migrate", migration)) if migration
  migration_file = File.join("db", "migrate", migration||"", "#{file_name}.rb")
  template_file = Rails.version.to_i < 4 ? 'migration.rb' : @migration_template
  migration_template template_file, migration_file
end