-
-
Notifications
You must be signed in to change notification settings - Fork 275
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
Comments
Hi @Paul-Bob 👋 Thought being that when we render things like TL;DR instead of: field :body, as: :code, pretty_generated: true maybe field :body, as: :json, pretty_generated: true What do you think? |
Hi @ObiWanKeoni
Suggestions and feedback are always welcome!
You're right. This option needs to be well-documented in terms of when to use it. It's intended for a specific use case, and the documentation should explicitly clarify that. To be honest,
Have you encountered such use cases before? Could you elaborate on the scenario? Thank you! |
Absolutely, I encounter this regularly. I have JSON fields all over my applications where this pattern would be extremely useful. Currently I'm implementing this boilerplate in numerous places: field :input, as: :code, stacked: true, theme: 'material-darker', language: 'javascript',
format_using: -> { value.is_a?(Hash) ? JSON.pretty_generate(value) : value },
update_using: -> { value.is_a?(String) ? JSON.parse(value) : value } The use case is very common with APIs and data processing applications. We receive JSON data (often as a Hash), need to display it pretty-printed in the UI for readability, but then parse it back when saving - nothing you're unfamiliar with I'm sure. This pattern repeats so much that I've considered creating a custom field type just for this purpose especially since changing some of this data across json fields is tedious (even if it's a trivial find/replace). But I believe having it upstream would benefit many developers beyond my organization. The proposed
The last item I think is the main point I'm making - if we are going to add options for JSON-specific representations, we might benefit from extracting that into a separate field since JSON carries different semantics than a base code representation anyway. Ultimately though, I'm comfortable implementing my own version of this if necessary Thanks @Paul-Bob - as always, really appreciate the work you are doing here. Love the product |
I understand the suggested direction. We intend to keep the Thank you for the context.
The code field already defaults to
A simple way to implement this on your end is to override the Here's a suggested approach: Step 1: Create a Custom BaseResourceGenerate the following file: # app/avo/base_resource.rb
module Avo
class BaseResource < Avo::Resources::Base
def field(id, as:, **kwargs)
return super if as != :json
super id,
as: :code,
stacked: true,
theme: 'material-darker',
format_using: -> { value.is_a?(Hash) ? JSON.pretty_generate(value) : value },
update_using: -> { value.is_a?(String) ? JSON.parse(value) : value },
**kwargs
end
end
end Step 2: Use the Custom Field in Your ResourceNow, you can use this custom behavior in your resource: # app/avo/resources/city.rb
class Avo::Resources::City < Avo::BaseResource
def fields
field :input, as: :json
end
end
Thank you for being an awesome community member! |
Context
Currently, it’s a common pattern to use
format_using
alongsideupdate_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: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:The text was updated successfully, but these errors were encountered: