Module: SqliteCrypto::MigrationHelpers::References

Defined in:
lib/sqlite_crypto/migration_helpers.rb

Instance Method Summary collapse

Instance Method Details

#references(*args, **options) ⇒ Object Also known as: belongs_to



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sqlite_crypto/migration_helpers.rb', line 19

def references(*args, **options)
  ref_name = args.first
  explicit_to_table = options.delete(:to_table)
  ref_table = explicit_to_table || ref_name.to_s.pluralize

  if (primary_key_type = detect_primary_key_type(ref_table))
    options[:type] ||= :string
    options[:limit] ||= IdTypes.string_limit_for(primary_key_type)
  end

  if explicit_to_table && options[:foreign_key]
    fk_options = options[:foreign_key].is_a?(Hash) ? options[:foreign_key] : {}
    options[:foreign_key] = fk_options.reverse_merge(to_table: explicit_to_table)
  end

  super
end