mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Pass adapter options through calls to render.
This commit is contained in:
parent
f304c8e487
commit
4315930a55
6 changed files with 124 additions and 6 deletions
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
### 1.5.1 (Next)
|
### 1.5.1 (Next)
|
||||||
|
|
||||||
* [#65](https://github.com/ruby-grape/grape-active_model_serializers/pull/65): Added Danger, PR linter - [@dblock](https://github.com/dblock).
|
|
||||||
* Your contribution here.
|
* Your contribution here.
|
||||||
|
* [#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).
|
||||||
|
|
||||||
### 1.5.0 (August 24, 2016)
|
### 1.5.0 (August 24, 2016)
|
||||||
|
|
||||||
|
|
|
||||||
19
README.md
19
README.md
|
|
@ -150,10 +150,10 @@ Or as follows.
|
||||||
|
|
||||||
ActiveModelSerializer will fetch automatically the right serializer to render.
|
ActiveModelSerializer will fetch automatically the right serializer to render.
|
||||||
|
|
||||||
### Manually specifying serializer options
|
### Manually specifying serializer / adapter options
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# Serializer options can be specified on routes or namespaces.
|
# Serializer and adapter options can be specified on routes or namespaces.
|
||||||
namespace 'foo', serializer: BarSerializer do
|
namespace 'foo', serializer: BarSerializer do
|
||||||
get "/" do
|
get "/" do
|
||||||
# will use "bar" serializer
|
# will use "bar" serializer
|
||||||
|
|
@ -171,6 +171,21 @@ namespace 'foo', serializer: BarSerializer do
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# Serializer and adapter options can also be specified in the body of the route
|
||||||
|
resource :users do
|
||||||
|
get '/:id' do
|
||||||
|
if conditional
|
||||||
|
# uses UserSerializer and configured default adapter automatically
|
||||||
|
user
|
||||||
|
else
|
||||||
|
# uses specified serializer and adapter
|
||||||
|
render current_user, serializer: ErrorSerializer, adapter: :attributes
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
### Custom Metadata
|
### Custom Metadata
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,10 @@ module Grape
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(resources, meta = {})
|
def render(resources, extra_options = {})
|
||||||
env['ams_meta'] = meta
|
extra_options.symbolize_keys!
|
||||||
|
env['ams_meta'] = extra_options.slice(:meta, :meta_key)
|
||||||
|
env['ams_adapter_options'] = extra_options.except(:meta, :meta_key)
|
||||||
resources
|
resources
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ module Grape
|
||||||
options[:scope] = endpoint unless options.key?(:scope)
|
options[:scope] = endpoint unless options.key?(:scope)
|
||||||
options.merge!(default_root_options) unless options.key?(:root)
|
options.merge!(default_root_options) unless options.key?(:root)
|
||||||
options.merge!(meta_options)
|
options.merge!(meta_options)
|
||||||
|
options.merge!(adapter_options)
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
@ -63,6 +64,10 @@ module Grape
|
||||||
options[:meta_key] = meta_key if meta && meta_key
|
options[:meta_key] = meta_key if meta && meta_key
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def adapter_options
|
||||||
|
env['ams_adapter_options'] || {}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
96
spec/grape/active_model_serializers/options_builder_spec.rb
Normal file
96
spec/grape/active_model_serializers/options_builder_spec.rb
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Grape::ActiveModelSerializers::OptionsBuilder do
|
||||||
|
let(:resolver) { described_class.new(resource, env) }
|
||||||
|
let(:resource) { User.new }
|
||||||
|
let(:env) { { 'api.endpoint' => UsersApi.endpoints.first } }
|
||||||
|
|
||||||
|
context '#options' do
|
||||||
|
let(:options) { resolver.options }
|
||||||
|
|
||||||
|
context 'meta options' do
|
||||||
|
let(:env) { super().merge('ams_meta' => meta_options) }
|
||||||
|
let(:meta_options) {
|
||||||
|
{
|
||||||
|
meta: meta,
|
||||||
|
meta_key: meta_key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let(:meta) { { sample: 'metadata' } }
|
||||||
|
let(:meta_key) { 'specified_key' }
|
||||||
|
|
||||||
|
context 'meta option set' do
|
||||||
|
context 'meta_key set' do
|
||||||
|
it 'includes meta' do
|
||||||
|
expect(options[:meta]).to eq(meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'includes meta_key' do
|
||||||
|
expect(options[:meta_key]).to eq(meta_key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'meta_key unset' do
|
||||||
|
let(:meta_key) { nil }
|
||||||
|
|
||||||
|
it 'includes meta' do
|
||||||
|
expect(options[:meta]).to eq(meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not include meta_key' do
|
||||||
|
expect(options.keys).not_to include(:meta_key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'meta option unset' do
|
||||||
|
let(:meta) { nil }
|
||||||
|
|
||||||
|
context 'meta_key set' do
|
||||||
|
it 'does not include meta' do
|
||||||
|
expect(options.keys).not_to include(:meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not include meta_key' do
|
||||||
|
expect(options.keys).not_to include(:meta_key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'meta_key unset' do
|
||||||
|
let(:meta_key) { nil }
|
||||||
|
|
||||||
|
it 'does not include meta' do
|
||||||
|
expect(options.keys).not_to include(:meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not include meta_key' do
|
||||||
|
expect(options.keys).not_to include(:meta_key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'adapter options' do
|
||||||
|
let(:env) { super().merge('ams_adapter_options' => adapter_options) }
|
||||||
|
let(:adapter_options) { {} }
|
||||||
|
|
||||||
|
context 'adapter' do
|
||||||
|
let(:adapter_options) { { adapter: adapter } }
|
||||||
|
let(:adapter) { :attributes }
|
||||||
|
|
||||||
|
it 'includes adapter as top-level option' do
|
||||||
|
expect(options[:adapter]).to eq(adapter)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'serializer' do
|
||||||
|
let(:adapter_options) { { serializer: serializer } }
|
||||||
|
let(:serializer) { V2::UserSerializer }
|
||||||
|
|
||||||
|
it 'includes serializer as top-level option' do
|
||||||
|
expect(options[:serializer]).to eq(serializer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
require 'pry'
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
# asserts serializer resolution order:
|
# asserts serializer resolution order:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue