Class: Adjective::AddColumnsMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/templates/add_columns_migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, modules = []) ⇒ AddColumnsMigration

Returns a new instance of AddColumnsMigration.



5
6
7
8
9
10
11
# File 'lib/templates/add_columns_migration.rb', line 5

def initialize(class_name, modules = [])
  # raise ArgumentError, "Provided class_name of: #{class_name} was not a valid String." if (!!class_name || !class_name.is_a?(String)) 
  @class_name = class_name
  @modules = modules.map{|m| m&.downcase}
  @version = "7.0"
  @module_classes = given_modules
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



3
4
5
# File 'lib/templates/add_columns_migration.rb', line 3

def class_name
  @class_name
end

#module_classesObject (readonly)

Returns the value of attribute module_classes.



3
4
5
# File 'lib/templates/add_columns_migration.rb', line 3

def module_classes
  @module_classes
end

#modulesObject (readonly)

Returns the value of attribute modules.



3
4
5
# File 'lib/templates/add_columns_migration.rb', line 3

def modules
  @modules
end

Instance Method Details

#attribute_down_fieldsObject



36
37
38
# File 'lib/templates/add_columns_migration.rb', line 36

def attribute_down_fields
  attribute_up_fields.gsub("add_column", "remove_column")
end

#attribute_up_fieldsObject



30
31
32
33
34
# File 'lib/templates/add_columns_migration.rb', line 30

def attribute_up_fields
  @module_classes.map do |mod|
    mod.adjective_add_columns(@class_name)
  end.join("\n").chomp
end

#base_templateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/templates/add_columns_migration.rb', line 40

def base_template
  <<~TEMPLATE
    class {{migration_class}} < ActiveRecord::Migration[{{version}}]
      def up
        {{add_methods}}
      end

      def down
        {{remove_methods}}
      end
    end
  TEMPLATE
end

#file_nameObject



18
19
20
21
22
# File 'lib/templates/add_columns_migration.rb', line 18

def file_name
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  migration_name = migration_class.underscore
  return "#{timestamp}_#{migration_name}.rb"
end

#given_modulesObject



24
25
26
27
28
# File 'lib/templates/add_columns_migration.rb', line 24

def given_modules
  @modules.map do |mod|
    klass = "Adjective::#{mod.capitalize}".constantize
  end
end

#migration_classObject



13
14
15
16
# File 'lib/templates/add_columns_migration.rb', line 13

def migration_class
  modules_string = @modules.map {|m| m.capitalize}.join("And")
  "Add#{modules_string}To#{@class_name.camelize}"
end

#renderObject



54
55
56
57
58
59
60
61
# File 'lib/templates/add_columns_migration.rb', line 54

def render
  base = base_template
  base
    .gsub("{{migration_class}}", migration_class.lstrip)
    .gsub("{{add_methods}}", attribute_up_fields.lstrip)
    .gsub("{{remove_methods}}", attribute_down_fields.lstrip)
    .gsub("{{version}}", @version)
end