1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Merge pull request #1420 from nashby/master

generate migrations with new .change method for rails >= 3.1. closes #1345
This commit is contained in:
José Valim 2011-11-06 05:23:58 -08:00
commit f9ef2cd32e
4 changed files with 42 additions and 10 deletions

View file

@ -1,5 +1,9 @@
class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
<% if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1 -%>
def 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

View file

@ -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

View file

@ -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 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)

View file

@ -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