Module: SchemaPlus::ForeignKeys::Middleware::Migration::Column

Defined in:
lib/schema_plus/foreign_keys/middleware/migration.rb

Instance Method Summary collapse

Instance Method Details

#around(env) {|env| ... } ⇒ Object

Add the foreign keys

Yields:

  • (env)

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/schema_plus/foreign_keys/middleware/migration.rb', line 70

def around(env)
  original_options = env.options
  env.options = original_options.dup

  is_reference = (env.type == :reference)
  is_polymorphic = is_reference && env.options[:polymorphic]

  # usurp foreign key creation from AR, since it doesn't support
  # all our features
  env.options[:foreign_key] = false 

  yield env

  return if is_polymorphic or env.implements_reference

  env.options = original_options

  add_foreign_keys(env)

end

#before(env) ⇒ Object

Column option shortcuts


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/schema_plus/foreign_keys/middleware/migration.rb', line 38

def before(env)
  opts = env.options[:foreign_key]

  return if opts == false

  opts = {} if opts == true

  [:references, :on_update, :on_delete, :deferrable].each do |key|
    (opts||={}).reverse_merge!(key => env.options[key]) if env.options.has_key? key
  end

  return if opts.nil?

  if opts.has_key?(:references) && !opts[:references]
    env.options[:foreign_key] = false
    return
  end

  case opts[:references]
  when nil
  when Array
    table, primary_key = opts[:references]
    opts[:references] = table
    opts[:primary_key] ||= primary_key
  end

  env.options[:foreign_key] = opts
end