1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

add mini-validator on creating migration

move validation to AR
This commit is contained in:
Jan Bernacki 2012-08-22 14:50:41 +04:00
parent e8c9f0513d
commit d965bbfe9f
2 changed files with 16 additions and 0 deletions

View file

@ -7,6 +7,7 @@ module ActiveRecord
def create_migration_file def create_migration_file
set_local_assigns! set_local_assigns!
validate_file_name!
migration_template "migration.rb", "db/migrate/#{file_name}.rb" migration_template "migration.rb", "db/migrate/#{file_name}.rb"
end end
@ -41,6 +42,14 @@ module ActiveRecord
attribute.name.singularize.foreign_key attribute.name.singularize.foreign_key
end.to_sym end.to_sym
end end
private
def validate_file_name!
unless file_name =~ /^[_a-z0-9]+$/
raise IllegalMigrationNameError.new(file_name)
end
end
end end
end end
end end

View file

@ -28,6 +28,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
run_generator [migration] run_generator [migration]
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/ assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
end end
def test_migration_with_invalid_file_name
migration = "add_something:datetime"
assert_raise ActiveRecord::IllegalMigrationNameError do
run_generator [migration]
end
end
def test_add_migration_with_attributes def test_add_migration_with_attributes
migration = "add_title_body_to_posts" migration = "add_title_body_to_posts"