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

Create DefaultSerializer so that as_json uses same interface. #207

Merged
merged 13 commits into from
Mar 6, 2013
Merged

Create DefaultSerializer so that as_json uses same interface. #207

merged 13 commits into from
Mar 6, 2013

Conversation

stantona
Copy link
Contributor

When adding acive_model_serializers as a gem in an exiting project, I noticed that it broke existing functionality, particularly when responding with an array of objects:

respond_with users

where User overrides as_json.

I found that ArraySerializers was circumventing this method calling directly into serializable_hash.

I took the idea of an old pull request (that was never merged in) and applied DefaultSerializer which simply calls into object's as_json when serializable_hash is called. This also provides a common interface.

All tests pass.

I'm still not entirely sure if this is the right way of doing this, or what the implications are, but suffice to say this fixed my particular problem.

LeeMallabone and others added 3 commits February 6, 2013 17:32
This is to ensure that PORO's as_json is called if no serializer
is specified.

Original behaviour was that serializable_hash was being called,
overriding the as_json method.
@@ -20,5 +20,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'activemodel', '>= 3.0'
gem.add_development_dependency "rails", ">= 3.0"
gem.add_development_dependency "pry"
gem.add_development_dependency "pry-nav"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why'd this get added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oversight, it helped with some debugging. I'll remove it.
On Feb 10, 2013 11:37 AM, "Steve Klabnik" notifications@github.com wrote:

In active_model_serializers.gemspec:

@@ -20,5 +20,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'activemodel', '>= 3.0'
gem.add_development_dependency "rails", ">= 3.0"
gem.add_development_dependency "pry"

  • gem.add_development_dependency "pry-nav"

Why'd this get added?


Reply to this email directly or view it on GitHubhttps://github.com//pull/207/files#r2955823..

@steveklabnik
Copy link
Contributor

All tests pass, although I had to rearrange the order of fields in a hash to make one particular test pass.

This kinda bugs me, though it may be an issue with the test, and not with your feature.

@stantona
Copy link
Contributor Author

Yeah this bugs me too. Have to dig deeper here.
On Feb 10, 2013 11:38 AM, "Steve Klabnik" notifications@github.com wrote:

All tests pass, although I had to rearrange the order of fields in a hash
to make one particular test pass.

This kinda bugs me, though it may be an issue with the test, and not with
your feature.


Reply to this email directly or view it on GitHubhttps://github.com//pull/207#issuecomment-13359416..

@stantona
Copy link
Contributor Author

@steveklabnik I removed the pry-nav gem in the gemspec. And after looking at the test again this morning, I realized that as suspected the order of the fields in the hash wasn't a problem. At one point I mistakenly thought I was dealing with strings and not hashes. I restored it to original order and all tests pass.

@steveklabnik
Copy link
Contributor

Thanks! I wonder why the jruby tests are failing: https://travis-ci.org/rails-api/active_model_serializers/jobs/4723457

@stantona
Copy link
Contributor Author

@steveklabnik hmm looks like it's failing when using Gemfile.edge. I get the same result on the master branch on my local machine. Need to dig deeper.

Michi Huber and others added 5 commits February 26, 2013 09:57
Merge remote-tracking branch 'fblee/master'
…_serializer

Fix rendering nil with custom serializer
'embed_key' option to allow embedding by attributes other than IDs
@steveklabnik
Copy link
Contributor

This doesn't merge cleanly any more, can you take a look? The Jruby thing shouldn't be an issue.

@stantona
Copy link
Contributor Author

stantona commented Mar 6, 2013

Sure I'll take a look sometime this evening. Thanks.

On 5 March 2013 16:06, Steve Klabnik notifications@github.com wrote:

This doesn't merge cleanly any more, can you take a look? The Jruby thing
shouldn't be an issue.


Reply to this email directly or view it on GitHubhttps://github.com//pull/207#issuecomment-14474157
.

steveklabnik and others added 2 commits March 5, 2013 17:07
Conflicts:
	lib/active_model/array_serializer.rb
@stantona
Copy link
Contributor Author

stantona commented Mar 6, 2013

@steveklabnik Merged.

@steveklabnik
Copy link
Contributor

Now there's a bunch of test failures. :(

In the future, please rebase rather than merge master back in. ❤️

@stantona
Copy link
Contributor Author

stantona commented Mar 6, 2013

@steveklabnik Ahh, the test failed in earlier versions because of a line break. I fixed it and it seems to be good now. And yep, I'll rebase in the future, cheers.

steveklabnik added a commit that referenced this pull request Mar 6, 2013
Create DefaultSerializer so that as_json uses same interface.
@steveklabnik steveklabnik merged commit 7b86838 into rails-api:master Mar 6, 2013
@steveklabnik
Copy link
Contributor

Thank you! Duck typing ftw! ❤️

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

Successfully merging this pull request may close these issues.

3 participants