Class: Eucalypt::Generators::Add::Column
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Eucalypt::Generators::Add::Column
show all
- Includes:
- Helpers, Migration::Helpers, Thor::Actions
- Defined in:
- lib/eucalypt/migration/namespaces/migration-add/generators/column.rb
Class Method Summary
collapse
Instance Method Summary
collapse
#sanitize_column_options, #sanitize_index_options, #sanitize_table_options
Class Method Details
.source_root ⇒ Object
16
17
18
|
# File 'lib/eucalypt/migration/namespaces/migration-add/generators/column.rb', line 16
def self.source_root
File.join File.dirname(File.dirname(File.dirname __dir__))
end
|
Instance Method Details
#generate(table:, column:, type:, options: {}) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/eucalypt/migration/namespaces/migration-add/generators/column.rb', line 20
def generate(table:, column:, type:, options: {})
table = Inflect.resource_keep_inflection(table.to_s)
column = Inflect.resource_keep_inflection(column.to_s)
type = Inflect.resource_keep_inflection(type.to_s)
sleep 1
migration_name = "add_#{column}_to_#{table}"
migration = Eucalypt::Helpers::Migration[title: migration_name, template: 'migration_base.tt']
return unless migration.create_anyway? if migration.exists?
config = {migration_class_name: migration_name.camelize}
template migration.template, migration.file_path, config
sanitized_options = sanitize_column_options(options)
insert_into_file migration.file_path, :after => "def change\n" do
String.build do |s|
s << " add_column :#{table}, :#{column}, :#{type}"
s << ', ' unless sanitized_options.empty?
s << sanitized_options.map{|opt| "#{opt.first}: #{opt.last}"}*', '
s << "\n"
end
end
end
|