Skip to content

Commit

Permalink
Fix overriding polymorphic types on a relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
lgebhardt committed Jan 26, 2024
1 parent 1c313e9 commit 23cbc50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/jsonapi/active_relation_retrieval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def find_fragments(filters, options = {})
def find_related_fragments(source_fragment, relationship, options = {})
if relationship.polymorphic? # && relationship.foreign_key_on == :self
source_resource_klasses = if relationship.foreign_key_on == :self
relationship.class.polymorphic_types(relationship.name).collect do |polymorphic_type|
relationship.polymorphic_types.collect do |polymorphic_type|
resource_klass_for(polymorphic_type)
end
else
Expand All @@ -284,7 +284,7 @@ def find_related_fragments(source_fragment, relationship, options = {})
def find_included_fragments(source_fragments, relationship, options)
if relationship.polymorphic? # && relationship.foreign_key_on == :self
source_resource_klasses = if relationship.foreign_key_on == :self
relationship.class.polymorphic_types(relationship.name).collect do |polymorphic_type|
relationship.polymorphic_types.collect do |polymorphic_type|
resource_klass_for(polymorphic_type)
end
else
Expand Down
36 changes: 22 additions & 14 deletions lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ def initialize(name, options = {})
@parent_resource = options[:parent_resource]
@relation_name = options[:relation_name]
@polymorphic = options.fetch(:polymorphic, false) == true
@polymorphic_types = options[:polymorphic_types]
@polymorphic_types_override = options[:polymorphic_types]

if options[:polymorphic_relations]
JSONAPI.configuration.deprecate('Use polymorphic_types instead of polymorphic_relations')
@polymorphic_types ||= options[:polymorphic_relations]
@polymorphic_types_override ||= options[:polymorphic_relations]
end

use_related_resource_records_for_joins_default = if options[:relation_name]
Expand Down Expand Up @@ -86,16 +87,27 @@ def inverse_relationship
@inverse_relationship
end

def self.polymorphic_types(name)
::JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types(name)
def polymorphic_types
return @polymorphic_types if @polymorphic_types

types = @polymorphic_types_override
types ||= ::JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types(_relation_name)

@polymorphic_types = types&.map { |t| t.to_s.pluralize } || []

if @polymorphic_types.blank?
warn "[POLYMORPHIC TYPE] No polymorphic types set or found for #{parent_resource.name} #{_relation_name}"
end

@polymorphic_types
end

def resource_types
if polymorphic? && belongs_to?
@polymorphic_types ||= self.class.polymorphic_types(_relation_name).collect { |t| t.pluralize }
else
[resource_klass._type.to_s.pluralize]
end
@resource_types ||= if polymorphic?
polymorphic_types
else
[resource_klass._type.to_s.pluralize]
end
end

def type
Expand Down Expand Up @@ -191,11 +203,7 @@ def polymorphic_type
end

def setup_implicit_relationships_for_polymorphic_types(exclude_linkage_data: true)
types = self.class.polymorphic_types(_relation_name)
unless types.present?
warn "[POLYMORPHIC TYPE] No polymorphic types found for #{parent_resource.name} #{_relation_name}"
return
end
types = polymorphic_types

types.each do |type|
parent_resource.has_one(type.to_s.underscore.singularize,
Expand Down

0 comments on commit 23cbc50

Please sign in to comment.