Class: Effective::Generators::MigrationGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/effective/migration_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_migrationObject

rails generate effective:migration courses body:text –database example



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/effective/migration_generator.rb', line 30

def create_migration
  if invoked_attributes.present?
    args = ["create_#{plural_name}"] + (invokable(invoked_attributes) | timestamps)
    args += ["--database", options['database']] if options['database']
    Rails::Generators.invoke('migration', args)
    return
  end

  return if with_resource_tenant do
    table_name = resource.klass.table_name

    if ActiveRecord::Base.connection.table_exists?(table_name)
      say_status(:error, "#{table_name} table already exist. We can't migrate (yet). Exiting.", :red)
      true
    end
  end

  if resource.model_attributes.blank?
    say_status(:error, "No model attributes present. Please add the effective_resource do ... end block and try again", :red)
    return
  end

  args = ["create_#{plural_name}"] + invokable(resource.model_attributes) - timestamps
  args += ["--database", options['database']] if options['database']

  if options['database'].blank? && defined?(Tenant)
    args += ["--database", resource.klass.name.split('::').first.downcase]
  end

  Rails::Generators.invoke('migration', args)
end

#invoke_migrationObject



25
26
27
# File 'lib/generators/effective/migration_generator.rb', line 25

def invoke_migration
  say_status :invoke, :migration, :white
end

#validate_resourceObject



21
22
23
# File 'lib/generators/effective/migration_generator.rb', line 21

def validate_resource
  exit unless resource_valid?
end