Skip to content

Commit 23cbc50

Browse files
committed
Fix overriding polymorphic types on a relationship
1 parent 1c313e9 commit 23cbc50

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/jsonapi/active_relation_retrieval.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def find_fragments(filters, options = {})
262262
def find_related_fragments(source_fragment, relationship, options = {})
263263
if relationship.polymorphic? # && relationship.foreign_key_on == :self
264264
source_resource_klasses = if relationship.foreign_key_on == :self
265-
relationship.class.polymorphic_types(relationship.name).collect do |polymorphic_type|
265+
relationship.polymorphic_types.collect do |polymorphic_type|
266266
resource_klass_for(polymorphic_type)
267267
end
268268
else
@@ -284,7 +284,7 @@ def find_related_fragments(source_fragment, relationship, options = {})
284284
def find_included_fragments(source_fragments, relationship, options)
285285
if relationship.polymorphic? # && relationship.foreign_key_on == :self
286286
source_resource_klasses = if relationship.foreign_key_on == :self
287-
relationship.class.polymorphic_types(relationship.name).collect do |polymorphic_type|
287+
relationship.polymorphic_types.collect do |polymorphic_type|
288288
resource_klass_for(polymorphic_type)
289289
end
290290
else

lib/jsonapi/relationship.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ def initialize(name, options = {})
1919
@parent_resource = options[:parent_resource]
2020
@relation_name = options[:relation_name]
2121
@polymorphic = options.fetch(:polymorphic, false) == true
22-
@polymorphic_types = options[:polymorphic_types]
22+
@polymorphic_types_override = options[:polymorphic_types]
23+
2324
if options[:polymorphic_relations]
2425
JSONAPI.configuration.deprecate('Use polymorphic_types instead of polymorphic_relations')
25-
@polymorphic_types ||= options[:polymorphic_relations]
26+
@polymorphic_types_override ||= options[:polymorphic_relations]
2627
end
2728

2829
use_related_resource_records_for_joins_default = if options[:relation_name]
@@ -86,16 +87,27 @@ def inverse_relationship
8687
@inverse_relationship
8788
end
8889

89-
def self.polymorphic_types(name)
90-
::JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types(name)
90+
def polymorphic_types
91+
return @polymorphic_types if @polymorphic_types
92+
93+
types = @polymorphic_types_override
94+
types ||= ::JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types(_relation_name)
95+
96+
@polymorphic_types = types&.map { |t| t.to_s.pluralize } || []
97+
98+
if @polymorphic_types.blank?
99+
warn "[POLYMORPHIC TYPE] No polymorphic types set or found for #{parent_resource.name} #{_relation_name}"
100+
end
101+
102+
@polymorphic_types
91103
end
92104

93105
def resource_types
94-
if polymorphic? && belongs_to?
95-
@polymorphic_types ||= self.class.polymorphic_types(_relation_name).collect { |t| t.pluralize }
96-
else
97-
[resource_klass._type.to_s.pluralize]
98-
end
106+
@resource_types ||= if polymorphic?
107+
polymorphic_types
108+
else
109+
[resource_klass._type.to_s.pluralize]
110+
end
99111
end
100112

101113
def type
@@ -191,11 +203,7 @@ def polymorphic_type
191203
end
192204

193205
def setup_implicit_relationships_for_polymorphic_types(exclude_linkage_data: true)
194-
types = self.class.polymorphic_types(_relation_name)
195-
unless types.present?
196-
warn "[POLYMORPHIC TYPE] No polymorphic types found for #{parent_resource.name} #{_relation_name}"
197-
return
198-
end
206+
types = polymorphic_types
199207

200208
types.each do |type|
201209
parent_resource.has_one(type.to_s.underscore.singularize,

0 commit comments

Comments
 (0)