-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Grape formatter feature requested in #1258 - Rebased and Repushed (#1273) #1336
Conversation
module Formatters | ||
module ActiveModelSerializers | ||
class << self | ||
def call(resource, env) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look great.
- would you mind changing this to
def self.call(resource, env)
? I don't think there's any advantage to opening the singleton class here - could you add the documentation from
lib/grape/active_model_serializers.rb
to the docs or README as you see fit. (I think there's an integrations page in progress somewhere) - Could you add a test so we can better support this code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref: #1312
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 I'll get that done for you, hopefully within the next 48 hours.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic. I apologize for not bring it up earlier. Somehow didn't see it
@@ -21,3 +21,4 @@ tmp | |||
*.swp | |||
.ruby-version | |||
.ruby-gemset | |||
.idea |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non project-specific ignores do not belong in the .gitignore
. You should pur them in your local ignore file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True enough, but there's already non-project stuff in there
B mobile phone
On Nov 19, 2015, at 5:47 PM, Lucas Hosseini notifications@github.com wrote:
In .gitignore:
@@ -21,3 +21,4 @@ tmp
*.swp
.ruby-version
.ruby-gemset
+.idea
Non project-specific ignores do not belong in the .gitignore. You should pur them in your local ignore file.—
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True enough, but not reason to add more imho.
@bf4 wrt to building a test, since it's not my code and I'm not familiar with how AMS works internally, I'm struggling to come up with a good strategy for testing effectively - could you give me some pointers so I don't waste time on a crappy test? |
Maybe take a look at how grape or a grape formatter tests it? You'd likely have to add grape as a dev dependency B mobile phone
|
@bf4 I've implemented some tests which show that it works with Grape - I added one to show that adding metadata works, but the test fails on all Rails versions except for 4.1 - Could you provide insight as to why that might be? Is this a bug in Grape or in this PR? |
b437365
to
7d83e1a
Compare
@@ -47,4 +47,5 @@ Gem::Specification.new do |spec| | |||
|
|||
spec.add_development_dependency 'bundler', '~> 1.6' | |||
spec.add_development_dependency 'timecop', '~> 0.7' | |||
spec.add_development_dependency 'grape', '~> 0.13.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, do you mean to be 0.13.x
where x >= 0
or 0.x
where x >= 13
?. If the latter, it should be ~> 0.13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter. Thanks for pointing that out 😄
Edit: Done
Thanks for all your work on this. I'm hoping you can figure out the build failures so I don't have to... a quick look makes it seem it's not using the json api adapter. |
@bf4 Figured it out - there's a difference between how ActiveRecord 4.2 and 4.0 - 4.1 handle |
@@ -14,6 +14,7 @@ Breaking changes: | |||
|
|||
Features: | |||
|
|||
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape 0.13.x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be >= 0.13, < 1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per + spec.add_development_dependency 'grape', '~> 0.13'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
module Formatters | ||
module ActiveModelSerializers | ||
def self.call(resource, env) | ||
return resource.to_json unless resource.is_a?(Array) || resource.respond_to?(:model_name) || resource.class.respond_to?(:model_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just thinking out loud: might be easier to read as
if resource.is_a...
resource.to_json
else
..new(..).to_json
end
Naive question: ActiveModelSerializers should handle Arrays and non-models just fine. See the logic in ActionController::Serialization. Any reason you're checking for an Array or model_name
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 that was put there by the previous developer, I have no idea why it was there, but assumed it was OK due to it being in the previous PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I just never noticed it till now. Progressive review I guess?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄 better caught now than in upstream master. I'll remove it
Made the documentation changes mentioned in inline comments, rebased & pushed 😄 |
ref: #1258 |
Ok, rebase off of master and they should pass #1349 |
b1a9161
to
5c5d8ca
Compare
@bf4 All good to merge? |
@johnathanludwig I'm sorry this hasn't been merged. I'd prefer to get a second LGTM on it, so @rails-api/ams any objects to merging this? 24 hours till merge. Also, you'll have to rebase it off master and resolve conflicts. Let me know if you'd like help. |
@bf4 I think you meant to tag @johnhamelink |
@bf4 I've rebased and resolved the conflicts (sensibly I hope). Let me know if you've any further comments! |
@@ -53,4 +53,5 @@ Gem::Specification.new do |spec| | |||
spec.add_development_dependency 'bundler', '~> 1.6' | |||
spec.add_development_dependency 'timecop', '~> 0.7' | |||
spec.add_development_dependency 'minitest-reporters' | |||
spec.add_development_dependency 'grape', '~> 0.13' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be [' >= 0.13', '< 1.0'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog - Remove parens from `render`, use `serializable` in all tests.
def test_formatter_handles_arrays | ||
get '/grape/render_array_with_json_api' | ||
|
||
expected = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serializable? or I can merge it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How could I create an array to pass into the serializer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you modified the routing as
+ def setup
+ @post = ARModels::Post.create(title: 'Dummy Title', body: 'Lorem Ipsum')
+ ARModels::Post.create(title: 'post 2', body: 'body 2')
+ @posts = ARModels::Post.all
+ end
# ..
resources :grape do
get '/render' do
- post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
+ render @post
end
get '/render_with_json_api' do
- post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
- render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
+ render @post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
end
get '/render_array_with_json_api' do
- render ARModels::Post.all, adapter: :json_api
+ render @posts, adapter: :json_api
end
end
Then your tests become testing
serializable_resource = serializable(@post)
serializable_resource = serializable(@post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
serializable_resource = serializable(@posts, adapter: :json_api)
or expected = serializable_resource.to_json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I never got back to you on this - work got in the way 😄
I tried to implement what you said, but unfortunately it doesn't work due to the fact that @post
can't be associated from within GrapeTest
class. I say LGTM.
@johnhamelink merged, yay! |
🎉 hurray! Thanks @bf4 for your great advice & guidance :) |
I've rebased, squashed and repushed #1273 so we can get this merged!
Edit: Just want to explicitly point out that credit for code goes to Julian Paas (https://github.com/jpaas)
Todos:
self.call
instead of a singleton.gitignore