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

refactor: reflect_on_association instead _reflections #3069

Merged
merged 6 commits into from
Jul 30, 2024

Conversation

Paul-Bob
Copy link
Contributor

@Paul-Bob Paul-Bob commented Jul 30, 2024

Description

The private _reflections API was used to access association reflections in the codebase. This approach relied on internal Rails APIs, which are prone to changes and could lead to compatibility issues in future Rails versions.

This PR replaces the use of the private _reflections API with the public reflect_on_association method. This method is part of the official ActiveRecord::Reflection::ClassMethods and provides a more robust and future-proof way to access association reflections.

@Paul-Bob Paul-Bob marked this pull request as draft July 30, 2024 07:21
@github-actions github-actions bot added the Chore label Jul 30, 2024
Copy link

codeclimate bot commented Jul 30, 2024

Code Climate has analyzed commit 72bfc37 and detected 0 issues on this pull request.

View more on Code Climate.

@Paul-Bob Paul-Bob self-assigned this Jul 30, 2024
@Paul-Bob Paul-Bob marked this pull request as ready for review July 30, 2024 08:06
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.

Looks good! Thanks for the refactor!

@@ -188,5 +188,9 @@ def join_record
reflection.through_reflection.klass.find_by(source_foreign_key => @attachment_record.id,
through_foreign_key => @record.id)
end

def has_many_reflection?
reflection_class.in? ["HasManyReflection", "HasAndBelongsToManyReflection"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed that above some mentions are classes and these are strings.
Is there anything we can do to make them the same or do we have to know when to call as a string and as a class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. There is no reason to use class.name.demodulize.to_s and check against a string.

@adrianthedev
Copy link
Collaborator

Also, let's link the Avo 2 PR as well here for reference.
We're still going to wait for them to make a decision if they make the jump to Avo 3 (preffered).

@@ -188,5 +188,9 @@ def join_record
reflection.through_reflection.klass.find_by(source_foreign_key => @attachment_record.id,
through_foreign_key => @record.id)
end

def has_many_reflection?
Copy link
Contributor

Choose a reason for hiding this comment

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

Noice extracting this method! LGTM!

@Paul-Bob Paul-Bob added Refactor and removed Chore labels Jul 30, 2024
@Paul-Bob Paul-Bob changed the title chore: reflect_on_association instead _reflections refactor: reflect_on_association instead _reflections Jul 30, 2024
@Paul-Bob Paul-Bob merged commit 4fab986 into main Jul 30, 2024
22 checks passed
@Paul-Bob Paul-Bob deleted the chore/reflect_on_associations branch July 30, 2024 09:18
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.

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.

3 participants