From 2be3e27f674650fd90e4e1f9ce3091f97cd8e0c8 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 31 Dec 2023 17:07:07 +0530 Subject: [PATCH 1/4] refactor: lookup polymorphic types only once --- lib/jsonapi/resource_common.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/jsonapi/resource_common.rb b/lib/jsonapi/resource_common.rb index c4731df1..001de721 100644 --- a/lib/jsonapi/resource_common.rb +++ b/lib/jsonapi/resource_common.rb @@ -1020,17 +1020,7 @@ def polymorphic(polymorphic = true) end def _polymorphic_types - @poly_hash ||= {}.tap do |hash| - ObjectSpace.each_object do |klass| - next unless Module === klass - if klass < ActiveRecord::Base - klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection| - (hash[reflection.options[:as]] ||= []) << klass.name.underscore - end - end - end - end - @poly_hash[_polymorphic_name.to_sym] + JSONAPI::Relationship.polymorphic_types(_polymorphic_name.to_sym) end def _polymorphic_resource_klasses From 9142d4d131ca47af4e125e03c836031330299974 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 18 Jan 2024 17:28:58 -0600 Subject: [PATCH 2/4] refactor: polymorphic lookups to utility module --- lib/jsonapi-resources.rb | 1 + lib/jsonapi/relationship.rb | 12 +----------- lib/jsonapi/resource_common.rb | 2 +- lib/jsonapi/utils.rb | 21 +++++++++++++++++++++ 4 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 lib/jsonapi/utils.rb diff --git a/lib/jsonapi-resources.rb b/lib/jsonapi-resources.rb index 04d7908f..7daaaad1 100644 --- a/lib/jsonapi-resources.rb +++ b/lib/jsonapi-resources.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'jsonapi/resources/railtie' +require 'jsonapi/utils' require 'jsonapi/naive_cache' require 'jsonapi/compiled_json' require 'jsonapi/relation_retrieval' diff --git a/lib/jsonapi/relationship.rb b/lib/jsonapi/relationship.rb index 31a053b2..c53ae1a2 100644 --- a/lib/jsonapi/relationship.rb +++ b/lib/jsonapi/relationship.rb @@ -87,17 +87,7 @@ def inverse_relationship end def self.polymorphic_types(name) - @poly_hash ||= {}.tap do |hash| - ObjectSpace.each_object do |klass| - next unless Module === klass - if ActiveRecord::Base > klass - klass.reflect_on_all_associations(:has_many).select { |r| r.options[:as] }.each do |reflection| - (hash[reflection.options[:as]] ||= []) << klass.name.underscore - end - end - end - end - @poly_hash[name.to_sym] + ::JSONAPI::Utils.polymorphic_types(name) end def resource_types diff --git a/lib/jsonapi/resource_common.rb b/lib/jsonapi/resource_common.rb index 001de721..85c81361 100644 --- a/lib/jsonapi/resource_common.rb +++ b/lib/jsonapi/resource_common.rb @@ -1020,7 +1020,7 @@ def polymorphic(polymorphic = true) end def _polymorphic_types - JSONAPI::Relationship.polymorphic_types(_polymorphic_name.to_sym) + JSONAPI::Utils.polymorphic_types(_polymorphic_name.to_sym) end def _polymorphic_resource_klasses diff --git a/lib/jsonapi/utils.rb b/lib/jsonapi/utils.rb new file mode 100644 index 00000000..00c01469 --- /dev/null +++ b/lib/jsonapi/utils.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module JSONAPI + module Utils + extend self + + def polymorphic_types(name) + @poly_hash ||= {}.tap do |hash| + ObjectSpace.each_object do |klass| + next unless Module === klass + if ActiveRecord::Base > klass + klass.reflect_on_all_associations(:has_many).select { |r| r.options[:as] }.each do |reflection| + (hash[reflection.options[:as]] ||= []) << klass.name.underscore + end + end + end + end + @poly_hash[name.to_sym] + end + end +end From a676c71f699b55929b48647f47242a19af422ac7 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 18 Jan 2024 17:30:47 -0600 Subject: [PATCH 3/4] refactor: separate polymorphic functions --- lib/jsonapi/utils.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/jsonapi/utils.rb b/lib/jsonapi/utils.rb index 00c01469..d6da0aec 100644 --- a/lib/jsonapi/utils.rb +++ b/lib/jsonapi/utils.rb @@ -5,7 +5,15 @@ module Utils extend self def polymorphic_types(name) - @poly_hash ||= {}.tap do |hash| + polymorphic_types_lookup[name.to_sym] + end + + def polymorphic_types_lookup + @polymorphic_types_lookup ||= build_polymorphic_types_lookup + end + + def build_polymorphic_types_lookup + {}.tap do |hash| ObjectSpace.each_object do |klass| next unless Module === klass if ActiveRecord::Base > klass @@ -15,7 +23,6 @@ def polymorphic_types(name) end end end - @poly_hash[name.to_sym] end end end From bd8c47ffaf6b3ad779e53dcc1e041ba661377a27 Mon Sep 17 00:00:00 2001 From: lgebhardt Date: Fri, 19 Jan 2024 14:55:06 -0500 Subject: [PATCH 4/4] Refactor into PolymorphicTypesLookup --- lib/jsonapi-resources.rb | 2 +- lib/jsonapi/relationship.rb | 2 +- lib/jsonapi/utils.rb | 28 ----------------- lib/jsonapi/utils/polymorphic_types_lookup.rb | 30 +++++++++++++++++++ 4 files changed, 32 insertions(+), 30 deletions(-) delete mode 100644 lib/jsonapi/utils.rb create mode 100644 lib/jsonapi/utils/polymorphic_types_lookup.rb diff --git a/lib/jsonapi-resources.rb b/lib/jsonapi-resources.rb index 7daaaad1..e26af1aa 100644 --- a/lib/jsonapi-resources.rb +++ b/lib/jsonapi-resources.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'jsonapi/resources/railtie' -require 'jsonapi/utils' +require 'jsonapi/utils/polymorphic_types_lookup' require 'jsonapi/naive_cache' require 'jsonapi/compiled_json' require 'jsonapi/relation_retrieval' diff --git a/lib/jsonapi/relationship.rb b/lib/jsonapi/relationship.rb index c53ae1a2..da706523 100644 --- a/lib/jsonapi/relationship.rb +++ b/lib/jsonapi/relationship.rb @@ -87,7 +87,7 @@ def inverse_relationship end def self.polymorphic_types(name) - ::JSONAPI::Utils.polymorphic_types(name) + ::JSONAPI::Utils::PolymorphicTypesLookup.polymorphic_types(name) end def resource_types diff --git a/lib/jsonapi/utils.rb b/lib/jsonapi/utils.rb deleted file mode 100644 index d6da0aec..00000000 --- a/lib/jsonapi/utils.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module JSONAPI - module Utils - extend self - - def polymorphic_types(name) - polymorphic_types_lookup[name.to_sym] - end - - def polymorphic_types_lookup - @polymorphic_types_lookup ||= build_polymorphic_types_lookup - end - - def build_polymorphic_types_lookup - {}.tap do |hash| - ObjectSpace.each_object do |klass| - next unless Module === klass - if ActiveRecord::Base > klass - klass.reflect_on_all_associations(:has_many).select { |r| r.options[:as] }.each do |reflection| - (hash[reflection.options[:as]] ||= []) << klass.name.underscore - end - end - end - end - end - end -end diff --git a/lib/jsonapi/utils/polymorphic_types_lookup.rb b/lib/jsonapi/utils/polymorphic_types_lookup.rb new file mode 100644 index 00000000..2a72673e --- /dev/null +++ b/lib/jsonapi/utils/polymorphic_types_lookup.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module JSONAPI + module Utils + module PolymorphicTypesLookup + extend self + + def polymorphic_types(name) + polymorphic_types_lookup[name.to_sym] + end + + def polymorphic_types_lookup + @polymorphic_types_lookup ||= build_polymorphic_types_lookup + end + + def build_polymorphic_types_lookup + {}.tap do |hash| + ObjectSpace.each_object do |klass| + next unless Module === klass + if ActiveRecord::Base > klass + klass.reflect_on_all_associations(:has_many).select { |r| r.options[:as] }.each do |reflection| + (hash[reflection.options[:as]] ||= []) << klass.name.underscore + end + end + end + end + end + end + end +end