File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
33require 'jsonapi/resources/railtie'
4+ require 'jsonapi/utils/polymorphic_types_lookup'
45require 'jsonapi/naive_cache'
56require 'jsonapi/compiled_json'
67require 'jsonapi/relation_retrieval'
Original file line number Diff line number Diff line change @@ -87,17 +87,7 @@ def inverse_relationship
8787 end
8888
8989 def self . polymorphic_types ( name )
90- @poly_hash ||= { } . tap do |hash |
91- ObjectSpace . each_object do |klass |
92- next unless Module === klass
93- if ActiveRecord ::Base > klass
94- klass . reflect_on_all_associations ( :has_many ) . select { |r | r . options [ :as ] } . each do |reflection |
95- ( hash [ reflection . options [ :as ] ] ||= [ ] ) << klass . name . underscore
96- end
97- end
98- end
99- end
100- @poly_hash [ name . to_sym ]
90+ ::JSONAPI ::Utils ::PolymorphicTypesLookup . polymorphic_types ( name )
10191 end
10292
10393 def resource_types
Original file line number Diff line number Diff line change @@ -1020,17 +1020,7 @@ def polymorphic(polymorphic = true)
10201020 end
10211021
10221022 def _polymorphic_types
1023- @poly_hash ||= { } . tap do |hash |
1024- ObjectSpace . each_object do |klass |
1025- next unless Module === klass
1026- if klass < ActiveRecord ::Base
1027- klass . reflect_on_all_associations ( :has_many ) . select { |r | r . options [ :as ] } . each do |reflection |
1028- ( hash [ reflection . options [ :as ] ] ||= [ ] ) << klass . name . underscore
1029- end
1030- end
1031- end
1032- end
1033- @poly_hash [ _polymorphic_name . to_sym ]
1023+ JSONAPI ::Utils . polymorphic_types ( _polymorphic_name . to_sym )
10341024 end
10351025
10361026 def _polymorphic_resource_klasses
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module JSONAPI
4+ module Utils
5+ module PolymorphicTypesLookup
6+ extend self
7+
8+ def polymorphic_types ( name )
9+ polymorphic_types_lookup [ name . to_sym ]
10+ end
11+
12+ def polymorphic_types_lookup
13+ @polymorphic_types_lookup ||= build_polymorphic_types_lookup
14+ end
15+
16+ def build_polymorphic_types_lookup
17+ { } . tap do |hash |
18+ ObjectSpace . each_object do |klass |
19+ next unless Module === klass
20+ if ActiveRecord ::Base > klass
21+ klass . reflect_on_all_associations ( :has_many ) . select { |r | r . options [ :as ] } . each do |reflection |
22+ ( hash [ reflection . options [ :as ] ] ||= [ ] ) << klass . name . underscore
23+ end
24+ end
25+ end
26+ end
27+ end
28+ end
29+ end
30+ end
You can’t perform that action at this time.
0 commit comments