Skip to content

Commit

Permalink
generate migrations with new .change method for rails >= 3.1. closes h…
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Nov 6, 2011
1 parent 95be78a commit f00d9c5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,7 +15,7 @@ def self.up
# 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 -%>
Expand All @@ -25,7 +29,9 @@ def self.up
# 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
6 changes: 3 additions & 3 deletions lib/generators/active_record/templates/migration_existing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ def self.up
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 -%>
Expand All @@ -29,6 +29,6 @@ def self.up
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
22 changes: 16 additions & 6 deletions test/generators/active_record_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@ class ActiveRecordGeneratorTest < Rails::Generators::TestCase
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)
Expand Down
16 changes: 16 additions & 0 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,20 @@ def swap(object, new_values)
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

0 comments on commit f00d9c5

Please sign in to comment.