Skip to content
Browse files

Added a helper to look for an modifying migration. If one is found du…

…ring :revoke, the modifying migration is deleted. If the modifying migration is not found, the creating migration is deleted.
  • Loading branch information...
1 parent 99d539b commit 8ad414b2175e71a9a00381537cea0ce1cb7813ea @seanski seanski committed
View
16 lib/generators/active_record/devise_generator.rb
@@ -1,6 +1,7 @@
require 'rails/generators/active_record'
require 'generators/devise/orm_helpers'
+
module ActiveRecord
module Generators
class DeviseGenerator < ActiveRecord::Generators::Base
@@ -11,11 +12,18 @@ class DeviseGenerator < ActiveRecord::Generators::Base
def copy_devise_migration
exists = model_exists?
- exists = !exists if behavior == :revoke
- unless exists
- migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
+ unless behavior == :revoke
+ unless exists
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
+ else
+ migration_template "migration_existing.rb", "db/migrate/add_devise_to_#{table_name}"
+ end
else
- migration_template "migration_existing.rb", "db/migrate/add_devise_to_#{table_name}"
+ if migration_exists?(table_name)
+ migration_template "migration_existing.rb", "db/migrate/add_devise_to_#{table_name}"
+ else
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
+ end
end
end
View
8 lib/generators/devise/orm_helpers.rb
@@ -14,6 +14,14 @@ def model_contents
def model_exists?
File.exists?(File.join(destination_root, model_path))
end
+
+ def migration_exists?(table_name)
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_devise_to_#{table_name}.rb$/).first
+ end
+
+ def migration_path
+ @migration_path ||= File.join("db", "migrate")
+ end
def model_path
@model_path ||= File.join("app", "models", "#{file_path}.rb")
View
6 test/generators/active_record_generator_test.rb
@@ -23,6 +23,12 @@ class ActiveRecordGeneratorTest < Rails::Generators::TestCase
test "all files are properly deleted" do
run_generator %w(monster)
+ run_generator %w(monster)
+ assert_migration "db/migrate/devise_create_monsters.rb"
+ assert_migration "db/migrate/add_devise_to_monsters.rb"
+ run_generator %w(monster), :behavior => :revoke
+ assert_no_migration "db/migrate/add_devise_to_monsters.rb"
+ assert_migration "db/migrate/devise_create_monsters.rb"
run_generator %w(monster), :behavior => :revoke
assert_no_file "app/models/monster.rb"
assert_no_migration "db/migrate/devise_create_monsters.rb"

0 comments on commit 8ad414b

Please sign in to comment.
Something went wrong with that request. Please try again.