Skip to content

Commit 28c1b5b

Browse files
committed
Document Model delcared attributes
1 parent 9ccdb15 commit 28c1b5b

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

docs/howto/serialize_poro.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,32 @@ end
4242

4343
The default serializer would be `MyModelSerializer`.
4444

45+
*IMPORTANT*: There is a surprising behavior (bug) in the current implementation of ActiveModelSerializers::Model that
46+
prevents an accessor from modifying attributes on the instance. The fix for this bug
47+
is a breaking change, so we have made an opt-in configuration.
48+
49+
New applications should set:
50+
51+
```ruby
52+
ActiveModelSerializers::Model.derive_attributes_from_names_and_fix_accessors
53+
```
54+
55+
Existing applications can use the fix *and* avoid breaking changes
56+
by making a superclass for new models. For example:
57+
58+
```ruby
59+
class SerializablePoro < ActiveModelSerializers::Model
60+
derive_attributes_from_names_and_fix_accessors
61+
end
62+
```
63+
64+
So that `MyModel` above would inherit from `SerializablePoro`.
65+
66+
`derive_attributes_from_names_and_fix_accessors` prepends the `DeriveAttributesFromNamesAndFixAccessors`
67+
module and does the following:
68+
69+
- `id` will *always* be in the attributes. (This is until we separate out the caching requirement for POROs.)
70+
- Overwrites the `attributes` method to that it only returns declared attributes.
71+
`attributes` will now be a frozen hash with indifferent access.
72+
4573
For more information, see [README: What does a 'serializable resource' look like?](../../README.md#what-does-a-serializable-resource-look-like).

lib/active_model_serializers/model.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ def self.included(base)
5656
base.attributes :id
5757
end
5858

59-
# Override the initialize method so that attributes aren't processed.
60-
#
61-
# @param attributes [Hash]
62-
def initialize(attributes = {})
63-
@errors = ActiveModel::Errors.new(self)
64-
super
65-
end
66-
6759
# Override the +attributes+ method so that the hash is derived from +attribute_names+.
6860
#
6961
# The the fields in +attribute_names+ determines the returned hash.

0 commit comments

Comments
 (0)