Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1506 document passing arbitrary options to serializer #1545

Merged
merged 7 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
- [Instrumentation](general/instrumentation.md)
- [JSON API Schema](jsonapi/schema.md)
- [ARCHITECTURE](ARCHITECTURE.md)
- [Passing Arbitrary Options](general/passing_arbitrary_options.md)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this should be in howto/. I'm thinking serializers.md#other (instance_options) and rendering.md#adapter_opts (passing arbitrary options) in general/ should reference this file located in howto/.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this should be under the How to section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that as well. on it.


## How to

Expand Down
45 changes: 45 additions & 0 deletions docs/general/passing_arbitrary_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[Back to Guides](../README.md)

# Passing Arbitrary Options To A Serializer

Let's say you have a basic Post Controller:

```ruby
class PostController < ApplicationController
def dashboard
render json: @posts
end
end
```

Odds are, your serializer will look something like this:

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
end
```

This works all fine and well, but maybe you passing in some arbitrary options
into the serializer. These options can be anything that isn't already reserved for use by
ActiveModelSerializers' adapter options.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should link to rendering.md#adapter_opts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can simplify the above to

# Passing Arbitrary Options To A Serializer

In addition to the [`serialization_scope`](serializers.md#scope), any options passed to `render`
that are not reserved for the [adapter](rendering.md#adapter_opts)
are available in the serializer as [instance_options](serializers.md#instance_options).

For example, we could pass a `user_id` option:

     ```ruby
    # posts_controller.rb
    etc

Docs for serialization_scope are stuck in #1252


Here's an example:

```ruby
# posts_controller.rb
class PostController < ApplicationController
def dashboard
render json: @posts, user_id: 12
end
end

# post_serializer.rb
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body

def comments_by_me
Comments.where(user_id: instance_options[:user_id], post_id: object.id)
end
end
```