Darren Cheng 2017-04-23 16:35:01 -07:00
parent da18531590
commit 1b09c69174
3 changed files with 102 additions and 19 deletions

View file

@ -4,6 +4,7 @@
* Your contribution here.
* [#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).
* [#63](https://github.com/ruby-grape/grape-active_model_serializers/pull/63): Pass adapter options through render - [@drn](https://github.com/drn).

View file

@ -43,7 +43,8 @@ module Grape
:meta, :meta_key
)
env['ams_adapter'] = options.slice(
:adapter, :serializer, :each_serializer
:adapter, :serializer, :each_serializer, :include,
:fields, :key_transform, :links, :namespace
)
env['ams_extra'] = options[:extra]
resources

View file

@ -16,45 +16,126 @@ describe 'Grape::EndpointExtension' do
end
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
let(:user) do
let(:user) {
Object.new do
def name
'sven'
end
end
end
}
let(:users) { [user, user] }
describe '#render' do
let(:env) { {} }
let(:env_key) {}
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
it { should respond_to(:render) }
let(:meta_content) { { total: 2 } }
let(:meta_full) { { meta: meta_content } }
context 'supplying meta' do
before do
allow(subject).to receive(:env) { { meta: meta_full } }
context 'meta options' do
let(:env_key) { 'ams_meta' }
let(:meta_full) { { meta: meta_content } }
context 'meta' do
let(:options) { { meta: { total: 2 } } }
it_behaves_like 'option capture'
end
it 'passes through the Resource and uses given meta settings' do
expect(subject.render(users, meta_full)).to eq(users)
context 'meta_key' do
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
context 'supplying meta and key' do
let(:meta_key) { { meta_key: :custom_key_name } }
context 'adapter options' do
let(:options) { {} }
let(:env_key) { 'ams_adapter' }
before do
allow(subject).to receive(:env) { { meta: meta_full.merge(meta_key) } }
context 'adapter' do
let(:options) { { adapter: :json } }
it_behaves_like 'option capture'
end
it 'passes through the Resource and uses given meta settings' do
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
context 'include' do
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