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
Changes from 3 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
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 @@
## Passing Arbitrary Options To A Serializer
Copy link
Member

Choose a reason for hiding this comment

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

Please add a line above this [Back to Guides](../README.md) and change the header level to h1 # Passing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On it.


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
Copy link
Member

Choose a reason for hiding this comment

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

but maybe you want to pass some 'arbitrary' options into the serializer?

into the serializer. Here's what you would do:

### posts_controller.rb
Copy link
Member

Choose a reason for hiding this comment

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

I don't know what's your opinion on this but I usually find easier to easier to understand when having the full code snippet instead of just part of it. Also I usually tend to prefer having one snippet with comments to specify the filename instead of using h3 here. An example here would be:

# post_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
  has_many :comments_by_me
  def comments_by_me  
    Comments.where(user_id: instance_options[:user_id], post_id: object.id)
  end
end

Maybe that's just my opinion and might not be true for everyone so let me know what you guys think.

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 really don't mind that at all. Definitely could flow a bit better. Going to do an A / B comparison and see what's up.


```ruby
...
def dashboard
render json: @posts, user_id: 12
end
...
```

### posts_serializer.rb
Copy link
Member

Choose a reason for hiding this comment

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

Typo. Should be post_serializer.rb.


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

These options can be anything that isn't already reserved for use by
ActiveModelSerializers' adapter options.