Skip to content

refactor: separate polymorphic functions #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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