From 257adfc80e0eccf5fce7ef58fb4f43bff1120432 Mon Sep 17 00:00:00 2001 From: "Gary S. Weaver" Date: Mon, 8 Oct 2012 17:17:14 -0400 Subject: [PATCH 1/2] #133 adding documentation for :serializer and :polymorphic options on associations --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index e98bf021..903560b0 100644 --- a/README.md +++ b/README.md @@ -328,6 +328,37 @@ class PostSerializer < ActiveModel::Serializer end ``` +Use the `:serializer` option to specify a custom serializer class: + +```ruby +class PostSerializer < ActiveModel::Serializer + attributes :id, :title, :body + has_one :author + has_many :comments, :serializer => CommentShortSerializer + + def include_associations! + include! :author if scope.admin? + include! :comments unless object.comments_disabled? + end +end +``` + +Use the `:polymorphic` option to specify an association that is polymorphic (STI): + +```ruby +class PostSerializer < ActiveModel::Serializer + attributes :id, :title, :body + has_one :author + has_many :comments + has_one :reviewer, :polymorphic => true + + def include_associations! + include! :author if scope.admin? + include! :comments unless object.comments_disabled? + end +end +``` + ## Embedding Associations By default, associations will be embedded inside the serialized object. So if From 9cd472c160416af8f45422dec877289a7aa92948 Mon Sep 17 00:00:00 2001 From: "Gary S. Weaver" Date: Tue, 9 Oct 2012 09:37:59 -0400 Subject: [PATCH 2/2] #133 making :serializer and :polymorphic documentation less verbose --- README.md | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/README.md b/README.md index 903560b0..1fe9da16 100644 --- a/README.md +++ b/README.md @@ -328,35 +328,11 @@ class PostSerializer < ActiveModel::Serializer end ``` -Use the `:serializer` option to specify a custom serializer class: +You may also use the `:serializer` option to specify a custom serializer class and the `:polymorphic` option to specify an association that is polymorphic (STI), e.g.: ```ruby -class PostSerializer < ActiveModel::Serializer - attributes :id, :title, :body - has_one :author has_many :comments, :serializer => CommentShortSerializer - - def include_associations! - include! :author if scope.admin? - include! :comments unless object.comments_disabled? - end -end -``` - -Use the `:polymorphic` option to specify an association that is polymorphic (STI): - -```ruby -class PostSerializer < ActiveModel::Serializer - attributes :id, :title, :body - has_one :author - has_many :comments has_one :reviewer, :polymorphic => true - - def include_associations! - include! :author if scope.admin? - include! :comments unless object.comments_disabled? - end -end ``` ## Embedding Associations