Sha256: 689c91aedf70300d1e974cd15374765e4f6806cb738a8107e98d9901563f3182

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'active_record'

module InheritsMigration  

  def self.included(base)
    base.class_eval do
      alias_method_chain :create_table , :inherits
    end
  end
  
  # Generate the association field.
  def create_table_with_inherits(table_name, options = {}, &block)
    options[:id] ||= false if options[:inherits]
    
    create_table_without_inherits(table_name, options) do |table_defintion|
      if options[:inherits]
        association_type = Object.const_get(options[:inherits].to_s.capitalize)
        association_inst = association_type.send(:new)
        attr_column = association_inst.column_for_attribute(association_type.primary_key)
        
        field_option = {:primary_key => true, :null => false}
        field_option[:limit] = attr_column.limit if attr_column.limit                
        table_defintion.column "#{options[:inherits]}_id", attr_column.type, field_option
      end
      yield table_defintion  
    end 
  end  
end

#ActiveRecord::ConnectionAdapters::SchemaStatements::send(:include, InheritsMigration)
ActiveRecord::Base
ActiveRecord::ConnectionAdapters::SchemaStatements::send(:include, InheritsMigration)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
class-table-inheritance-1.2.1 lib/inherits-migration.rb
class-table-inheritance-1.2.0 lib/inherits-migration.rb