diff --git a/CHANGELOG.md b/CHANGELOG.md index 4274e08..b75eaa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/grape-active_model_serializers/endpoint_extension.rb b/lib/grape-active_model_serializers/endpoint_extension.rb index 7c21fdd..4965d00 100644 --- a/lib/grape-active_model_serializers/endpoint_extension.rb +++ b/lib/grape-active_model_serializers/endpoint_extension.rb @@ -43,7 +43,8 @@ def render(resources, extra_options = {}) :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 diff --git a/spec/grape-active_model_serializers/endpoint_extension_spec.rb b/spec/grape-active_model_serializers/endpoint_extension_spec.rb index d4378b0..b2ba568 100644 --- a/spec/grape-active_model_serializers/endpoint_extension_spec.rb +++ b/spec/grape-active_model_serializers/endpoint_extension_spec.rb @@ -16,45 +16,126 @@ 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 + + context 'meta_key' do + let(:options) { { meta_key: 'custom_meta' } } + 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 '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' } + + context 'adapter' do + let(:options) { { adapter: :json } } + it_behaves_like 'option capture' + end - before do - allow(subject).to receive(:env) { { meta: meta_full.merge(meta_key) } } + context 'include' do + let(:options) { { include: '*' } } + 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 '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