Skip to content

Commit f75acdb

Browse files
authored
Polymorphic types override per relationship (#1440)
* Add warning about disabling eager loading * Fix overriding polymorphic types on a relationship
1 parent c529c23 commit f75acdb

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
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/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Configuration
1313
:warn_on_route_setup_issues,
1414
:warn_on_missing_routes,
1515
:warn_on_performance_issues,
16+
:warn_on_eager_loading_disabled,
1617
:default_allow_include_to_one,
1718
:default_allow_include_to_many,
1819
:allow_sort,
@@ -67,6 +68,7 @@ def initialize
6768
self.warn_on_route_setup_issues = true
6869
self.warn_on_missing_routes = true
6970
self.warn_on_performance_issues = true
71+
self.warn_on_eager_loading_disabled = true
7072

7173
# :none, :offset, :paged, or a custom paginator name
7274
self.default_paginator = :none
@@ -326,6 +328,8 @@ def allow_include=(allow_include)
326328

327329
attr_writer :warn_on_performance_issues
328330

331+
attr_writer :warn_on_eager_loading_disabled
332+
329333
attr_writer :use_relationship_reflection
330334

331335
attr_writer :resource_cache

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,

lib/jsonapi/resources/railtie.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ class Railtie < ::Rails::Railtie
1919
}
2020
end
2121

22-
initializer "jsonapi_resources.initialize", after: :initialize do
23-
JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types_lookup_clear!
22+
config.before_initialize do
23+
if !Rails.application.config.eager_load && ::JSONAPI::configuration.warn_on_eager_loading_disabled
24+
warn 'WARNING: jsonapi-resources may not load polymorphic types when Rails `eager_load` is disabled. ' \
25+
'Polymorphic types may be set per relationship . This warning may be disable in the configuration ' \
26+
'by setting `warn_on_eager_loading_disabled` to false.'
27+
end
2428
end
2529
end
2630
end

0 commit comments

Comments
 (0)