Skip to content

Commit ca92139

Browse files
committed
Make ActiveModel::Serialization#read_attribute_for_serialization public
Despite the `#read_attribute_for_serialization` method being declared as `private`, the module-level `ActiveModel::Serialization` documentation mentions it explicitly by name. Mentioning the method directly and encouraging classes to override it to customize behavior breaks Rails existing `public`-`private` interface convention. This commit promotes the method to `public` visibility.
1 parent 3997ed6 commit ca92139

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

activemodel/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Make `ActiveModel::Serialization#read_attribute_for_serialization` public
2+
3+
*Sean Doyle*
4+
15
* Add a default token generator for password reset tokens when using `has_secure_password`.
26

37
```ruby

activemodel/lib/active_model/serialization.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module ActiveModel
2929
# An +attributes+ hash must be defined and should contain any attributes you
3030
# need to be serialized. Attributes must be strings, not symbols.
3131
# When called, serializable hash will use instance methods that match the name
32-
# of the attributes hash's keys. In order to override this behavior, take a look
33-
# at the private method +read_attribute_for_serialization+.
32+
# of the attributes hash's keys. In order to override this behavior, override
33+
# the +read_attribute_for_serialization+ method.
3434
#
3535
# ActiveModel::Serializers::JSON module automatically includes
3636
# the +ActiveModel::Serialization+ module, so there is no need to
@@ -148,29 +148,29 @@ def serializable_hash(options = nil)
148148
hash
149149
end
150150

151+
# Hook method defining how an attribute value should be retrieved for
152+
# serialization. By default this is assumed to be an instance named after
153+
# the attribute. Override this method in subclasses should you need to
154+
# retrieve the value for a given attribute differently:
155+
#
156+
# class MyClass
157+
# include ActiveModel::Serialization
158+
#
159+
# def initialize(data = {})
160+
# @data = data
161+
# end
162+
#
163+
# def read_attribute_for_serialization(key)
164+
# @data[key]
165+
# end
166+
# end
167+
alias :read_attribute_for_serialization :send
168+
151169
private
152170
def attribute_names_for_serialization
153171
attributes.keys
154172
end
155173

156-
# Hook method defining how an attribute value should be retrieved for
157-
# serialization. By default this is assumed to be an instance named after
158-
# the attribute. Override this method in subclasses should you need to
159-
# retrieve the value for a given attribute differently:
160-
#
161-
# class MyClass
162-
# include ActiveModel::Serialization
163-
#
164-
# def initialize(data = {})
165-
# @data = data
166-
# end
167-
#
168-
# def read_attribute_for_serialization(key)
169-
# @data[key]
170-
# end
171-
# end
172-
alias :read_attribute_for_serialization :send
173-
174174
def serializable_attributes(attribute_names)
175175
attribute_names.index_with { |n| read_attribute_for_serialization(n) }
176176
end

0 commit comments

Comments
 (0)