rails/rails — pull request #13972 by joshuaflanagan (2014-02-08 02:48:21+00:00)
Title: Fix search for create_table_migration template override

A developer should be able to customize the template used by the model or migration generator, by adding a file to their source tree in a predictable location.

This functionality was likely broken for *create table migrations* by:
https://github.com/rails/rails/commit/20e041579f5fe9df51bdc40048cc1ef78915e116

After that change, you could now generate a migration:

`rails g migration CreateFoo`

and it would look for a custom template at `/lib/templates/active_record/migration/create_table_migration.rb`

However, if you generated the migration by generating a model:

`rails g model Bar`

it would look for a template at the unpredictable and awkward path `/lib/templates/migration/templates/create_table_migration.rb`

This change adds the migration generator's template paths to the model generator's search paths. When you generate a model, it will now look for templates in order:

```
/lib/templates/active_record/model/create_table_migration.rb
/lib/rails/generators/active_record/model/templates/create_table_migration.rb
/lib/templates/active_record/migration/create_table_migration.rb
/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
```

This has the side-effect of allowing you to have different templates for creating a table, depending on whether you generated a model or the migration directly. That was not the motivation, but its likely more helpful than harmful.

- [ ] Seeking feedback on how to properly modify the `source_paths` for `ModelGenerator`. I'm not very familiar with generator or Thor `Action` internals, so I had to blindly stab until I found something that worked. I particularly do not like the awkwardness of the `ensure_migration_source_paths` method - ideally that would be handled by some sort of initializer.
- [ ] Confirm we want to retain the functionality introduced by https://github.com/rails/rails/commit/20e041579f5fe9df51bdc40048cc1ef78915e116. It may be preferable to revert that commit. It introduced an awkward coupling between the ModelGenerator and MigrationGenerator that is exposed by this fix.