pat / combustion

Simple, elegant testing for Rails Engines
MIT License
708 stars 51 forks source link

Q: any way to use generators in internal app? #87

Closed jrochkind closed 6 years ago

jrochkind commented 6 years ago

It would be convenient to be able to use rails generators (rails generate model ..., rails generate scaffold ... etc) in the ./spec/internal dummy app. Has anyone found a way to do that?

pat commented 6 years ago

I'll have to look into the Rails code to see how it figures out a Rails app's location/context for the generators… it may be feasible? My biggest concern would be about whether this logic in Rails changes often (thus making support across Rails versions challenging). Let's keep this issue open for the moment, and I'll see what I can figure out.

pat commented 6 years ago

Okay, a little bit of research, and here's where I'm at…

There's two different options here: should the generate command operate within the context of the engine? Or within the context of the dummy app? Arguably it could be either - and Rails itself has some code under the hood that allows for the former: if you use Rails to generate your engine, it'll create a bin/rails file that operates within your engine's context just fine.

But, if you want a bin/rails to work from the context of your dummy app, then instead, you could have the following contents:

#! /usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
Bundler.require :default, :development

APP_PATH = File.expand_path('../lib/myengine', __dir__)
Combustion.initialize! :all # or whichever parts of Rails you want
require 'rails/commands'

If you then use this, the output won't mention spec/internal, but the files will end up in there.

I don't think there's a way to have all of this context automatically picked up via the global rails executable (as opposed to a local bin/rails file), but others are very welcome to prove me wrong :)

… now, should all of this be wrapped up into part of the combust inserted templates? I'm not so sure - Rails puts its own file there if you've used its plugin generator, and with the two different contexts I'd rather not make presumptions one way or another. If folks really want this behaviour, I think I'll leave them to add it themselves - and you can name the executable whatever you like (so, bin/dummy_rails could then be invoked bin/dummy_rails generate model foo with the correct results).

Feedback on all of this very much welcome!

pat commented 6 years ago

Marking this as closed, but don't hesitate to add thoughts/suggestions if you'd like.