Skip to content
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

[0.9 stable] Lazily compute possible serializer class names #2467

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I try to avoid return in a block. Break would work, too, no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break could work yes. I find return from a block quite idomatic, but feel free to change the style if you feel strongly about it.

The goal here was first and foremost to reduce the overhead.

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