mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Ensure all AMS options are passed through.
See the following for details: * https://github.com/rails-api/active_model_serializers/blob/master/docs/general/rendering.md * https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md
This commit is contained in:
parent
da18531590
commit
1b09c69174
3 changed files with 102 additions and 19 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
* Your contribution here.
|
* Your contribution here.
|
||||||
* [#74](https://github.com/ruby-grape/grape-active_model_serializers/pull/74): Add support for Sequel - [@drn](https://github.com/drn).
|
* [#74](https://github.com/ruby-grape/grape-active_model_serializers/pull/74): Add support for Sequel - [@drn](https://github.com/drn).
|
||||||
|
* [#73](https://github.com/ruby-grape/grape-active_model_serializers/pull/73): Ensure all AMS options are passed through - [@drn](https://github.com/drn).
|
||||||
* [#65](https://github.com/ruby-grape/grape-active_model_serializers/pull/65): Added Danger, PR linter - [@dblock](https://github.com/dblock).
|
* [#65](https://github.com/ruby-grape/grape-active_model_serializers/pull/65): Added Danger, PR linter - [@dblock](https://github.com/dblock).
|
||||||
* [#63](https://github.com/ruby-grape/grape-active_model_serializers/pull/63): Pass adapter options through render - [@drn](https://github.com/drn).
|
* [#63](https://github.com/ruby-grape/grape-active_model_serializers/pull/63): Pass adapter options through render - [@drn](https://github.com/drn).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,8 @@ module Grape
|
||||||
:meta, :meta_key
|
:meta, :meta_key
|
||||||
)
|
)
|
||||||
env['ams_adapter'] = options.slice(
|
env['ams_adapter'] = options.slice(
|
||||||
:adapter, :serializer, :each_serializer
|
:adapter, :serializer, :each_serializer, :include,
|
||||||
|
:fields, :key_transform, :links, :namespace
|
||||||
)
|
)
|
||||||
env['ams_extra'] = options[:extra]
|
env['ams_extra'] = options[:extra]
|
||||||
resources
|
resources
|
||||||
|
|
|
||||||
|
|
@ -16,45 +16,126 @@ describe 'Grape::EndpointExtension' do
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
||||||
|
let(:user) {
|
||||||
let(:user) do
|
|
||||||
Object.new do
|
Object.new do
|
||||||
def name
|
def name
|
||||||
'sven'
|
'sven'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
}
|
||||||
|
|
||||||
let(:users) { [user, user] }
|
let(:users) { [user, user] }
|
||||||
|
|
||||||
describe '#render' do
|
describe '#render' do
|
||||||
|
let(:env) { {} }
|
||||||
|
let(:env_key) {}
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(subject).to receive(:env).and_return({})
|
allow(subject).to receive(:env).and_return(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples_for 'option capture' do
|
||||||
|
it 'captures options' do
|
||||||
|
subject.render(users, options)
|
||||||
|
expect(env[env_key]).to eq(options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples_for 'skipped option capture' do
|
||||||
|
it 'does not capture options' do
|
||||||
|
subject.render(users, options)
|
||||||
|
expect(env[env_key]).to eq({})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should respond_to(:render) }
|
it { should respond_to(:render) }
|
||||||
let(:meta_content) { { total: 2 } }
|
|
||||||
let(:meta_full) { { meta: meta_content } }
|
|
||||||
|
|
||||||
context 'supplying meta' do
|
context 'meta options' do
|
||||||
before do
|
let(:env_key) { 'ams_meta' }
|
||||||
allow(subject).to receive(:env) { { meta: meta_full } }
|
let(:meta_full) { { meta: meta_content } }
|
||||||
|
|
||||||
|
context 'meta' do
|
||||||
|
let(:options) { { meta: { total: 2 } } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'passes through the Resource and uses given meta settings' do
|
context 'meta_key' do
|
||||||
expect(subject.render(users, meta_full)).to eq(users)
|
let(:options) { { meta_key: 'custom_meta' } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'unknown option' do
|
||||||
|
let(:options) { { unknown: 'value' } }
|
||||||
|
it_behaves_like 'skipped option capture'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'supplying meta and key' do
|
context 'adapter options' do
|
||||||
let(:meta_key) { { meta_key: :custom_key_name } }
|
let(:options) { {} }
|
||||||
|
let(:env_key) { 'ams_adapter' }
|
||||||
|
|
||||||
before do
|
context 'adapter' do
|
||||||
allow(subject).to receive(:env) { { meta: meta_full.merge(meta_key) } }
|
let(:options) { { adapter: :json } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'passes through the Resource and uses given meta settings' do
|
context 'include' do
|
||||||
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
|
let(:options) { { include: '*' } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'fields' do
|
||||||
|
let(:options) { { fields: [:id] } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'key_transform' do
|
||||||
|
let(:options) { { key_transform: :camel_lower } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'links' do
|
||||||
|
let(:links_object) {
|
||||||
|
{
|
||||||
|
href: 'http://example.com/api/posts',
|
||||||
|
meta: {
|
||||||
|
count: 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let(:options) { { links: links_object } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'namespace' do
|
||||||
|
let(:options) { { namespace: V4 } }
|
||||||
|
it_behaves_like 'option capture'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'unknown option' do
|
||||||
|
let(:options) { { unknown: 'value' } }
|
||||||
|
it_behaves_like 'skipped option capture'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'extra options' do
|
||||||
|
let(:env_key) { 'ams_extra' }
|
||||||
|
|
||||||
|
context 'namespace' do
|
||||||
|
let(:options) { { extra: { option: 'value' } } }
|
||||||
|
|
||||||
|
it 'captures options' do
|
||||||
|
subject.render(users, options)
|
||||||
|
expect(env[env_key]).to eq(options[:extra])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'unknown option' do
|
||||||
|
let(:options) { { unknown: 'value' } }
|
||||||
|
|
||||||
|
it 'does not capture options' do
|
||||||
|
subject.render(users, options)
|
||||||
|
expect(env[env_key]).to eq(nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue