Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Field Partials #1169

Open
6 tasks
gazayas opened this issue Nov 20, 2023 · 0 comments
Open
6 tasks

Custom Field Partials #1169

gazayas opened this issue Nov 20, 2023 · 0 comments

Comments

@gazayas
Copy link
Contributor

gazayas commented Nov 20, 2023

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.

def permitted_fields
[
# 🚅 super scaffolding will insert new fields above this line.
]
end

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant