-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
base: main
Are you sure you want to change the base?
Add pretty_generated option for code fields #3782
Conversation
Code Climate has analyzed commit 1e2f90b and detected 0 issues on this pull request. View more on Code Climate. |
@Paul-Bob. I ran into the Rubocop errors below: lib/avo/fields/code_field.rb:17:11: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - format_using.
format_using = -> {
^^^^^^^^^^^^
[Correctable] Lint/UselessAssignment: Useless assignment to variable - update_using.
Raw Output:
lib/avo/fields/code_field.rb:21:11: W: [Correctable] Lint/UselessAssignment: Useless assignment to variable - update_using.
update_using = -> {
^^^^^^^^^^^^ Which I solved by changing from this: if @pretty_generated
format_using = -> {JSON.pretty_generate(JSON.parse(value.to_json))}
update_using = -> { JSON.parse(value)}
end To this in the code_field.rb file: 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 This code solved the error but it uses a lambda. I don't know if it could use improvement or not. Currently, the prettified version of the JSON is displayed both on the edit and show page. To test this out in my browser, i added an seo field to the post resource. The edit function works as expected and the Pretty rendering of the JSON doesn't break on updating. |
@Paul-Bob I'll then proceed with the tests if they are needed and then update the documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting this PR. Let's add tests and documentation, and it'll be good to go!
Regarding the tests, let's commit those to the PR |
I had stumbled upon an issue I could not solve in my first draft of one of the tests. It involved how to fill in the JSON in the code field. I attached the pictures and the code for context in my earlier 2 comments above. If you could help me look at it and get unstuck. |
It's ok to commit code that's still a work in progress. We won't merge until it's ready. But it's tough for me to test things and give feedback if the code isn't committed. Otherwise, I'd need to recreate your setup by copying snippets from the comments, which isn't ideal |
Okay. Let me commit the code from the tests shortly so that you can look at them. |
Currently, the test is failing with a parser error CodeField with pretty_generated option for a JSON code field correctly formats JSON code on create and displays it in a pretty way on the show page
Failure/Error: args[:update_using] ||= -> {JSON.parse(value)}
JSON::ParserError:
unexpected token at end of stream '"{\n \"name\": \"New York'
# /usr/share/rvm/gems/ruby-3.3.1/gems/json-2.10.2/lib/json/common.rb:248:in `parse'
# /usr/share/rvm/gems/ruby-3.3.1/gems/json-2.10.2/lib/json/common.rb:248:in `parse'
# ./lib/avo/fields/code_field.rb:16:in `block in initialize'
# ./lib/avo/execution_context.rb:69:in `instance_exec'
# ./lib/avo/execution_context.rb:69:in `handle'
|
…com:zhephyn/avo into add_pretty_generated_option_for_code_fields
Hi @zhephyn, The issue was that when visiting a form where the DB value for the JSON was I've updated the args[:format_using] ||= -> { value.blank? ? value : JSON.pretty_generate(value) } Now, it properly checks if the value is blank and avoids pretty-generating it in such cases. |
@Paul-Bob. Okay. This really gave me a hard time to figure out. Is there a specific approach you used when debugging that allowed you to know what the problem was? I had made changes to my city.rb file locally to use the pretty_generated option instead of format_using and update_using. So I don't get why the test was still failing. Secondly, I have a working copy of the documentation changes on my local machine that I'm gonna push momentarily. Apart from this, is there anything else you'd want me to help out with before we merge the PR? PS: Thanks so much for your patience🙏 |
Yes, I did exactly that, on the When I visited the creation form page, the
Awesome!
I think that's pretty much it, everything is working, tested, and documented. I'll give it one final review, push any tweaks if necessary, and aim to merge it later this week. Thanks so much for this contribution, you're on a hot streak! 😄
Don't worry about it, take your time |
I've pushed the documentation PR to the documentation repository. Plus, i want to take on another issue. Is there one you have preference for that you want to be worked on this week? If yes, i could take on that and start on it. Otherwise, let me browse through them and pick one to work on. |
Description
This PR adds a pretty generated option for code fields such that;
Instead of:
field :body, as: :code, format_using: -> { JSON.pretty_generate(JSON.parse(value.to_json)) }, update_using: -> { JSON.parse(value) }
We can use this syntax instead:
field :body, as: :code, pretty_generated: true
Fixes #3727
Checklist: