Skip to content

Commit

Permalink
Ensure all AMS options are passed through.
Browse files Browse the repository at this point in the history
  • Loading branch information
drn committed Apr 26, 2017
1 parent da18531 commit 1b09c69
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
3 changes: 2 additions & 1 deletion lib/grape-active_model_serializers/endpoint_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
117 changes: 99 additions & 18 deletions spec/grape-active_model_serializers/endpoint_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b09c69

Please sign in to comment.