Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7932: method_missing_for_table_definitions_by_extend.diff

File method_missing_for_table_definitions_by_extend.diff, 1.9 kB (added by norbert, 1 year ago)
  • activerecord/test/migration_test.rb

    old new  
    9393      Person.connection.drop_table :testings rescue nil 
    9494    end 
    9595 
     96    def test_create_table_with_method_missing 
     97      Person.connection.create_table :testings do |t| 
     98        t.one :string 
     99        t.two :integer, :default => 2 
     100      end 
     101 
     102      columns = Person.connection.columns(:testings) 
     103 
     104      assert_equal %w(id one two), columns.map { |c| c.name }.sort 
     105      assert_equal 2, columns.find { |c| c.name == "two" }.default 
     106    ensure 
     107      Person.connection.drop_table :testings rescue nil 
     108    end 
     109 
    96110    def test_create_table_with_not_null_column 
    97111      assert_nothing_raised do 
    98112        Person.connection.create_table :testings do |t| 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    9090      def create_table(name, options = {}) 
    9191        table_definition = TableDefinition.new(self) 
    9292        table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false 
     93        extend_table_definition! table_definition 
    9394 
    9495        yield table_definition 
    9596 
     
    298299        def options_include_default?(options) 
    299300          options.include?(:default) && !(options[:null] == false && options[:default].nil?) 
    300301        end 
     302 
     303        def extend_table_definition!(table_definition) 
     304          class << table_definition 
     305            def method_missing(name, *args) 
     306              column(name, *args) 
     307            end 
     308          end 
     309        end 
    301310    end 
    302311  end 
    303312end