Skip to content

Commit

Permalink
refactor: separate polymorphic functions (#1433)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bf4 and lgebhardt committed Apr 18, 2024
1 parent 000e560 commit 4d56ce4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/jsonapi-resources.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'jsonapi/resources/railtie'
require 'jsonapi/utils/polymorphic_types_lookup'
require 'jsonapi/naive_cache'
require 'jsonapi/compiled_json'
require 'jsonapi/relation_retrieval'
Expand Down
12 changes: 1 addition & 11 deletions lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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::PolymorphicTypesLookup.polymorphic_types(name)
end

def resource_types
Expand Down
12 changes: 1 addition & 11 deletions lib/jsonapi/resource_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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::Utils.polymorphic_types(_polymorphic_name.to_sym)
end

def _polymorphic_resource_klasses
Expand Down
30 changes: 30 additions & 0 deletions lib/jsonapi/utils/polymorphic_types_lookup.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4d56ce4

Please sign in to comment.