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

implement sparse fieldsets #86

Merged
merged 7 commits into from
Oct 17, 2016
Merged

Conversation

Azdaroth
Copy link
Contributor

No description provided.

@Azdaroth
Copy link
Contributor Author

@fotinakis Looks good to merge? The specs fail for Ruby head (2.4) because of the json gem. There's already an issue created for that: ruby/json#303, but it hasn't been resolved... What do you think about dropping specs for Ruby 2.4 and add Ruby 2.3 testing scenario instead?

@Azdaroth
Copy link
Contributor Author

@fotinakis thoughts?

post = create(:post, :with_author, long_comments: long_comments)

serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author', users: ''}, include: 'author')
expect(serialized_data).to eq ({
Copy link
Owner

Choose a reason for hiding this comment

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

The style of these tests should be updated to match how the rest of the tests look, and they should be much shorter if possible. I think the way to do that would be split them into two different concerns like the rests of the tests: 1) put all attribute handling tests in the top internal-only serialize_primary block. 2) Now that sparse fieldset attributes are guaranteed to be tested above, have only short relationships handling tests here. They should look like the other tests, which rely on serialize_primary for the attrributes and only test the relations. See inherits relations test for an example.

Also, nitpicks, should match the style of the other tests as well -- single quotes, spaces around hash-rocket, etc.

@@ -260,12 +268,18 @@ def self.serialize(objects, options = {})
includes = options[:include]
includes = (includes.is_a?(String) ? includes.split(',') : includes).uniq if includes

fields = options[:fields] || {}
fields = Hash[fields.map do |type, whitelisted_fields|
Copy link
Owner

Choose a reason for hiding this comment

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

This could be cleaned up and commented a bit, it's very non-obvious what it's doing by looking at it.

@fotinakis
Copy link
Owner

Sorry for the delay @Azdaroth — two comments above, and yeah I think dropping Ruby 2.4 support right now would be fine.

@Azdaroth
Copy link
Contributor Author

@fotinakis adressed the comments and all specs are green now ;).

@fotinakis
Copy link
Owner

Awesome, this looks great. Two last things:

  • Can you make the fields argument also accept an array? It's weird to have to do {posts: 'title,author,long_comments'} as the official interface instead of {posts: ['title', 'author', 'long_comments']}. We can't assume that fields is coming from the HTTP request, it might just be set in a controller manually, so we should support both forms (similar to how we handle includes right now).
  • Optionally, adding a small section to the README about this would be great. I can do that later if you want to skip this.

@Azdaroth
Copy link
Contributor Author

Good point about decoupling from HTTP request, will update the PR.

@Azdaroth
Copy link
Contributor Author

@fotinakis Updated with docs :).


```json
{
"data" => {
Copy link
Owner

Choose a reason for hiding this comment

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

This is not valid JSON. :) Need to turn all the => into :

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woah, right ;)

}
```

You could also pass an array of specifi fields for given type instead of comma-separated values:
Copy link
Owner

Choose a reason for hiding this comment

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

specifi --> specific

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

You could also pass an array of specifi fields for given type instead of comma-separated values:

``` ruby
JSONAPI::Serializer.serialize(post, fields: {posts: ['title', 'author'], users: ['name']}, include: 'author')
Copy link
Owner

Choose a reason for hiding this comment

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

Can you set fields into a variable and pass it in? That would make this line shorter and more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@fotinakis fotinakis merged commit f03663e into fotinakis:master Oct 17, 2016
@fotinakis
Copy link
Owner

Thanks for contributing this! Now released in v0.16.0.

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.

2 participants