Skip to content

Commit

Permalink
Lazily compute possible serializer class names
Browse files Browse the repository at this point in the history
There is no point computing them all and then later
look them up.
  • Loading branch information
byroot committed Apr 10, 2024
1 parent 6d2f653 commit 4ed12fd
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ def serializer_for(resource, options = {})
ArraySerializer
end
else
search_list = build_serializer_class_list(resource, options)
result = search_list.map do |klass_name|
Serializer.serializers_cache.fetch_or_store(klass_name) do
_const_get(klass_name)
end
end

result.find { |serializer| !serializer.nil? }
each_possible_serializer(resource, options) do |klass_name|
serializer = Serializer.serializers_cache.fetch_or_store(klass_name) do
_const_get(klass_name)
end
return serializer unless serializer.nil?
end
nil
end
end

Expand Down Expand Up @@ -124,11 +123,10 @@ def strip_attribute(attr)
attr
end

def build_serializer_class_list(resource, options)
list = []
list << build_serializer_class(resource, options)
list << build_serializer_class(resource, {})
list << build_serializer_class(resource.class.name.demodulize, {})
def each_possible_serializer(resource, options)
yield build_serializer_class(resource, options)
yield build_serializer_class(resource, {})
yield build_serializer_class(resource.class.name.demodulize, {})
end

def build_serializer_class(resource, options)
Expand Down

0 comments on commit 4ed12fd

Please sign in to comment.