Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ You can specify client_id_prefix:
JsonApi::Parameters.client_id_prefix = 'client_'
```

Default client_id_prefix is `cid_`
Default client_id_prefix is `nil`

All IDs starting with `JsonApi::Parameters.client_id_prefix` will be removed from params.
If defined, all IDs starting with `JsonApi::Parameters.client_id_prefix` will be removed from params.

In case of creating new nested resources, client will need to generate IDs sent in `relationships` and `included` parts of request.

Expand Down
2 changes: 2 additions & 0 deletions lib/jsonapi_parameters/default_handlers/base_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def included_object_base(included_object)
end

def client_generated_id?(related_id)
return false unless JsonApi::Parameters.client_id_prefix.present?
Comment thread
choosen marked this conversation as resolved.
Outdated

related_id.to_s.starts_with?(JsonApi::Parameters.client_id_prefix)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi_parameters/parameters.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module JsonApi
module Parameters
@ensure_underscore_translation = false
@client_id_prefix = 'cid_'
@client_id_prefix = nil
Comment thread
choosen marked this conversation as resolved.
Outdated

class << self
attr_accessor :ensure_underscore_translation
Expand Down
236 changes: 163 additions & 73 deletions spec/integration/authors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,91 +137,181 @@
expect(jsonapi_response[:data][:relationships][:posts][:data]).to eq([])
end

it 'creates an author with a post, and then adds a new post and updates existing one' do
params = {
data: {
type: 'authors',
attributes: {
name: 'John Doe'
},
relationships: {
posts: {
data: [
{
id: '123',
type: 'post'
}
]
}
}
},
included: [
{
type: 'post',
id: '123',
context 'when client id prefix is defined' do
it 'creates an author with a post, and then adds a new post and updates existing one' do
params = {
data: {
type: 'authors',
attributes: {
title: 'Some title',
body: 'Some body that I used to love',
category_name: 'Some category'
name: 'John Doe'
},
relationships: {
posts: {
data: [
{
id: '123',
type: 'post'
}
]
}
}
}
]
}
},
included: [
{
type: 'post',
id: '123',
attributes: {
title: 'Some title',
body: 'Some body that I used to love',
category_name: 'Some category'
}
}
]
}

post_with_rails_fix :create, params: params
post_with_rails_fix :create, params: params

author_id = jsonapi_response[:data][:id]
post_id = jsonapi_response[:data][:relationships][:posts][:data].first[:id]
params = {
id: author_id,
data: {
type: 'authors',
author_id = jsonapi_response[:data][:id]
post_id = jsonapi_response[:data][:relationships][:posts][:data].first[:id]
params = {
id: author_id,
relationships: {
posts: {
data: [
{
id: post_id,
type: 'post'
},
{
id: 'cid_new_post',
type: 'post'
}
]
data: {
type: 'authors',
id: author_id,
relationships: {
posts: {
data: [
{
id: post_id,
type: 'post'
},
{
id: 'cid_new_post',
type: 'post'
}
]
}
}
}
},
included: [
{
type: 'post',
id: post_id,
},
included: [
{
type: 'post',
id: post_id,
attributes: {
title: 'Updated title',
body: 'Updated body',
category_name: 'Updated category'
}
},
{
type: 'post',
id: 'cid_new_post',
attributes: {
title: 'New title',
body: 'New body',
category_name: 'New category'
}
}
]
}

JsonApi::Parameters.client_id_prefix = 'cid_'

patch_with_rails_fix :update, params: params, as: :json

expect(Post.first.title).to eq('Updated title')
expect(Post.first.body).to eq('Updated body')
expect(Post.first.category_name).to eq('Updated category')
expect(Post.last.title).to eq('New title')
expect(Post.last.body).to eq('New body')
expect(Post.last.category_name).to eq('New category')

JsonApi::Parameters.client_id_prefix = nil
end
end

context 'when client id prefix is not defined' do
it 'raises an error for not being able to find a record with client defined ID' do
params = {
data: {
type: 'authors',
attributes: {
title: 'Updated title',
body: 'Updated body',
category_name: 'Updated category'
name: 'John Doe'
},
relationships: {
posts: {
data: [
{
id: '123',
type: 'post'
}
]
}
}
},
{
type: 'post',
id: 'cid_new_post',
attributes: {
title: 'New title',
body: 'New body',
category_name: 'New category'
included: [
{
type: 'post',
id: '123',
attributes: {
title: 'Some title',
body: 'Some body that I used to love',
category_name: 'Some category'
}
}
}
]
}
]
}

patch_with_rails_fix :update, params: params, as: :json
post_with_rails_fix :create, params: params

author_id = jsonapi_response[:data][:id]
post_id = jsonapi_response[:data][:relationships][:posts][:data].first[:id]
params = {
id: author_id,
data: {
type: 'authors',
id: author_id,
relationships: {
posts: {
data: [
{
id: post_id,
type: 'post'
},
{
id: 'cid_new_post',
type: 'post'
}
]
}
}
},
included: [
{
type: 'post',
id: post_id,
attributes: {
title: 'Updated title',
body: 'Updated body',
category_name: 'Updated category'
}
},
{
type: 'post',
id: 'cid_new_post',
attributes: {
title: 'New title',
body: 'New body',
category_name: 'New category'
}
}
]
}

expect(Post.first.title).to eq('Updated title')
expect(Post.first.body).to eq('Updated body')
expect(Post.first.category_name).to eq('Updated category')
expect(Post.last.title).to eq('New title')
expect(Post.last.body).to eq('New body')
expect(Post.last.category_name).to eq('New category')
expect do
patch_with_rails_fix :update, params: params, as: :json
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

it 'creates an author with a pair of sharp scissors' do
Expand Down
Loading