Module: ActiveGraph::Generators::MigrationHelper

Extended by:
ActiveSupport::Concern
Included in:
MigrationGenerator, ModelGenerator, UpgradeV8Generator
Defined in:
lib/rails/generators/active_graph_generator.rb

Instance Method Summary collapse

Instance Method Details

#base_migration_file_name(file_name, prefix = '') ⇒ Object



14
15
16
# File 'lib/rails/generators/active_graph_generator.rb', line 14

def base_migration_file_name(file_name, prefix = '')
  "#{prefix}#{file_name.parameterize}"
end

#existing_migration(dirname, file_name) ⇒ Object



27
28
29
# File 'lib/rails/generators/active_graph_generator.rb', line 27

def existing_migration(dirname, file_name)
  migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
end

#migration_file_name(file_name, prefix = '') ⇒ Object



18
19
20
# File 'lib/rails/generators/active_graph_generator.rb', line 18

def migration_file_name(file_name, prefix = '')
  "#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{base_migration_file_name(file_name, prefix)}.rb"
end

#migration_lookup_at(dirname) ⇒ Object



22
23
24
# File 'lib/rails/generators/active_graph_generator.rb', line 22

def migration_lookup_at(dirname)
  Dir.glob("#{dirname}/[0-9]*_*.rb")
end

#migration_template(template_name, prefix = '') ⇒ Object

:revoke happens when task is invoked with ‘rails destroy model ModelName`



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails/generators/active_graph_generator.rb', line 32

def migration_template(template_name, prefix = '')
  real_file_name = case @behavior
                   when :revoke
                     existing_migration(
                       'db/neo4j/migrate',
                       base_migration_file_name(file_name, prefix)
                     )
                   else
                     migration_file_name(file_name, prefix)
                   end

  # If running with :revoke and migration doesn't exist, real_file_name = nil
  return if !real_file_name

  @migration_class_name = file_name.camelize

  # template() method is still run on revoke but it doesn't generate anything
  # other than a consol message indicating the filepath.
  # (this appears to be behavior provided by rails)
  template template_name, File.join('db/neo4j/migrate', real_file_name)

  # On revoke, we need to manually remove the file
  FileUtils.rm(real_file_name) if @behavior == :revoke
end