简体   繁体   中英

How can a gem (unintentionally) change the migrations path?

I wrote a Gem ( https://github.com/absolutedevops/civo ) which is a simple Rails Engine containing a few API accessing models.

However, when I include it in a Rails project, any generators create their files under the Gem's source code not the project's. I can't see anything I'm doing in the Gem that would cause this. It's repeatable (it's happening in two projects at my company and I can reproduce it with a minimal set of steps below).

Can anyone tell me how I've managed this? I've been a Rails user for many years but haven't ever come across this before.

$ rails -v
Rails 4.2.6
$ rails new civo-test
[...]
Bundle complete! 12 Gemfile dependencies, 55 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
$ cd civo-test
$ echo 'gem "civo"' >> Gemfile
$ bundle
[...]
Bundle complete! 13 Gemfile dependencies, 66 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ rails g migration a_new_migration_here
Running via Spring preloader in process 75091
      invoke  active_record
      create    db/migrate/20160411093346_a_new_migration_here.rb
$ ls -l db/migrate/20160411093346_a_new_migration_here.rb
ls: db/migrate/20160411093346_a_new_migration_here.rb: No such file or directory
$ rails g migration a_new_migration_here                 
Running via Spring preloader in process 75193
      invoke  active_record
   identical    db/migrate/20160411093346_a_new_migration_here.rb
$ ls -l /Users/andy/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/civo-0.3.21/db/migrate/ 
total 8
-rw-r--r--  1 andy  staff  73 11 Apr 10:33 20160411093346_a_new_migration_here.rb

Looking at the civo-ruby repository at https://github.com/absolutedevops/civo-ruby (which I think is the one you meant to link to), you define ENGINE_ROOT to point to the location where your engine code is installed. Rails also uses this constant in few places, and I suspect this is where the collision is happening.

You could rename the constant, eg to CIVO_ENGINE_ROOT , or you might be able to move it into the Civo module so it is namespaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM