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: use custom resource for has_many field #1109

Conversation

Paul-Bob
Copy link
Contributor

@Paul-Bob Paul-Bob commented Jul 28, 2022

Description

By default, has_many will use the field resource.

For example field :comments, as: :has_many will use CommentResource.

If you want to use a custom resource you need to add the use_resource: YourCustomResource option.

Make sure to have the custom resource prepared to be used by adding self.model_class = ::YourModel

Fixes #1103

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

@codeclimate
Copy link

codeclimate bot commented Jul 28, 2022

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

Here's the issue category breakdown:

Category Count
Complexity 1

View more on Code Climate.

@codecov
Copy link

codecov bot commented Jul 28, 2022

Codecov Report

Merging #1109 (08ac86b) into main (514f221) will decrease coverage by 0.02%.
The diff coverage is 92.15%.

@@            Coverage Diff             @@
##             main    #1109      +/-   ##
==========================================
- Coverage   94.20%   94.18%   -0.03%     
==========================================
  Files         525      530       +5     
  Lines       10465    10560      +95     
==========================================
+ Hits         9859     9946      +87     
- Misses        606      614       +8     
Impacted Files Coverage Δ
spec/dummy/app/avo/filters/photo_filter.rb 55.55% <55.55%> (ø)
spec/dummy/app/avo/actions/delete_comment.rb 60.00% <60.00%> (ø)
spec/dummy/app/policies/post_policy.rb 92.30% <66.66%> (-7.70%) ⬇️
...omponents/avo/index/resource_controls_component.rb 100.00% <100.00%> (ø)
...p/components/avo/views/resource_index_component.rb 94.66% <100.00%> (-1.28%) ⬇️
app/controllers/avo/application_controller.rb 84.79% <100.00%> (+0.17%) ⬆️
app/controllers/avo/associations_controller.rb 94.11% <100.00%> (ø)
lib/avo/fields/base_field.rb 95.48% <100.00%> (+0.02%) ⬆️
lib/avo/fields/has_base_field.rb 93.22% <100.00%> (+1.06%) ⬆️
.../dummy/app/avo/resources/photo_comment_resource.rb 100.00% <100.00%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 514f221...08ac86b. Read the comment docs.

Copy link
Collaborator

@adrianthedev adrianthedev left a comment

Choose a reason for hiding this comment

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

I pushed a few changes:

  • changed the resource field option to use_resource. I did that because in the BaseField class we already have a @resource instance variable and we might get some clashes
  • I changed related_resource in ApplicationController to take into account the use_resource option from the field. That's why you couldn't see the changes from the HasBaseField

Next up:

  • we need to make sure the links in the turbo frame take the users to the proper locations.
    For example, when you click on the Attach comment button it takes you to a page where that new resource is used. Same for the rest of the buttons and links.
  • make sure the page is translatable using the translation tags
  • other misc things like search, attach/detach/create

CleanShot 2022-07-29 at 12 20 09@2x

spec/dummy/config/initializers/avo.rb Outdated Show resolved Hide resolved
spec/dummy/app/avo/resources/test_resource.rb Outdated Show resolved Hide resolved
@adrianthedev
Copy link
Collaborator

Getting back to you with some things that were "broken"

  • the search is not working because it's not using the proper resource. Comment does not have search_query and PhotoComment has it. But it seems to be working on my end (screenrec below)
  • the pagination seems to be working too

Let me know if I'm looking in the wrong place or I'm not reproducing the bugs properly.

CleanShot 2022-07-31 at 20 16 23

CleanShot 2022-07-31 at 20 14 14

Copy link
Collaborator

@adrianthedev adrianthedev left a comment

Choose a reason for hiding this comment

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

Some minor tweaks here and there.

  • docs needed too

@id.to_s.humanize(keep_id_suffix: true)
end

def name_from_use_resource
Copy link
Collaborator

Choose a reason for hiding this comment

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

use_resource is not a BaseField property. Can we create a name method in HasBaseField that checks for use_resource and uses that or falls back to super?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That approach is kinda tricky because we always want to give priority to custom name or to localization... We can have the name method overridden in HasBaseField but there we need to check for custom name and translation too so that would be a duplicated method. Check my last approach for name

lib/avo/fields/has_base_field.rb Outdated Show resolved Hide resolved
lib/avo/fields/has_base_field.rb Show resolved Hide resolved
spec/dummy/app/avo/resources/post_resource.rb Outdated Show resolved Hide resolved
spec/dummy/app/models/comment.rb Outdated Show resolved Hide resolved
@Paul-Bob Paul-Bob marked this pull request as ready for review August 2, 2022 02:14
Copy link
Collaborator

@adrianthedev adrianthedev left a comment

Choose a reason for hiding this comment

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

There's one thing I'd like us not to push with this PR. The name(from_use_resource: nil) override in BaseField.

Also, are all items from this comment checked?

Otherwise, we're good to go! Good job!

Comment on lines 100 to 102
def name
super(from_use_resource: use_resource&.name)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking of a different route where we didn't have to introduce from_use_resource to the BaseField.name method.

Suggested change
def name
super(from_use_resource: use_resource&.name)
end
def name
return use_resource&.name if use_resource.present?
super
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That approach have an issue, if use resource is present the name: "CustomName" and translation will be unconsidered. I don't think we want it, we want the name to have the flowing priority:

  1. Custom name, using name option
  2. Translation keys
  3. Name from use resource if present, otherwise
  4. Name from id

I understand that my first approach and second one are kinda tricky but that was the ways i found to maintain this naming priority to be the same without duplicating the code and the guards

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check the last approach based on what you suggested

@Paul-Bob
Copy link
Contributor Author

Paul-Bob commented Aug 2, 2022

Next up:

* [ ]  we need to make sure the links in the turbo frame take the users to the proper locations.
  For example, when you click on the `Attach comment` button it takes you to a page where that new resource is used. Same for the rest of the buttons and links.

✅ HalfCheked, it keeps the "old" path but actually use the new resource so it's fine

* [ ]  make sure the page is translatable using the translation tags

✅ I tested it very quick and looks fine, can you test this one too please?

* [ ]  other misc things like search, attach/detach/create

✔️ Checked, attach and create are even in the spec

Copy link
Collaborator

@adrianthedev adrianthedev left a comment

Choose a reason for hiding this comment

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

Perfect!

Thank you!

Amazing job!

@adrianthedev adrianthedev merged commit f46babf into avo-hq:main Aug 2, 2022
@adrianthedev adrianthedev added the Enhancement New feature or request label Aug 2, 2022
@adrianthedev adrianthedev changed the title improvement/allow_to_specify_resource_on_has_many_fields feature: use custom resource for has_many field Aug 2, 2022
pkrauze pushed a commit to pkrauze/avo that referenced this pull request Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the resource option to has_many/has_and_bleogns_to_many associations
2 participants