Class: Undestroy::Binding::ActiveRecord::MigrationStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/undestroy/binding/active_record/migration_statement.rb

Constant Summary collapse

SCHEMA =
[
  :create_table, :drop_table, :rename_table,
  :add_column, :rename_column, :change_column, :remove_column,
]
INDEX =
[
  :add_index, :remove_index
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, *args, &block) ⇒ MigrationStatement

Returns a new instance of MigrationStatement.



31
32
33
34
35
36
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 31

def initialize(method_name, *args, &block)
  self.class.ensure_models_loaded
  self.method_name = method_name
  self.arguments = args
  self.block = block
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



13
14
15
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 13

def arguments
  @arguments
end

#blockObject

Returns the value of attribute block.



13
14
15
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 13

def block
  @block
end

#method_nameObject

Returns the value of attribute method_name.



13
14
15
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 13

def method_name
  @method_name
end

Class Method Details

.add(klass = ActiveRecord::Migration) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 15

def self.add(klass=ActiveRecord::Migration)
  klass.class_eval do

    def method_missing_with_undestroy(method, *args, &block)
      original = method(:method_missing_without_undestroy)
      original.call method, *args, &block
      stmt = Undestroy::Binding::ActiveRecord::MigrationStatement.new(method, *args, &block)
      stmt.run!(original) if stmt.run?
    end

    alias :method_missing_without_undestroy :method_missing
    alias :method_missing :method_missing_with_undestroy

  end
end

.ensure_models_loadedObject



90
91
92
93
94
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 90

def self.ensure_models_loaded
  Undestroy::Config.config.model_paths.each do |path|
    Undestroy::Binding::ActiveRecord.load_models(path)
  end
end

Instance Method Details

#configObject



46
47
48
49
50
51
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 46

def config
  Undestroy::Config.catalog.select do |c|
    c.source_class.respond_to?(:table_name) &&
    c.source_class.table_name.to_s == source_table_name.to_s
  end.first
end

#index_action?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 64

def index_action?
  INDEX.include?(method_name)
end

#run!(callable) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 80

def run!(callable)
  callable.call(method_name, *target_arguments, &block)

  if create_table?
    config.fields.values.sort.each do |field|
      callable.call(:add_column, target_table_name, field.name, field.type)
    end
  end
end

#run?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 68

def run?
  (
    arguments.present? &&
    config && config.migrate &&
    !rename_table_exception? &&
    (
      schema_action? ||
      index_action? && config.indexes
    )
  )
end

#schema_action?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 60

def schema_action?
  SCHEMA.include?(method_name)
end

#source_table_nameObject



38
39
40
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 38

def source_table_name
  self.arguments[0]
end

#target_argumentsObject



53
54
55
56
57
58
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 53

def target_arguments
  self.arguments.dup.tap do |args|
    args[0] = target_table_name
    args[1] = binding.prefix_table_name(args[1]) if rename_table?
  end
end

#target_table_nameObject



42
43
44
# File 'lib/undestroy/binding/active_record/migration_statement.rb', line 42

def target_table_name
  config.target_class.table_name if config
end