Rspec-rails: No easy way to override the scaffold controller spec generator

Created on 14 Jun 2013  ·  5Comments  ·  Source: rspec/rspec-rails

I want to change the way the scaffold controller spec gets generated.

When creating a new generator in my rails app at:
lib/generators/rspec/scaffold/templates/controller_spec.rb

This never gets invoked, only the out of the box rspec-rails scaffold controller generator gets invoked.

It would be great if we could easily just change the way the specs for scaffold controllers are generated.

Most helpful comment

Looks like it actually does work, I just had the path to put the overriding template file wrong. The correct path is:
lib/templates/rspec/scaffold/controller_spec.rb
(prior I had lib/generators/rspec/scaffold/templates/controller_spec.rb)

When putting a file in that correct path, the template gets overriden by that file.
Should I update the readme to indicated where to put these files?

All 5 comments

I don't really know how to make this work. It seems like work on Rails' generator mechanism would be required.

If you have any more information about this, or want to implement it, I'd be happy to look at some code :) But given this, I'm going to close as not an issue.

In the rails generator documentation, it says you can override the templates by putting a file with the same name in your app with the same relative path of the original generator:
http://guides.rubyonrails.org/generators.html#customizing-your-workflow-by-changing-generators-templates
So, based on that logic, it should be theoretically possible to do the same with the controller_spec.rb file
I'm going to look through the rails generator code to see where this is done (i.e. where it enables you to override helper.rb)

@oveddan did you resolve this issue?

Not yet - I think it has do something with the way rails selects its generator template for scaffolds. Need to look into this more.

Looks like it actually does work, I just had the path to put the overriding template file wrong. The correct path is:
lib/templates/rspec/scaffold/controller_spec.rb
(prior I had lib/generators/rspec/scaffold/templates/controller_spec.rb)

When putting a file in that correct path, the template gets overriden by that file.
Should I update the readme to indicated where to put these files?

Was this page helpful?
0 / 5 - 0 ratings