Skip to content

Commit 76ef777

Browse files
bf4lgebhardt
andauthored
refactor: separate polymorphic functions (#1433)
* refactor: lookup polymorphic types only once * refactor: polymorphic lookups to utility module * refactor: separate polymorphic functions * Refactor into PolymorphicTypesLookup --------- Co-authored-by: lgebhardt <larry@cerebris.com>
1 parent 9f436b5 commit 76ef777

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

lib/jsonapi-resources.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'jsonapi/resources/railtie'
4+
require 'jsonapi/utils/polymorphic_types_lookup'
45
require 'jsonapi/naive_cache'
56
require 'jsonapi/compiled_json'
67
require 'jsonapi/relation_retrieval'

lib/jsonapi/relationship.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff 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

lib/jsonapi/resource_common.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

0 commit comments

Comments
 (0)