Skip to content

Commit

Permalink
Whitelist adapter options and support extra options.
Browse files Browse the repository at this point in the history
  • Loading branch information
drn committed Sep 9, 2016
1 parent 4315930 commit 0963774
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ resource :users do
get '/:id' do
if conditional
# uses UserSerializer and configured default adapter automatically
user
current_user
else
# uses specified serializer and adapter
render current_user, serializer: ErrorSerializer, adapter: :attributes
Expand All @@ -186,6 +186,21 @@ resource :users do
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

```ruby
Expand Down
11 changes: 8 additions & 3 deletions lib/grape-active_model_serializers/endpoint_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ def serialization_scope
end

def render(resources, extra_options = {})
extra_options.symbolize_keys!
env['ams_meta'] = extra_options.slice(:meta, :meta_key)
env['ams_adapter_options'] = extra_options.except(:meta, :meta_key)
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
end

Expand Down
15 changes: 11 additions & 4 deletions lib/grape-active_model_serializers/options_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def options
options.merge!(default_root_options) unless options.key?(:root)
options.merge!(meta_options)
options.merge!(adapter_options)
options.merge!(extra_options)
options
)
end
Expand Down Expand Up @@ -57,16 +58,22 @@ def innermost_scope

def meta_options
options = {}
ams_meta = env['ams_meta'] || {}
meta = ams_meta[:meta]
meta_key = ams_meta[:meta_key]
meta_options = env['ams_meta'] || {}
meta = meta_options[:meta]
meta_key = meta_options[:meta_key]
options[:meta] = meta if meta
options[:meta_key] = meta_key if meta && meta_key
options
end

def adapter_options
env['ams_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
Expand Down
35 changes: 34 additions & 1 deletion spec/grape/active_model_serializers/options_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
end

context 'adapter options' do
let(:env) { super().merge('ams_adapter_options' => adapter_options) }
let(:env) { super().merge('ams_adapter' => adapter_options) }
let(:adapter_options) { {} }

context 'adapter' do
Expand All @@ -91,6 +91,39 @@
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

0 comments on commit 0963774

Please sign in to comment.