From 28c1b5bef639f592e0359ed38e9ee5c3cf0091fa Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 19 Jan 2017 16:07:47 -0600 Subject: [PATCH] Document Model delcared attributes --- docs/howto/serialize_poro.md | 28 +++++++++++++++++++++++++++ lib/active_model_serializers/model.rb | 8 -------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/howto/serialize_poro.md b/docs/howto/serialize_poro.md index 20091c52..3b98267c 100644 --- a/docs/howto/serialize_poro.md +++ b/docs/howto/serialize_poro.md @@ -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). diff --git a/lib/active_model_serializers/model.rb b/lib/active_model_serializers/model.rb index b61661bc..7e05ffe6 100644 --- a/lib/active_model_serializers/model.rb +++ b/lib/active_model_serializers/model.rb @@ -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.