From f3b202746be8dab78c8fe57f8034e3bb49d3e312 Mon Sep 17 00:00:00 2001 From: Abhay Nikam Date: Thu, 7 Jun 2018 09:58:48 +0530 Subject: [PATCH 1/4] Added examples to use `class_name` options for association. --- docs/general/serializers.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 94af4a0d..47162274 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -81,6 +81,7 @@ e.g. ```ruby has_one :bio has_one :blog, key: :site +has_one :blog, class_name: "Blog" has_one :maker, virtual_value: { id: 1 } has_one :blog do |serializer| @@ -114,6 +115,7 @@ e.g. has_many :comments has_many :comments, key: :reviews has_many :comments, serializer: CommentPreviewSerializer +has_many :comments, class_name: "Comment" has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }] has_many :comments, key: :last_comments do last(1) @@ -127,6 +129,7 @@ e.g. ```ruby belongs_to :author, serializer: AuthorPreviewSerializer belongs_to :author, key: :writer +belongs_to :author, class_name: "Author" belongs_to :post belongs_to :blog def blog @@ -294,7 +297,7 @@ end Whether you write the method as above or as `object.comments.where(created_by: scope)` is a matter of preference (assuming `scope_name` has been set). -Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers. +Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers. ##### Controller Authorization Context @@ -381,7 +384,7 @@ class PostsController < ActionController::Base end end ``` -Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`. +Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`. #### #read_attribute_for_serialization(key) From 65cf4836c8b7850d847e6d99f5060520e7c2677f Mon Sep 17 00:00:00 2001 From: Abhay Nikam Date: Thu, 7 Jun 2018 10:04:03 +0530 Subject: [PATCH 2/4] Updated `class_name` defination. --- docs/general/serializers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 47162274..5b3c0bcd 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -65,7 +65,7 @@ Where: - `virtual_value:` - `polymorphic:` defines if polymorphic relation type should be nested in serialized association. - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. - - `class_name:` used to determine `type` when `type` not given + - `class_name:` used to determine `type` when `type` not given. `class_name` option to supply the model name as string. - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details. - optional: `&block` is a context that returns the association's attributes. From 993a48fdcf38fdbc5cec711353d8aa00880203ec Mon Sep 17 00:00:00 2001 From: Abhay Nikam Date: Thu, 7 Jun 2018 10:05:21 +0530 Subject: [PATCH 3/4] wordsmitting --- docs/general/serializers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 5b3c0bcd..28417898 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -65,7 +65,7 @@ Where: - `virtual_value:` - `polymorphic:` defines if polymorphic relation type should be nested in serialized association. - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. - - `class_name:` used to determine `type` when `type` not given. `class_name` option to supply the model name as string. + - `class_name:` used to determine `type` when `type` is not given. `class_name` option to supply the model name as string. - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details. - optional: `&block` is a context that returns the association's attributes. From 4559774d6040ed48f2bc1161e03961e209cbe0bb Mon Sep 17 00:00:00 2001 From: Abhay Nikam Date: Thu, 21 Jun 2018 08:49:33 +0530 Subject: [PATCH 4/4] Updated the defination for the class_name in documentation --- docs/general/serializers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 28417898..e951c08f 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -65,7 +65,7 @@ Where: - `virtual_value:` - `polymorphic:` defines if polymorphic relation type should be nested in serialized association. - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. - - `class_name:` used to determine `type` when `type` is not given. `class_name` option to supply the model name as string. + - `class_name:` the (String) model name used to determine `type`, when `type` is not given. e.g. `class_name: "Comment"` would imply the type `comments` - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details. - optional: `&block` is a context that returns the association's attributes.