Skip to content

Commit cd736e0

Browse files
committed
Memoize attributes
1 parent 036604b commit cd736e0

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/active_model/serializer/attributes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ def initialize(name, block)
4040

4141
# Return the +attributes+ of +object+ as presented
4242
# by the serializer.
43-
def attributes(requested_attrs = nil)
44-
self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
43+
def attributes(requested_attrs = nil, reload = false)
44+
@attributes = nil if reload
45+
@attributes ||= self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
4546
next unless requested_attrs.nil? || requested_attrs.include?(key)
4647
hash[key] = attribute_mapping.call(self)
4748
end
@@ -65,7 +66,6 @@ def attributes(*attrs)
6566
end
6667
end
6768

68-
# TODO: remove the dynamic method definition
6969
# @example
7070
# class AdminAuthorSerializer < ActiveModel::Serializer
7171
# attributes :id, :recent_edits

0 commit comments

Comments
 (0)