Class: Shaf::Generator::Migration::AddIndex

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/migration/add_index.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#add_change, #call, #column_def, identified_by, identifier, inherited, #initialize, #table_name, #target, usage

Constructor Details

This class inherits a constructor from Shaf::Generator::Migration::Base

Instance Method Details

#add_index_changeObject



27
28
29
30
31
32
33
34
# File 'lib/shaf/generator/migration/add_index.rb', line 27

def add_index_change
  col_def =  column_def("#{column}:index", create: false)
  [
    "alter_table(:#{table_name}) do",
    col_def.prepend("  "), # indent body with 2 spaces
    "end\n"
  ]
end

#columnObject



23
24
25
# File 'lib/shaf/generator/migration/add_index.rb', line 23

def column
  args[1]
end

#compile_changesObject



19
20
21
# File 'lib/shaf/generator/migration/add_index.rb', line 19

def compile_changes
  add_change add_index_change
end

#compile_migration_nameObject



15
16
17
# File 'lib/shaf/generator/migration/add_index.rb', line 15

def compile_migration_name
  "add_#{column}_index_to_#{table_name}"
end

#validate_argsObject



9
10
11
12
13
# File 'lib/shaf/generator/migration/add_index.rb', line 9

def validate_args
  if (table_name || "").empty? || (column || "").empty?
    raise "Please provide a table and the column to create index on"
  end
end