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

feature: format_using and update_using field options #1797

Merged
merged 5 commits into from
Jun 28, 2023

Conversation

adrianthedev
Copy link
Collaborator

@adrianthedev adrianthedev commented Jun 21, 2023

Description

Enables developers to add custom formatters before the data is displayed in the UI and before it gets saved to the database.

We retrofitted format_using to use the ExecutionContext and dropped the value argument.

The update_using block is run just before the value is saved on the model.

Docs

field :metadata, as: :code,
  format_using: -> {
    if view == :edit
      JSON.generate(value)
    else
      value
    end
  },
  update_using: -> do
    ActiveSupport::JSON.decode(value)
  end

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

class ExecutionContext
attr_accessor :target, :context, :params, :view_context, :current_user, :request, :include, :main_app, :avo

def initialize(**args)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method initialize has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.

@codeclimate
Copy link

codeclimate bot commented Jun 21, 2023

Code Climate has analyzed commit cdb2462 and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

View more on Code Climate.

@useattractor
Copy link

useattractor bot commented Jun 21, 2023

feature: format_as and update_as field options 🔗

Stats

Language Score Trend
Ruby 8.42 (from 8.32) 📈 1.21%
JavaScript 28.35 (from 28.35) 📉 0.0%

Trends

Most Improved Largest Declines
Ruby No decreases for Ruby detected lib/avo/fields/base_field.rb and lib/avo/base_resource.rb
JavaScript No decreases for JavaScript detected No increases for JavaScript detected

To-Dos

New to Refactor Refactored
Ruby No new To-Dos for Ruby detected No completed To-Dos for Ruby detected
JavaScript No new To-Dos for JavaScript detected No completed To-Dos for JavaScript detected

@useattractor
Copy link

useattractor bot commented Jun 26, 2023

feature: format_as and update_as field options 🔗

Stats

Language Score Trend
Ruby 8.41 (from 8.32) 📈 1.0%
JavaScript 28.35 (from 28.35) 📉 0.0%

Trends

Most Improved Largest Declines
Ruby No decreases for Ruby detected lib/avo/fields/base_field.rb and lib/avo/base_resource.rb
JavaScript No decreases for JavaScript detected No increases for JavaScript detected

To-Dos

New to Refactor Refactored
Ruby No new To-Dos for Ruby detected No completed To-Dos for Ruby detected
JavaScript No new To-Dos for JavaScript detected No completed To-Dos for JavaScript detected

@useattractor
Copy link

useattractor bot commented Jun 26, 2023

feature: format_as and update_as field options 🔗

Stats

Language Score Trend
Ruby 8.4 (from 8.32) 📈 0.92%
JavaScript 28.35 (from 28.35) 📉 0.0%

Trends

Most Improved Largest Declines
Ruby No decreases for Ruby detected lib/avo/fields/base_field.rb and lib/avo/base_resource.rb
JavaScript No decreases for JavaScript detected No increases for JavaScript detected

To-Dos

New to Refactor Refactored
Ruby No new To-Dos for Ruby detected No completed To-Dos for Ruby detected
JavaScript No new To-Dos for JavaScript detected No completed To-Dos for JavaScript detected

class ExecutionContext
attr_accessor :target, :context, :params, :view_context, :current_user, :request, :include, :main_app, :avo

def initialize(**args)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method initialize has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.

@useattractor
Copy link

useattractor bot commented Jun 26, 2023

feature: format_as and update_as field options 🔗

Stats

Language Score Trend
Ruby 8.4 (from 8.32) 📈 0.98%
JavaScript 28.35 (from 28.35) 📉 0.0%

Trends

Most Improved Largest Declines
Ruby No decreases for Ruby detected lib/avo/fields/base_field.rb and lib/avo/base_resource.rb
JavaScript No decreases for JavaScript detected No increases for JavaScript detected

To-Dos

New to Refactor Refactored
Ruby No new To-Dos for Ruby detected No completed To-Dos for Ruby detected
JavaScript No new To-Dos for JavaScript detected No completed To-Dos for JavaScript detected

@useattractor
Copy link

useattractor bot commented Jun 26, 2023

feature: format_as and update_as field options 🔗

Stats

Language Score Trend
Ruby 8.4 (from 8.32) 📈 0.96%
JavaScript 28.35 (from 28.35) 📉 0.0%

Trends

Most Improved Largest Declines
Ruby No decreases for Ruby detected lib/avo/fields/base_field.rb and lib/avo/base_resource.rb
JavaScript No decreases for JavaScript detected No increases for JavaScript detected

To-Dos

New to Refactor Refactored
Ruby No new To-Dos for Ruby detected No completed To-Dos for Ruby detected
JavaScript No new To-Dos for JavaScript detected No completed To-Dos for JavaScript detected

