From f00d9c5eff542f6ccbc318ad44b8b522e94f6e81 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sun, 6 Nov 2011 15:47:34 +0300 Subject: [PATCH 1/2] generate migrations with new .change method for rails >= 3.1. closes #1345 --- .../active_record/templates/migration.rb | 8 ++++++- .../templates/migration_existing.rb | 6 ++--- .../active_record_generator_test.rb | 22 ++++++++++++++----- test/support/helpers.rb | 16 ++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/generators/active_record/templates/migration.rb b/lib/generators/active_record/templates/migration.rb index d97e8918..f91ab270 100644 --- a/lib/generators/active_record/templates/migration.rb +++ b/lib/generators/active_record/templates/migration.rb @@ -1,5 +1,9 @@ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration +<% if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1 -%> + def self.change +<% else -%> def self.up +<% end -%> create_table(:<%= table_name %>) do |t| t.database_authenticatable :null => false t.recoverable @@ -11,7 +15,7 @@ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %> # t.token_authenticatable -<% for attribute in attributes -%> +<% attributes.each do |attribute| -%> t.<%= attribute.type %> :<%= attribute.name %> <% end -%> @@ -25,7 +29,9 @@ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration # add_index :<%= table_name %>, :authentication_token, :unique => true end +<% unless ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1 -%> def self.down drop_table :<%= table_name %> end +<% end -%> end diff --git a/lib/generators/active_record/templates/migration_existing.rb b/lib/generators/active_record/templates/migration_existing.rb index 3e52fc0c..94376969 100644 --- a/lib/generators/active_record/templates/migration_existing.rb +++ b/lib/generators/active_record/templates/migration_existing.rb @@ -5,13 +5,13 @@ class AddDeviseTo<%= table_name.camelize %> < ActiveRecord::Migration t.recoverable t.rememberable t.trackable - + # t.encryptable # t.confirmable # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %> # t.token_authenticatable -<% for attribute in attributes -%> +<% attributes.each do |attribute| -%> t.<%= attribute.type %> :<%= attribute.name %> <% end -%> @@ -29,6 +29,6 @@ class AddDeviseTo<%= table_name.camelize %> < ActiveRecord::Migration def self.down # By default, we don't want to make any assumption about how to roll back a migration when your # model already existed. Please edit below which fields you would like to remove in this migration. - raise ActiveRecord::IrreversibleMigration + raise ActiveRecord::IrreversibleMigration end end diff --git a/test/generators/active_record_generator_test.rb b/test/generators/active_record_generator_test.rb index f2b466ed..7bb0b3c7 100644 --- a/test/generators/active_record_generator_test.rb +++ b/test/generators/active_record_generator_test.rb @@ -7,20 +7,30 @@ if DEVISE_ORM == :active_record tests ActiveRecord::Generators::DeviseGenerator destination File.expand_path("../../tmp", __FILE__) setup :prepare_destination - + test "all files are properly created" do - run_generator %w(monster) - assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/ - assert_migration "db/migrate/devise_create_monsters.rb" + with_rails_version :MAJOR => 3, :MINOR => 0 do + run_generator %w(monster) + assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/ + assert_migration "db/migrate/devise_create_monsters.rb", /def self\.up/ + end end - + + test "all files are properly created with rails31 migration syntax" do + with_rails_version :MAJOR => 3, :MINOR => 1 do + run_generator %w(monster) + assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/ + assert_migration "db/migrate/devise_create_monsters.rb", /def self\.change/ + end + end + test "update model migration when model exists" do run_generator %w(monster) assert_file "app/models/monster.rb" run_generator %w(monster) assert_migration "db/migrate/add_devise_to_monsters.rb" end - + test "all files are properly deleted" do run_generator %w(monster) run_generator %w(monster) diff --git a/test/support/helpers.rb b/test/support/helpers.rb index 3e63a331..020623bd 100644 --- a/test/support/helpers.rb +++ b/test/support/helpers.rb @@ -57,4 +57,20 @@ class ActiveSupport::TestCase object.send :"#{key}=", value end end + + def with_rails_version(constants, &block) + saved_constants = {} + constants.each do |constant, val| + saved_constants[constant] = ::Rails::VERSION.const_get constant + Kernel::silence_warnings { ::Rails::VERSION.const_set(constant, val) } + end + + begin + block.call + ensure + constants.each do |constant, val| + Kernel::silence_warnings { ::Rails::VERSION.const_set(constant, saved_constants[constant]) } + end + end + end end From 493ddbd99ea763c13ca5e49544ecb5863949037c Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sun, 6 Nov 2011 16:15:17 +0300 Subject: [PATCH 2/2] change method should be instance method. closes #1345 --- lib/generators/active_record/templates/migration.rb | 2 +- test/generators/active_record_generator_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/active_record/templates/migration.rb b/lib/generators/active_record/templates/migration.rb index f91ab270..a52b6565 100644 --- a/lib/generators/active_record/templates/migration.rb +++ b/lib/generators/active_record/templates/migration.rb @@ -1,6 +1,6 @@ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration <% if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1 -%> - def self.change + def change <% else -%> def self.up <% end -%> diff --git a/test/generators/active_record_generator_test.rb b/test/generators/active_record_generator_test.rb index 7bb0b3c7..5193d4f5 100644 --- a/test/generators/active_record_generator_test.rb +++ b/test/generators/active_record_generator_test.rb @@ -20,7 +20,7 @@ if DEVISE_ORM == :active_record with_rails_version :MAJOR => 3, :MINOR => 1 do run_generator %w(monster) assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/ - assert_migration "db/migrate/devise_create_monsters.rb", /def self\.change/ + assert_migration "db/migrate/devise_create_monsters.rb", /def change/ end end