Class: UniqIdentifier::Generators::AddGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/uniq_identifier/add_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object



12
13
14
# File 'lib/generators/uniq_identifier/add_generator.rb', line 12

def self.next_migration_number(path)
  ActiveRecord::Generators::Base.next_migration_number(path)
end

Instance Method Details

#add_migration_fileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/uniq_identifier/add_generator.rb', line 22

def add_migration_file
  migration_template 'migration.rb', "db/migrate/add_uuid_#{file_path}.rb" if options.orm == :active_record

  hook   = 'uniq_identifier'
  data   = "\n"
  data += indent("#{hook}")

  if options.orm == 'mongoid'
    data += "\n"
    data += indent("field :uuid, type: String")
  end

  if options.orm == :active_record
    header = "class #{class_name} < ActiveRecord::Base"
  else
    header = 'include Mongoid::Document'
  end

  model_path = File.join('app', 'models', "#{file_path}.rb")
  inject_into_file model_path, data, after: header, verbose: true

  readme("./README-#{options.orm}")
end