In a new Rails 6.1.3 project, I'm attempting to have bin/rails generate scaffold Thing name:string invoke the typical Rails scaffolding (including active_record and scaffold_controller) as well as invoking my own custom generator to create an additional ThingForm that I want to be a part of my scaffolding.
I created a custom generator for form that works just fine by itself. bin/rails generate form Thing name:string is properly setting up an app/forms/thing_form.rb.
But in attempting to get this generator hooked into the default scaffolding generator, I'm running into different problems with different approaches I've tried.
Inspired by
active_model_serializers, when I set up ahook_for :formas part ofRails::Generators::ResourceGeneratorit ends up no longer invokingactive_record. Here's the commit from my example repo: https://github.com/diachini/generator_hook_investigation/commit/8989b8f96b05ac1157d1d3a247bee9f0f2befe5d- To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with
active_model_serializersinstalled properly keeps theactive_recordinvocation happening.
- To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with
Taking a page out of this Gist explaining custom generators - I similarly attempted a
hook_foron theScaffoldGeneratorand theControllerGenerator- Just like my
active_model_serializersattempt, this no longer invokedactive_record. - It also began ignoring config for turning off
assetsandscaffold_stylesheet. - On a separate branch in my example repo: https://github.com/diachini/generator_hook_investigation/commit/86bc7b53ac34ea29d5e32d07486c73d962f86247
- Just like my
I saw a similar StackOverflow question with an update regarding copying the whole
ScaffoldGenerator, but was hoping to avoid that approach if there's something simple that I'm missing with thehook_forapproach thatactive_model_serializerstook.