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

Fix belongs_to and ID field linking to child resources if configured #3109

Merged

Conversation

stevegeek
Copy link
Contributor

@stevegeek stevegeek commented Aug 8, 2024

Description

If a resource includes a belongs_to field which relates to a record which uses STI, then the current behaviour is that the link rendered on the show page for the resource to the associated record always links to the parent class resource. In other places this behaviour is controlled by link_to_child_resource but not here.

Related, if id_links_to_resource is used and link_to_child_resource is set for an association, the link does not respect the link_to_child_resource setting.

This PR updates the belongs_to field to respect the link_to_child_resource option, which can either be set on the Resource, or set on the field.

It also changes the way BaseComponent determines if a field should link_to_child_resource by using the association reflection and checking the related association.

I believe this fixes #2629 and fixes #2782

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

Screenshots & recording

I have checked this in an application using Avo but can't share screenshots from it.

Manual review steps

  1. Step 1
  2. Step 2

Manual reviewer: please leave a comment with output from the test if that's the case.

Copy link

codeclimate bot commented Aug 8, 2024

Code Climate has analyzed commit 93c1117 and detected 0 issues on this pull request.

View more on Code Climate.

@stevegeek
Copy link
Contributor Author

@Paul-Bob @adrianthedev , if there is anything I can do to help you guys with reviewing this PR, do let me know

@Paul-Bob Paul-Bob self-requested a review August 14, 2024 15:53
@Paul-Bob
Copy link
Contributor

I'm reviewing it, thank you @stevegeek

@Paul-Bob
Copy link
Contributor

I believe this fixes #2629 and #2782

#2629 is already fixed, I verified it here https://main.avodemo.com/avo/resources/people

Is indeed fixing #2782, confirmed on the reproduction repo provided by @cyu

Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

I added a comment to cover the for_attribute scenario, otherwise, it's looking great @stevegeek! Thank you!


@resource.link_to_child_resource
end

def field_linked_to_child_resource?
field.present? && field.respond_to?(:link_to_child_resource) && field.link_to_child_resource
resolved_field = @reflection ? @parent_resource.get_field(@reflection.name) : field
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this will not find the field when using for_attribute option. For example:

field :the_children, as: :has_many, for_attribute: :children, link_to_child_resource: true

This should cover that scenario, can you please verify that?

Suggested change
resolved_field = @reflection ? @parent_resource.get_field(@reflection.name) : field
resolved_field = @reflection ? @parent_resource.get_field(params[:for_attribute] || @reflection.name) : field

@@ -19,6 +27,36 @@
end
end

context "Resource show page links to associated resources" do
let(:paul) { create :sibling, name: "paul" }
Copy link
Contributor

Choose a reason for hiding this comment

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

Great name! :D

@adrianthedev
Copy link
Collaborator

Thanks @stevegeek.
FYI, we have a major Holiday today and we'll probably be back at it on Monday.

@Paul-Bob Paul-Bob merged commit 78937ff into avo-hq:main Aug 16, 2024
22 checks passed
@Paul-Bob Paul-Bob added the Fix label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants