bullet_train
bullet_train copied to clipboard
Custom Field Partials
The idea came up in #1149 to give the developers the ability to create their own custom field partials. They could then subsequently run the command whenever they want to create said new partial.
cc/ @jagthedrummer @kaspth @pascallaliberte @andrewculver
An example from @jagthedrummer:
rails g super_scaffold:field_type canvas_field
# The command above would generate stuff like:
# app/views/fields/_canvas_field.html.erb
# app/javascript/controllers/fields/canvas_field_controller.js
rails g super_scaffold Project Team name:text_field drawing:canvas_field
What we'll need to do:
- [ ] Create a new rails generator (
rails g super_scaffold:field_type
) - [ ] Create a partial (maybe with a similar naming convention to
tangible_things
so we have a hook to catch when creating the new partial from a template) - [ ] Create a corresponding Stimulus Controller
- [ ] Add
valid_partial_types
to Attribute class and other necessary files - [ ] Look over the Transformer to see if it needs updating to handle the new field partial
- [ ] Internal tests to ensure everything is working correctly
valid_partial_types
We will need a list of valid_partial_types
or something similar to handle the conditional we have in the Attributes class in partial_name
.
https://github.com/bullet-train-co/bullet_train-core/blob/509c34602cfde35c7aa0fd583e459d24f982058b/bullet_train-super_scaffolding/lib/scaffolding/attribute.rb#L126-L129
I'm thinking we can override partial_name
in the starter repository, similar to what we do in the teams controller, etc.
https://github.com/bullet-train-co/bullet_train/blob/0211217230642a8f8f960391bcdbe700e9179c00/app/controllers/account/teams_controller.rb#L6-L10
For instance, we could make a hash like this to which we would add the custom field:
def valid_partial_types
{
canvas_field: "canvas_field"
# 🚅 super scaffolding will insert new fields above this line.
}
end
Then we could check for custom partial fields before hitting the conditional:
def partial_name
return options[:attribute] if options[:attribute]
return valid_field_partials[type.to_sym] if valid_field_partials[type.to_sym]
...
end
Updating the Transformer
Concerning the Transformer, I think most of the logic should be okay because most things are handled with an attribute
object (for example, the partial name here).
https://github.com/bullet-train-co/bullet_train-core/blob/509c34602cfde35c7aa0fd583e459d24f982058b/bullet_train-super_scaffolding/lib/scaffolding/transformer.rb#L822-L824
There might be some things that we aren't catching though, so we'll just have to proceed with caution.
There are also some minor partial-based discrepancies like this:
https://github.com/bullet-train-co/bullet_train-core/blob/509c34602cfde35c7aa0fd583e459d24f982058b/bullet_train-super_scaffolding/lib/scaffolding/transformer.rb#L826-L828
I don't want to go out of the way to override the Transformer as well, so we could just add a new method to Attribute
and, again, override it in the starter repository. We would then call that method in the Transformer to cover any extra custom scaffolding that we need for the partial.