docs/general/serializers.md in active_model_serializers-0.10.7 vs docs/general/serializers.md in active_model_serializers-0.10.8

- old
+ new

@@ -63,11 +63,11 @@ - `if:` - `unless:` - `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:` 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. - prevents `association_name` method from being called. - return value of block is used as the association value. @@ -79,10 +79,11 @@ 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| serializer.cached_blog end @@ -112,10 +113,11 @@ ```ruby 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) end ``` @@ -125,10 +127,11 @@ 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 Blog.new(id: 999, name: 'Custom blog') end @@ -233,10 +236,19 @@ link(:link_authors) { link_authors_url } link :other, 'https://example.com/resource' link(:posts) { link_author_posts_url(object) } ``` +Just like attributes, links also support conditions in options +```ruby +link(:secret, if: :internal?) { object.secret_link } + +def internal? + instance_options[:context] == :internal +end +``` + #### #object The object being serialized. #### #root @@ -292,11 +304,11 @@ ``` 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 In the controller, the scope/scope_name options are equal to the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20), @@ -379,11 +391,11 @@ def current_user User.new(id: 2, name: 'Bob', admin: false) 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) The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'` @@ -472,10 +484,10 @@ the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions. ```ruby class MySerializer < ActiveModel::Serializer def self.serializer_for(model, options) - return SparseAdminSerializer if model.class == 'Admin' + return SparseAdminSerializer if model.class.name == 'Admin' super end # the rest of the serializer end