Class: Shaf::Generator::Migration::AddColumn
- Inherits:
-
Base
- Object
- Base
- Shaf::Generator::Migration::AddColumn
show all
- Defined in:
- lib/shaf/generator/migration/add_column.rb
Constant Summary
Constants inherited
from Base
Base::DB_COL_TYPES
Instance Attribute Summary
Attributes inherited from Base
#args
Instance Method Summary
collapse
Methods inherited from Base
#add_change, #call, #column_def, #db_type, identifier, inherited, #initialize, #target, usage
Instance Method Details
#add_columns_change ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/shaf/generator/migration/add_column.rb', line 36
def add_columns_change
cols = columns.map do |s|
"add_column #{column_def(s, create: false)}"
end
[
"alter_table(:#{table_name}) do",
*cols.map { |col| col.prepend(" ") },
"end\n"
]
end
|
#columns ⇒ Object
32
33
34
|
# File 'lib/shaf/generator/migration/add_column.rb', line 32
def columns
args[1..-1]
end
|
#compile_changes ⇒ Object
28
29
30
|
# File 'lib/shaf/generator/migration/add_column.rb', line 28
def compile_changes
add_change add_columns_change
end
|
#compile_migration_name ⇒ Object
19
20
21
22
|
# File 'lib/shaf/generator/migration/add_column.rb', line 19
def compile_migration_name
cols = columns.map { |c| c.split(':').first }
"add_#{cols.join('_')}_to_#{table_name}"
end
|
#table_name ⇒ Object
24
25
26
|
# File 'lib/shaf/generator/migration/add_column.rb', line 24
def table_name
args.first
end
|
#validate_args ⇒ Object
9
10
11
12
13
14
15
16
17
|
# File 'lib/shaf/generator/migration/add_column.rb', line 9
def validate_args
if (table_name || "").empty?
raise "Please provide a table and at least " \
"one column when generation add column migration"
elsif args.size < 2 || (args[1] || "").empty?
raise "Please provide at least one column when " \
"generation add column migration"
end
end
|