Skip to content

Add pretty_generated option for code fields #3782

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def initialize(id, **args, &block)
@for_attribute = args[:for_attribute]
@meta = args[:meta]
@copyable = args[:copyable] || false
@pretty_generated = args[:pretty_generated] || false

@args = args

Expand Down
10 changes: 10 additions & 0 deletions lib/avo/fields/code_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class CodeField < BaseField
def initialize(id, **args, &block)
hide_on :index

if args[:pretty_generated]
args[:format_using] ||= lambda do
JSON.pretty_generate(JSON.parse(value.to_json))
end

args[:update_using] ||= lambda do
JSON.parse(value)
end
end

super(id, **args, &block)

@language = args[:language].present? ? args[:language].to_s : "javascript"
Expand Down
Loading