From dba85f2720a167aa3e37c0367cf7fc9861840126 Mon Sep 17 00:00:00 2001 From: Chris Peters Date: Sat, 2 Apr 2016 13:18:07 -0400 Subject: [PATCH] #1594 - Document generator's auto-extending of (if it exists) --- docs/general/getting_started.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/general/getting_started.md b/docs/general/getting_started.md index d9d08ae3..ac6d5f79 100644 --- a/docs/general/getting_started.md +++ b/docs/general/getting_started.md @@ -80,6 +80,32 @@ end ActiveModelSerializers will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`). +### Extending a Base `ApplicationSerializer` + +By default, new serializers descend from `ActiveModel::Serializer`. However, if +you wish to share behavior across your serializers, you can create an +`ApplicationSerializer` at `app/serializers/application_serializer.rb`: + +```ruby +class ApplicationSerializer < ActiveModel::Serializer +end +``` + +Then any newly-generated serializers will automatically descend from +`ApplicationSerializer`. + +``` +$ rails g serializer post +``` + +Now generates: + +```ruby +class PostSerializer < ApplicationSerializer + attributes :id +end +```` + ## Rails Integration ActiveModelSerializers will automatically integrate with your Rails app,