Sha256: 34fea2cb6c8f5e898e34e13bc601b4ce3a2ff913d9f14943544559907b2b1730

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'active_support'
require 'active_support/core_ext/module/attr_accessor_with_default'

module AutomaticForeignKey::ActiveRecord::ConnectionAdapters
  module TableDefinition
    def self.included(base)
      base.class_eval do
        alias_method_chain :column, :automatic_foreign_key
        alias_method_chain :primary_key, :automatic_foreign_key
      end
    end
        
    def primary_key_with_automatic_foreign_key(name, options = {})
      column(name, :primary_key, options)
    end

    def indices
      @indices ||= []
    end

    def column_with_automatic_foreign_key(name, type, options = {})
      column_without_automatic_foreign_key(name, type, options)
      references = ActiveRecord::Base.references(self.name, name, options)
      if references
        AutomaticForeignKey.set_default_update_and_delete_actions!(options)
        foreign_key(name, references.first, references.last, options) 
        if index = options.fetch(:index, AutomaticForeignKey.auto_index)
          # append [column_name, index_options] pair
          self.indices << [name, AutomaticForeignKey.options_for_index(index)]
        end
      elsif options[:index]
        self.indices << [name, AutomaticForeignKey.options_for_index(options[:index])]
      end
      self
    end

    # Some people liked this; personally I've decided against using it but I'll keep it nonetheless
    def belongs_to(table, options = {})
      options = options.merge(:references => table)
      options[:on_delete] = options.delete(:dependent) if options.has_key?(:dependent)
      column("#{table.to_s.singularize}_id".to_sym, :integer, options)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
automatic_foreign_key-1.1.8 lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb
automatic_foreign_key-1.1.7 lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb
automatic_foreign_key-1.1.6 lib/automatic_foreign_key/active_record/connection_adapters/table_definition.rb