Sha256: 1840158cc6ee371b43e2ae4b842c4c1708ced0113549b9cb8c22dd6a3c85187e

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module ForeignKeyMigrations::ActiveRecord
  module Base
    def self.included(base) # :nodoc
      base.extend(ClassMethods)
    end

    module ClassMethods
      # Determines referenced table and column.
      # Used in migrations.
      #   references('comments', 'post_id') # => ['posts', 'id']
      #
      # If <tt>column_name</tt> is parent_id it references to the same table
      #   references('pages', 'parent_id')  # => ['pages', 'id']
      #
      # If referenced table cannot be determined properly it may be overriden
      #   references('widgets', 'main_page_id', :references => 'pages'))
      #   # => ['pages', 'id']
      #
      # Also whole result may be given by hand
      #   references('addresses', 'member_id', :references => ['users', 'uuid'])
      #   # => ['users', 'uuid']
      def references(table_name, column_name, options = {})
        column_name = column_name.to_s
        if options.has_key?(:references)
          references = options[:references]
          references = [references, :id] unless references.nil? || references.is_a?(Array)
          references
        elsif column_name == 'parent_id'
          [table_name, :id]
        elsif column_name =~ /^(.*)_id$/
          [pluralized_table_name($1), :id]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aspgems-foreign_key_migrations-2.0.0.beta2 lib/foreign_key_migrations/active_record/base.rb
aspgems-foreign_key_migrations-2.0.0.beta1 lib/foreign_key_migrations/active_record/base.rb