Skip to content

Commit

Permalink
Merge pull request #2467 from Shopify/0-9-stable-lazy-serializer-list
Browse files Browse the repository at this point in the history
[0.9 stable] Lazily compute possible serializer class names
  • Loading branch information
byroot committed Apr 10, 2024
2 parents 9f7222c + 4ed12fd commit 14f465c
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 14f465c

Please sign in to comment.