avo icon indicating copy to clipboard operation
avo copied to clipboard

feature: Always use index_query when loading a resource

Open sriddbs opened this issue 1 year ago • 8 comments

Description

Avo::AssociationsController will now always consider index_query to load a resource

Fixes #2445

Checklist:

  • [x] 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

sriddbs avatar Feb 17 '24 10:02 sriddbs

Code Climate has analyzed commit 6bc4a47e and detected 0 issues on this pull request.

View more on Code Climate.

codeclimate[bot] avatar Feb 17 '24 10:02 codeclimate[bot]

hey @Paul-Bob let me know if this is all what is required 😄 can you also point me where to add the specs for this please?

sriddbs avatar Feb 17 '24 10:02 sriddbs

Thank you @Paul-Bob for the detailed explanation 👍

How to distinguish on the index_query block where we're on an association (with the current implementation i guess we can do @parent.present?)

am making the changes in app/controllers/avo/associations_controller.rb, so do we still need the @parent.present? check?

The example can be added anywhere that you think it fits on spec/dummy app.

yes, I added the index_query on a resource which is also loaded via association and the changes I made seem working but as you suggested I will add the specs as well 👍

sriddbs avatar Feb 21 '24 06:02 sriddbs

I'm going to let you guys jam on this. I have a couple of questions.

  1. Is this going to be a breaking change for some existing users?
  2. Do we really want to apply the index_query to associations too? Why?

adrianthedev avatar Feb 21 '24 06:02 adrianthedev

  1. Is this going to be a breaking change for some existing users?

Code will not break, the self.index_query will start to apply when rendering a resource through a has_* association field so it may affect the records shown. In order to keep 0 changes we can add the association_query option like:

self.index_query = -> {
  query.order(position: :asc)
}

self.association_query = -> {
  query.order(position: :asc)
}

Here you can even reuse code:

def order_query
  -> {
    query.order(position: :asc)
  }
end

self.index_query = order_query
self.association_query = order_query
  1. Do we really want to apply the index_query to associations too? Why?

There is a linked issue on the PR description with more details and with more linked discussions. Has fields are basically scoped index views of a resource.

Lets take an example where we want to order the users always ascending by position on Avo::Resources::User everywhere on the app:

self.index_query = -> {
  query.order(position: :asc)
}

Now when visiting the users page the order is respected but when have something like:

field :users, as: :has_many

The order will not getting applied. Sure there is the possibility to field :users, as: :has_many, scope: -> { query.order(position: :asc) } but you'll need to apply this scope on all your users fields and I'm giving here a simple ordering example, it easily get messy when you have a multiple lines logic and need to change one little detail.

Paul-Bob avatar Feb 21 '24 10:02 Paul-Bob

am making the changes in app/controllers/avo/associations_controller.rb, so do we still need the @parent.present? check?

@sriddbs I mean on the Proc, we should be able to separate the query when it's executed as association:

self.index_query = -> {
  if on_association? # fictive method I think we can do @parent.present?
    query.do_this...
  else
    query.do_that...
  end
}

Makes sense? There are cases when we want to apply different logic to the query when is loaded as association.

Paul-Bob avatar Feb 21 '24 10:02 Paul-Bob

I mean on the Proc, we should be able to separate the query when it's executed as association

Ahh got it 👍

sriddbs avatar Feb 22 '24 10:02 sriddbs

@sriddbs what is the use-case on your end? What would you like to apply from index_query to the association?

adrianthedev avatar Feb 23 '24 07:02 adrianthedev

This PR has been marked as stale because there was no activity for the past 15 days.

github-actions[bot] avatar Mar 10 '24 01:03 github-actions[bot]

This PR has been marked as stale because there was no activity for the past 15 days.

github-actions[bot] avatar Mar 28 '24 01:03 github-actions[bot]

Closing for now as we haven't gotten the use-case for it. Please feel free to reopen.

adrianthedev avatar Apr 01 '24 12:04 adrianthedev

Linked discussions:

  • https://github.com/avo-hq/avo/issues/2443
  • https://discord.com/channels/740892036978442260/1217489935007879239/1217512469170491412

Workaround: use scope, if it repeats all over the code create a helper for it

Paul-Bob avatar Apr 01 '24 14:04 Paul-Bob