diff options
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 27 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 69 | ||||
-rw-r--r-- | railties/guides/rails_guides/generator.rb | 3 |
3 files changed, 90 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 0e5e33fa02..11da84e245 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -158,7 +158,13 @@ module ActiveRecord td = table_definition td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false - td.instance_eval(&blk) if blk + if block_given? + if blk.arity == 1 + yield td + else + td.instance_eval(&blk) + end + end if options[:force] && table_exists?(table_name) drop_table(table_name) @@ -235,14 +241,19 @@ module ActiveRecord # # See also Table for details on # all of the various column transformation - def change_table(table_name, options = {}) - if supports_bulk_alter? && options[:bulk] - recorder = ActiveRecord::Migration::CommandRecorder.new(self) - yield Table.new(table_name, recorder) - bulk_change_table(table_name, recorder.commands) - else - yield Table.new(table_name, self) + def change_table(table_name, options = {}, &blk) + bulk_change = supports_bulk_alter? && options[:bulk] + recorder = bulk_change ? ActiveRecord::Migration::CommandRecorder.new(self) : self + table = Table.new(table_name, recorder) + + if block_given? + if blk.arity == 1 + yield table + else + table.instance_eval(&blk) + end end + bulk_change_table(table_name, recorder.commands) if bulk_change end # Renames a table. diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index e8ad37d437..d6c7edc461 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1742,6 +1742,75 @@ if ActiveRecord::Base.connection.supports_migrations? ensure Person.connection.drop_table :testings rescue nil end + + def test_create_table_should_not_have_mixed_syntax + assert_raise(NoMethodError) do + Person.connection.create_table :testings, :force => true do |t| + t.string :foo + integer :bar + end + end + assert_raise(NameError) do + Person.connection.create_table :testings, :force => true do + t.string :foo + integer :bar + end + end + end + + def test_change_table_without_block_parameter_no_bulk + Person.connection.create_table :testings, :force => true do + string :foo + end + assert Person.connection.column_exists?(:testings, :foo, :string) + + Person.connection.change_table :testings do + remove :foo + integer :bar + end + + assert_equal %w(bar id), Person.connection.columns(:testings).map { |c| c.name }.sort + ensure + Person.connection.drop_table :testings rescue nil + end + + if ActiveRecord::Base.connection.supports_bulk_alter? + def test_change_table_without_block_parameter_with_bulk + Person.connection.create_table :testings, :force => true do + string :foo + end + assert Person.connection.column_exists?(:testings, :foo, :string) + + assert_queries(1) do + Person.connection.change_table(:testings, :bulk => true) do + integer :bar + string :foo_bar + end + end + + assert_equal %w(bar foo foo_bar id), Person.connection.columns(:testings).map { |c| c.name }.sort + ensure + Person.connection.drop_table :testings rescue nil + end + end + + def test_change_table_should_not_have_mixed_syntax + Person.connection.create_table :testings, :force => true do + string :foo + end + assert_raise(NoMethodError) do + Person.connection.change_table :testings do |t| + t.remove :foo + integer :bar + end + end + assert_raise(NameError) do + Person.connection.change_table :testings do + t.remove :foo + integer :bar + end + end + end end # SexierMigrationsTest class MigrationLoggerTest < ActiveRecord::TestCase diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 4682ead66e..6f6d3bda80 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -137,7 +137,8 @@ module RailsGuides if guide =~ /\.html\.erb$/ # Generate the special pages like the home. - result = view.render(:layout => 'layout', :file => guide) + # Passing a template handler in the template name is deprecated. So pass the file name without the extension. + result = view.render(:layout => 'layout', :file => $`) else body = File.read(File.join(source_dir, guide)) body = set_header_section(body, view) |