Skip to content

Commit

Permalink
Merge pull request #115 from epigenesys/rails-5-generator
Browse files Browse the repository at this point in the history
Rails 5 generator
  • Loading branch information
rafaelfranca authored Sep 11, 2018
2 parents f7463cb + 1acf165 commit fccaac7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/generators/active_record/session_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 7 additions & 3 deletions test/generators/session_migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fccaac7

Please sign in to comment.