-
Notifications
You must be signed in to change notification settings - Fork 90
implement sparse fieldsets #86
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
Changes from all commits
c02a419
f041424
858eb26
f12ebf7
1ebc351
b40d368
3ca3555
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ rvm: | |
- 1.9.3 | ||
- 2.1.1 | ||
- 2.2.2 | ||
- ruby-head | ||
- 2.3.1 | ||
before_install: | ||
- gem install bundler | ||
script: bundle exec rspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -854,6 +854,125 @@ def read_attribute_for_validation(attr) | |
}) | ||
end | ||
end | ||
|
||
context 'sparse fieldsets' do | ||
it 'allows to limit fields(attributes) for serialized resource' do | ||
first_user = create(:user) | ||
second_user = create(:user) | ||
first_comment = create(:long_comment, user: first_user) | ||
second_comment = create(:long_comment, user: second_user) | ||
long_comments = [first_comment, second_comment] | ||
post = create(:post, :with_author, long_comments: long_comments) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title'}) | ||
expect(serialized_data).to eq ({ | ||
'data' => { | ||
'type' => 'posts', | ||
'id' => post.id.to_s, | ||
'attributes' => { | ||
'title' => post.title, | ||
}, | ||
'links' => { | ||
'self' => '/posts/1' | ||
} | ||
} | ||
}) | ||
end | ||
|
||
it 'allows to limit fields(relationships) for serialized resource' do | ||
first_user = create(:user) | ||
second_user = create(:user) | ||
first_comment = create(:long_comment, user: first_user) | ||
second_comment = create(:long_comment, user: second_user) | ||
long_comments = [first_comment, second_comment] | ||
post = create(:post, :with_author, long_comments: long_comments) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author,long_comments'}) | ||
expect(serialized_data['data']['relationships']).to eq ({ | ||
'author' => { | ||
'links' => { | ||
'self' => '/posts/1/relationships/author', | ||
'related' => '/posts/1/author' | ||
} | ||
}, | ||
'long-comments' => { | ||
'links' => { | ||
'self' => '/posts/1/relationships/long-comments', | ||
'related' => '/posts/1/long-comments' | ||
} | ||
} | ||
}) | ||
end | ||
|
||
it "allows also to pass specific fields as array instead of comma-separates values" do | ||
first_user = create(:user) | ||
second_user = create(:user) | ||
first_comment = create(:long_comment, user: first_user) | ||
second_comment = create(:long_comment, user: second_user) | ||
long_comments = [first_comment, second_comment] | ||
post = create(:post, :with_author, long_comments: long_comments) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: ['title', 'author']}) | ||
expect(serialized_data['data']['attributes']).to eq ({ | ||
'title' => post.title | ||
}) | ||
expect(serialized_data['data']['relationships']).to eq ({ | ||
'author' => { | ||
'links' => { | ||
'self' => '/posts/1/relationships/author', | ||
'related' => '/posts/1/author' | ||
} | ||
} | ||
}) | ||
end | ||
|
||
it 'allows to limit fields(attributes and relationships) for included resources' do | ||
first_user = create(:user) | ||
second_user = create(:user) | ||
first_comment = create(:long_comment, user: first_user) | ||
second_comment = create(:long_comment, user: second_user) | ||
long_comments = [first_comment, second_comment] | ||
post = create(:post, :with_author, long_comments: long_comments) | ||
|
||
expected_primary_data = serialize_primary(post, { | ||
serializer: MyApp::PostSerializer, | ||
include_linkages: ['author'], | ||
fields: { 'posts' => [:title, :author] } | ||
}) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author', users: ''}, include: 'author') | ||
expect(serialized_data).to eq ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also, nitpicks, should match the style of the other tests as well -- single quotes, spaces around hash-rocket, etc. |
||
'data' => expected_primary_data, | ||
'included' => [ | ||
serialize_primary(post.author, serializer: MyAppOtherNamespace::UserSerializer, fields: { 'users' => [] }) | ||
] | ||
}) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author'}, include: 'author') | ||
expect(serialized_data).to eq ({ | ||
'data' => expected_primary_data, | ||
'included' => [ | ||
serialize_primary(post.author, serializer: MyAppOtherNamespace::UserSerializer) | ||
] | ||
}) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author', users: 'nonexistent'}, include: 'author') | ||
expect(serialized_data).to eq ({ | ||
'data' => expected_primary_data, | ||
'included' => [ | ||
serialize_primary(post.author, serializer: MyAppOtherNamespace::UserSerializer, fields: { 'users' => [:nonexistent] }) | ||
] | ||
}) | ||
|
||
serialized_data = JSONAPI::Serializer.serialize(post, fields: {posts: 'title,author', users: 'name'}, include: 'author') | ||
expect(serialized_data).to eq ({ | ||
'data' => expected_primary_data, | ||
'included' => [ | ||
serialize_primary(post.author, serializer: MyAppOtherNamespace::UserSerializer, fields: { 'users' => [:name] }) | ||
] | ||
}) | ||
end | ||
end | ||
end | ||
|
||
describe 'serialize (class method)' do | ||
|
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.
This could be cleaned up and commented a bit, it's very non-obvious what it's doing by looking at it.