-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: search association field by type and id
- Loading branch information
Showing
5 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Avo | ||
module Concerns | ||
module FindAssociationField | ||
# The supported association types are defined in the ASSOCIATIONS constant. | ||
unless defined?(ASSOCIATIONS) | ||
ASSOCIATIONS = ["belongs_to", "has_one", "has_many", "has_and_belongs_to_many"] | ||
end | ||
|
||
# This method is used to find an association field for a given resource. | ||
# Ideally, the exact type of the association should be known in advance. | ||
# However, there are cases where the type is unknown | ||
# and the method will return the first matching association field | ||
# based on the provided association name. | ||
def find_association_field(resource:, association:) | ||
resource.get_field_definitions.find do |field| | ||
(field.id == association.to_sym) && field.type.in?(ASSOCIATIONS) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters