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

Explicitly set serializer for associations #696

Merged
merged 1 commit into from
Jan 6, 2015

Conversation

ggordon
Copy link
Contributor

@ggordon ggordon commented Oct 24, 2014

Adding back the ability to specify the serializer for an association.
Ex. has_one :author, serializer: PersonSerializer

if resource.respond_to?(:to_ary)
config.array_serializer
else
get_serializer_for(resource.class)
options.fetch(:options, {})
Copy link
Member

Choose a reason for hiding this comment

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

Trying to fetch :options from :options don't contribute to readability. But it's okay for me. It's just a detail, I liked the implementation 👍
If you want to try a different approach you could simply add options[:options] content to the type level.

@joaomdmoura
Copy link
Member

@ggordon, would be cool to update the README either 😄
LGTM 👍

@ggordon
Copy link
Contributor Author

ggordon commented Oct 24, 2014

@joaomdmoura Thanks for feedback, I'll update the README.

@ggordon ggordon force-pushed the explicit_serializer branch from d3d72b0 to 7b683b8 Compare November 6, 2014 14:55
@mieko
Copy link
Contributor

mieko commented Nov 7, 2014

I noticed the implementation doesn't guard against nil associations like the implicit version of the call, that is:

class UserSerializer < ActiveModel::Serializer
  # ...
end

# Assume comment.user == nil
class CommentSerializer < ActiveModel::Serializer
  # This call guards against nil, outputting null, when implicitly using UserSerializer
  belongs_to :user

  # When it's made explicit, if user is nil, a UserSerializer is created anyway, 
  # with the nil object, and errors out trying to access members.
  belongs_to :user, serializer: UserSerializer
end

@ggordon
Copy link
Contributor Author

ggordon commented Nov 7, 2014

@mieko Thanks, I'll look into it.

@ggordon ggordon force-pushed the explicit_serializer branch from 7b683b8 to 03829e1 Compare November 7, 2014 20:09
@kurko
Copy link
Member

kurko commented Nov 12, 2014

@ggordon can you rebase? Also, can you find a better name for the options commented? Perhaps association_options?

@ggordon
Copy link
Contributor Author

ggordon commented Nov 12, 2014

@kurko I'll fix and rebase in the morning.

Document specifying serializer for assocaition
@ggordon ggordon force-pushed the explicit_serializer branch from 03829e1 to 9f97158 Compare November 13, 2014 15:19
@kurko kurko added the V: 0.10.x label Jan 6, 2015
kurko added a commit that referenced this pull request Jan 6, 2015
Explicitly set serializer for associations
@kurko kurko merged commit 6eb75af into rails-api:master Jan 6, 2015
@kurko
Copy link
Member

kurko commented Jan 6, 2015

Thanks for your work, @ggordon, and sorry for the huuuuge delay.

@ggordon ggordon deleted the explicit_serializer branch January 6, 2015 12:16
@ggordon
Copy link
Contributor Author

ggordon commented Jan 6, 2015

No problem. @kurko, thanks for merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants