Skip to content

Commit f52ade2

Browse files
committed
Merge pull request #1255 from bf4/serialize_inheritable_attributes
Make more class attributes inheritable
2 parents 49eb531 + 0f50847 commit f52ade2

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lib/active_model/serializer.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class Serializer
2727
)
2828
/x
2929

30-
class << self
31-
attr_accessor :_attributes
32-
attr_accessor :_attributes_keys
33-
end
34-
3530
with_options instance_writer: false, instance_reader: false do |serializer|
31+
class_attribute :_type, instance_reader: true
32+
class_attribute :_attributes
33+
self._attributes ||= []
34+
class_attribute :_attributes_keys
35+
self._attributes_keys ||= {}
3636
serializer.class_attribute :_cache
3737
serializer.class_attribute :_fragmented
3838
serializer.class_attribute :_cache_key
@@ -43,8 +43,8 @@ class << self
4343
end
4444

4545
def self.inherited(base)
46-
base._attributes = _attributes.try(:dup) || []
47-
base._attributes_keys = _attributes_keys.try(:dup) || {}
46+
base._attributes = _attributes.dup
47+
base._attributes_keys = _attributes_keys.dup
4848
base._cache_digest = digest_caller_file(caller.first)
4949
super
5050
end
@@ -125,7 +125,6 @@ def self.get_serializer_for(klass)
125125
end
126126

127127
attr_accessor :object, :root, :scope
128-
class_attribute :_type, instance_writer: false
129128

130129
def initialize(object, options = {})
131130
self.object = object
@@ -149,10 +148,10 @@ def attributes
149148
attributes = self.class._attributes.dup
150149

151150
attributes.each_with_object({}) do |name, hash|
152-
unless self.class._fragmented
153-
hash[name] = send(name)
154-
else
151+
if self.class._fragmented
155152
hash[name] = self.class._fragmented.public_send(name)
153+
else
154+
hash[name] = send(name)
156155
end
157156
end
158157
end

0 commit comments

Comments
 (0)