Module: SchemaPlus::ActiveRecord::Migration::CommandRecorder

Includes:
ColumnOptionsHandler
Defined in:
lib/schema_plus/active_record/migration/command_recorder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColumnOptionsHandler

#schema_plus_handle_column_options

Instance Attribute Details

#schema_plus_configObject

:nodoc:



7
8
9
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 7

def schema_plus_config
  @schema_plus_config
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 9

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method_chain :add_column, :schema_plus
    alias_method_chain :add_reference, :schema_plus unless ::ActiveRecord::VERSION::MAJOR.to_i < 4
    alias_method_chain :invert_add_index, :schema_plus
  end
end

Instance Method Details

#add_column_with_schema_plus(table_name, name, type, options = {}) ⇒ Object

:nodoc:



17
18
19
20
21
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 17

def add_column_with_schema_plus(table_name, name, type, options = {}) #:nodoc:
  add_column_without_schema_plus(table_name, name, type, options)
  revertable_schema_plus_handle_column_options(table_name, name, options, :config => schema_plus_config)
  self
end

#add_foreign_key(*args) ⇒ Object



64
65
66
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 64

def add_foreign_key(*args)
  record(:add_foreign_key, args)
end

#add_reference_with_schema_plus(table_name, ref_name, options = {}) ⇒ Object

seems like this is fixing a rails bug:

change_table foo, :bulk => true { |t| t.references :bar }

results in an ‘unknown method :add_reference_sql’ (with mysql2)

should track it down separately and submit a patch/fix to rails



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 29

def add_reference_with_schema_plus(table_name, ref_name, options = {}) #:nodoc:
  options[:references] = nil if options[:polymorphic]
  # which is the worse hack...?
  if RUBY_VERSION >= "2.0.0" and self.delegate.respond_to? :add_reference_sql
    # .. rebinding a method from a different module?  (can't do this in ruby 1.9.3)
    ::ActiveRecord::ConnectionAdapters::SchemaStatements.instance_method(:add_reference).bind(self).call(table_name, ref_name, options)
  else
    # .. or copying and pasting the code?
    polymorphic = options.delete(:polymorphic)
    index_options = options.delete(:index)
    add_column(table_name, "#{ref_name}_id", :integer, options)
    add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
    add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : nil) if index_options
  end

  self
end

#invert_add_foreign_key(args) ⇒ Object



68
69
70
71
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 68

def invert_add_foreign_key(args)
  table_name, column_names, references_table_name, references_column_names, options = args
  [:remove_foreign_key, [table_name, column_names, references_table_name, references_column_names, (options||{}).merge(if_exists: true)]]
end

#invert_add_index_with_schema_plus(args) ⇒ Object



73
74
75
76
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 73

def invert_add_index_with_schema_plus(args)
  table, columns, options = *args
  [:remove_index, [table, (options||{}).merge(column: columns, if_exists: true)]]
end

#invert_remove_foreign_key(args) ⇒ Object



82
83
84
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 82

def invert_remove_foreign_key(args)
  [:add_foreign_key, args]
end

#remove_foreign_key(*args) ⇒ Object



78
79
80
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 78

def remove_foreign_key(*args)
  record(:remove_foreign_key, args)
end