Class: SlashMigrate::ColumnsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/slash_migrate/columns_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/slash_migrate/columns_controller.rb', line 19

def create
  migration = AddColumnsMigration.from_params(table: @table, rows: params[:attributes])

  unless migration.any?
    redirect_to(new_table_column_path(@table), alert: "Add at least one column.")
    return
  end

  written = migration.write!
  redirect_to migrations_path,
    notice: "Created #{written.join(", ")}. Run it below to apply."
end

#dropObject



40
41
42
43
44
45
46
47
# File 'app/controllers/slash_migrate/columns_controller.rb', line 40

def drop
  column = find_column
  head :not_found and return unless column

  written = DropColumnMigration.new(table: @table, column: column).write!
  redirect_to migrations_path,
    notice: "Created #{written.join(", ")}. Run it below to apply."
end

#editObject



32
33
34
35
36
37
38
# File 'app/controllers/slash_migrate/columns_controller.rb', line 32

def edit
  @column = find_column
  head :not_found and return unless @column

  @drop_migration = DropColumnMigration.new(table: @table, column: @column)
  @drop_caveat = drop_caveat
end

#newObject



5
6
7
8
# File 'app/controllers/slash_migrate/columns_controller.rb', line 5

def new
  @migration = AddColumnsMigration.new(table: @table)
  @existing_tables = inspector.table_names
end

#previewObject



10
11
12
13
14
15
16
17
# File 'app/controllers/slash_migrate/columns_controller.rb', line 10

def preview
  @migration = AddColumnsMigration.from_params(table: @table, rows: params[:attributes])
  @hint = "Add a column to see the migration it will generate." unless @migration.any?
  render_stream :preview
rescue => e
  @error = e.message
  render_stream :preview
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/slash_migrate/columns_controller.rb', line 59

def update
  original = find_column
  head :not_found and return unless original

  migration = EditColumnMigration.new(table: @table, original: original, desired: desired_column)

  unless migration.changed?
    redirect_to(edit_table_column_path(@table, params[:name]), alert: "No changes to apply.")
    return
  end

  written = migration.write!
  redirect_to migrations_path,
    notice: "Created #{written.join(", ")}. Run it below to apply."
end

#update_previewObject



49
50
51
52
53
54
55
56
57
# File 'app/controllers/slash_migrate/columns_controller.rb', line 49

def update_preview
  original = find_column
  @migration = original && EditColumnMigration.new(table: @table, original: original, desired: desired_column)
  @hint = "Change a value to see the migration it will generate." unless @migration&.changed?
  render_stream :update_preview
rescue => e
  @error = e.message
  render_stream :update_preview
end