mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Merge pull request #63 from ruby-grape/adapter-options
Pass adapter options through calls to render.
This commit is contained in:
commit
2748d5ba2b
6 changed files with 187 additions and 9 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)
|
||||||
|
|
||||||
|
|
|
||||||
34
README.md
34
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,36 @@ 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
|
||||||
|
current_user
|
||||||
|
else
|
||||||
|
# uses specified serializer and adapter
|
||||||
|
render current_user, serializer: ErrorSerializer, adapter: :attributes
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# Adhoc serializer options can be specified in the body of the route
|
||||||
|
resource :users do
|
||||||
|
get '/:id' do
|
||||||
|
render current_user, extra: { adhoc_name_option: 'value' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class UserSerializer
|
||||||
|
def name
|
||||||
|
instance_options[:adhoc_name_option] # accessible in instance_options
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
### Custom Metadata
|
### Custom Metadata
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,15 @@ module Grape
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(resources, meta = {})
|
def render(resources, extra_options = {})
|
||||||
env['ams_meta'] = meta
|
options = extra_options.symbolize_keys
|
||||||
|
env['ams_meta'] = options.slice(
|
||||||
|
:meta, :meta_key
|
||||||
|
)
|
||||||
|
env['ams_adapter'] = options.slice(
|
||||||
|
:adapter, :serializer, :each_serializer
|
||||||
|
)
|
||||||
|
env['ams_extra'] = options[:extra]
|
||||||
resources
|
resources
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ 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.merge!(extra_options)
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
@ -56,13 +58,23 @@ module Grape
|
||||||
|
|
||||||
def meta_options
|
def meta_options
|
||||||
options = {}
|
options = {}
|
||||||
ams_meta = env['ams_meta'] || {}
|
meta_options = env['ams_meta'] || {}
|
||||||
meta = ams_meta[:meta]
|
meta = meta_options[:meta]
|
||||||
meta_key = ams_meta[:meta_key]
|
meta_key = meta_options[:meta_key]
|
||||||
options[:meta] = meta if meta
|
options[:meta] = meta if meta
|
||||||
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'] || {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def extra_options
|
||||||
|
options = env['ams_extra'] || {}
|
||||||
|
return options if options.is_a?(Hash)
|
||||||
|
raise 'Extra options must be a hash'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
129
spec/grape/active_model_serializers/options_builder_spec.rb
Normal file
129
spec/grape/active_model_serializers/options_builder_spec.rb
Normal file
|
|
@ -0,0 +1,129 @@
|
||||||
|
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' => 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
|
||||||
|
|
||||||
|
context 'each_serializer' do
|
||||||
|
let(:adapter_options) { { each_serializer: each_serializer } }
|
||||||
|
let(:each_serializer) { V2::UserSerializer }
|
||||||
|
|
||||||
|
it 'includes each_serializer as top-level option' do
|
||||||
|
expect(options[:each_serializer]).to eq(each_serializer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'extra options' do
|
||||||
|
let(:env) { super().merge('ams_extra' => extra_options) }
|
||||||
|
|
||||||
|
context 'hash' do
|
||||||
|
let(:extra_options) { { extra: 'info' } }
|
||||||
|
|
||||||
|
it 'includes extra options in top-level options' do
|
||||||
|
expect(options.keys).to include(:extra)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'not a hash' do
|
||||||
|
let(:extra_options) { 'extra' }
|
||||||
|
|
||||||
|
it 'raises an exception' do
|
||||||
|
expect {
|
||||||
|
options
|
||||||
|
}.to raise_exception(
|
||||||
|
'Extra options must be a hash'
|
||||||
|
)
|
||||||
|
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