File tree Expand file tree Collapse file tree 4 files changed +33
-22
lines changed Expand file tree Collapse file tree 4 files changed +33
-22
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require 'jsonapi/resources/railtie'
4
+ require 'jsonapi/utils/polymorphic_types_lookup'
4
5
require 'jsonapi/naive_cache'
5
6
require 'jsonapi/compiled_json'
6
7
require 'jsonapi/relation_retrieval'
Original file line number Diff line number Diff line change @@ -87,17 +87,7 @@ def inverse_relationship
87
87
end
88
88
89
89
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 )
101
91
end
102
92
103
93
def resource_types
Original file line number Diff line number Diff line change @@ -1020,17 +1020,7 @@ def polymorphic(polymorphic = true)
1020
1020
end
1021
1021
1022
1022
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 )
1034
1024
end
1035
1025
1036
1026
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