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

Add pretty_generated option for code fields #3727

Open
Paul-Bob opened this issue Mar 7, 2025 · 1 comment
Open

Add pretty_generated option for code fields #3727

Paul-Bob opened this issue Mar 7, 2025 · 1 comment
Labels
😎 Cool Developer Experience (DX) Good first issue Good for newcomers Help wanted We could use some help with this

Comments

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Mar 7, 2025

Context

Currently, it’s a common pattern to use format_using alongside update_using to render a prettified version of a code field's content while ensuring valid parsing on update. A typical example involves formatting JSON fields like so:

field :body, as: :code,
  format_using: -> {
    JSON.pretty_generate(JSON.parse(value.to_json))
  },
  update_using: -> {
    JSON.parse(value)
  }

This approach, while effective, introduces redundancy and boilerplate code. To streamline this, we propose adding a first-party pretty_generated option, allowing developers to enable this behavior with a simple flag:

field :body, as: :code, pretty_generated: true
@Paul-Bob Paul-Bob added this to Issues Mar 7, 2025
@Paul-Bob Paul-Bob moved this to Triage in Issues Mar 7, 2025
@Paul-Bob Paul-Bob moved this from Triage to To Do in Issues Mar 7, 2025
@Paul-Bob Paul-Bob added Help wanted We could use some help with this Good first issue Good for newcomers Developer Experience (DX) 😎 Cool labels Mar 7, 2025
@ObiWanKeoni
Copy link
Contributor

Hi @Paul-Bob 👋
Can I suggest a new JSON field that inherits from code_field and has this option?

Thought being that when we render things like custom_css, pretty_generated would have no (or perhaps disastrous) effects when attempting to pretty generate a json value. I think having a json field would also allow us to infer the language as javascript for syntax highlighting and have conditional rendering if, by chance, an app used a text field instead of JSONB and wanted to render it as pretty JSON.

TL;DR instead of:

field :body, as: :code, pretty_generated: true

maybe

field :body, as: :json, pretty_generated: true

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
😎 Cool Developer Experience (DX) Good first issue Good for newcomers Help wanted We could use some help with this
Projects
Status: To Do
Development

No branches or pull requests

2 participants