Module: Fakey::Table

Defined in:
lib/fakey/schema_definitions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



50
51
52
53
54
55
# File 'lib/fakey/schema_definitions.rb', line 50

def self.included(base)
  base.class_eval do
    alias_method_chain :references, :foreign_keys
    alias_method_chain :belongs_to, :foreign_keys
  end
end

Instance Method Details

#references_with_foreign_keys(*args) ⇒ Object Also known as: belongs_to_with_foreign_keys



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fakey/schema_definitions.rb', line 57

def references_with_foreign_keys(*args)
  options = args.extract_options!
  polymorphic = options.delete(:polymorphic)
  args.each do |col|
    column_name =  options[:column] || "#{col}_id"
    column_database_type = options[:type] || :integer
    if polymorphic
      @base.add_column(@table_name, column_name, column_database_type, options)
      @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options)
    else
      options[:references] ||= col.to_s.pluralize
      @base.add_column_with_foreign_key(@table_name, column_name, column_database_type, options)
    end
  end
end