@adrianthedev adrianthedev changed the title feature: format_as and update_as field options feature: format_using and update_using field options Jun 26, 2023
@adrianthedev adrianthedev merged commit af82381 into main Jun 28, 2023
13 of 14 checks passed
@adrianthedev adrianthedev deleted the feature/format-as-and-update-as-field-options branch June 28, 2023 19:31
@github-actions
Copy link
Contributor

This PR has been merged into main. The functionality will be available in the next release.

Please check the release guide for more information.

@koppen
Copy link
Contributor

koppen commented Aug 3, 2023

@adrianthedev Thanks for all your hard work, and please take the following as constructive criticism:

Dropping the value argument is a breaking change and it did indeed break our application in production. We've got a bunch of formatters in our application using the documented API, ie

field :is_writer, as: :text, format_using: -> (value) { value.present? ? '👍' : '👎' }

which are apparently supposed to now be

field :is_writer, as: :text, format_using: -> { value.present? ? '👍' : '👎' }

but we had no way of knowing that we needed to make this change. Sure, we could have verified the changes in more depth, but when the release notes state that a new feature is added (which we don't use) and links to documentation that states our way of doing things is correct, we didn't raise any red flags.

We like Avo (so much that we've paid for it and contributed previously) and I beg of you; please be more wary about breaking changes in the future, they make me look bad ;) I would much have preferred if the change wasn't breaking. Or if it had to be breaking, that it was clearly indicated in the release notes. Also, the documentation should probably have been updated, especially seeing how it was linked from the release notes.

Cheers! I'm looking forward to digging into update_using, though :)

@adrianthedev
Copy link
Collaborator Author

adrianthedev commented Aug 8, 2023

Hey @koppen.

Thanks for the feedback. Yes, this is constructive 🙌
Please allow me to address this.

We know that breaking changes are not cool. We're developers too.
But they are needed sometimes to improve the product.
With this particular change, we are making the API more consistent (by removing the block arguments) across all of Avo.

We usually cover all changes in our release notes. Even this one is covered.
We also provide an up-to-date upgrade guide which we encourage everyone to read before updating Avo. I think that goes for any dependency.

Regarding the documentation, I'm not sure how that could have been out of sync. The release went out on July 4th and the docs were updated a few days prior.

That being said, I know breaking changes suck and we definitely don't want to make you look bad, but there might be others in the future. That's how technology advances.
We thought that having an upgrade guide that people will consult before updating the package will be enough.
We're open to suggestions to how we can improve the process. We even thought about something automated to run after a bundle install, but bundler doesn't support that.

Apologies for the trouble and open to suggestions for improvement.
And thanks for sending this in.

@koppen
Copy link
Contributor

koppen commented Aug 9, 2023

The change is fine, and I am perfectly okay with breaking changes, I just wish it had been more obvious to me.

We usually cover all changes in our release notes. Even this one is covered.

The release notes is what we usually run through on upgrades, mostly because that's what Dependabot surfaces for us. And indeed, the change was mentioned there as a new feature.

We also provide an up-to-date upgrade guide

I did not know about this 😳 That said, it seems to stop at v2.11 and doesn't mention v.2.36?

CleanShot 2023-08-09 at 09 22 17

Regarding the documentation, I'm not sure how that could have been out of sync. The release went out on July 4th and the docs were updated a few days prior.

Again, something doesn't seem right here. I do not see the updated documentation at that URL:

CleanShot 2023-08-09 at 09 23 45

So a couple of concrete suggestions that would help me in the future:

  1. Any chance the upgrade guide could be linked from the release notes? I reckon it'd fit right into the "More information and release video here" line, something like "Upgrade guide at https://docs.avohq.io/2.0/upgrade.html and more information and release video here"
  2. Please explicitly mark breaking changes in the release notes, we're not likely to dive into an upgrade guide unless we know we need to.

@adrianthedev
Copy link
Collaborator Author

Wow. That is still hapenning. You're a long-time user of Avo.

You are seeing the old docs. Please try to do a ctrl/command+shift+R on that page and clear the cookies and service worker for docs.avohq.io.
The old docs website used a service worker which was indefinitely cached on the browser. There's no "turning it off" from the server. We're open to suggestions on that.

We changed those docs about a year ago.
Sorry about that.

@adrianthedev
Copy link
Collaborator Author

Your suggestions do make sense. I'll go ahead and follow them in the next releases ❤️

@koppen
Copy link
Contributor

koppen commented Aug 9, 2023

Whoa! Everything looks new and fresh! It's like the wool has been pulled from my eyes 😆

Yeah, service workers can be a problematic relationship, and indeed, there's no turning it off server-side - short of changing the URLs entirely, I guess.

@adrianthedev
Copy link
Collaborator Author

Yeah. I feel like changing the URL is going to be too much of a "breaking change".

Sorry about that.
Please enjoy the new docs (which don't have a SW).

@adrianthedev
Copy link
Collaborator Author

Posted about it on our Discord server too.
We'll mention it in our next newsletter too.

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

Successfully merging this pull request may close these issues.

2 participants