mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Document Model delcared attributes
This commit is contained in:
parent
9ccdb15610
commit
28c1b5bef6
@ -42,4 +42,32 @@ end
|
||||
|
||||
The default serializer would be `MyModelSerializer`.
|
||||
|
||||
*IMPORTANT*: There is a surprising behavior (bug) in the current implementation of ActiveModelSerializers::Model that
|
||||
prevents an accessor from modifying attributes on the instance. The fix for this bug
|
||||
is a breaking change, so we have made an opt-in configuration.
|
||||
|
||||
New applications should set:
|
||||
|
||||
```ruby
|
||||
ActiveModelSerializers::Model.derive_attributes_from_names_and_fix_accessors
|
||||
```
|
||||
|
||||
Existing applications can use the fix *and* avoid breaking changes
|
||||
by making a superclass for new models. For example:
|
||||
|
||||
```ruby
|
||||
class SerializablePoro < ActiveModelSerializers::Model
|
||||
derive_attributes_from_names_and_fix_accessors
|
||||
end
|
||||
```
|
||||
|
||||
So that `MyModel` above would inherit from `SerializablePoro`.
|
||||
|
||||
`derive_attributes_from_names_and_fix_accessors` prepends the `DeriveAttributesFromNamesAndFixAccessors`
|
||||
module and does the following:
|
||||
|
||||
- `id` will *always* be in the attributes. (This is until we separate out the caching requirement for POROs.)
|
||||
- Overwrites the `attributes` method to that it only returns declared attributes.
|
||||
`attributes` will now be a frozen hash with indifferent access.
|
||||
|
||||
For more information, see [README: What does a 'serializable resource' look like?](../../README.md#what-does-a-serializable-resource-look-like).
|
||||
|
||||
@ -56,14 +56,6 @@ module ActiveModelSerializers
|
||||
base.attributes :id
|
||||
end
|
||||
|
||||
# Override the initialize method so that attributes aren't processed.
|
||||
#
|
||||
# @param attributes [Hash]
|
||||
def initialize(attributes = {})
|
||||
@errors = ActiveModel::Errors.new(self)
|
||||
super
|
||||
end
|
||||
|
||||
# Override the +attributes+ method so that the hash is derived from +attribute_names+.
|
||||
#
|
||||
# The the fields in +attribute_names+ determines the returned hash.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user