diff --git a/lib/generators/active_record/session_migration_generator.rb b/lib/generators/active_record/session_migration_generator.rb index 4cc6f32..b827d8c 100644 --- a/lib/generators/active_record/session_migration_generator.rb +++ b/lib/generators/active_record/session_migration_generator.rb @@ -19,6 +19,10 @@ def session_table_name end current_table_name end + + def migration_version + "[#{ActiveRecord::Migration.current_version}]" if ActiveRecord::Migration.respond_to?(:current_version) + end end end end diff --git a/lib/generators/active_record/templates/migration.rb b/lib/generators/active_record/templates/migration.rb index a994589..0334c24 100644 --- a/lib/generators/active_record/templates/migration.rb +++ b/lib/generators/active_record/templates/migration.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %> def change create_table :<%= session_table_name %> do |t| t.string :session_id, :null => false diff --git a/test/generators/session_migration_generator_test.rb b/test/generators/session_migration_generator_test.rb index 73ce165..d17027e 100644 --- a/test/generators/session_migration_generator_test.rb +++ b/test/generators/session_migration_generator_test.rb @@ -8,21 +8,25 @@ class SessionMigrationGeneratorTest < Rails::Generators::TestCase destination 'tmp' setup :prepare_destination + def active_record_version + "\\[#{ActiveRecord::Migration.current_version}\\]" if ActiveRecord::Migration.respond_to?(:current_version) + end + def test_session_migration_with_default_name run_generator - assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/ + assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration#{active_record_version}/ end def test_session_migration_with_given_name run_generator ["create_session_table"] - assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/ + assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration#{active_record_version}/ end def test_session_migration_with_custom_table_name ActiveRecord::SessionStore::Session.table_name = "custom_table_name" run_generator assert_migration "db/migrate/add_sessions_table.rb" do |migration| - assert_match(/class AddSessionsTable < ActiveRecord::Migration/, migration) + assert_match(/class AddSessionsTable < ActiveRecord::Migration#{active_record_version}/, migration) assert_match(/create_table :custom_table_name/, migration) end ensure