mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Merge pull request #39 from jwkoelewijn/options_from_namespace
Look for namespace and other options to configure serializers
This commit is contained in:
commit
ca9c8faaa9
2 changed files with 26 additions and 3 deletions
|
|
@ -36,7 +36,7 @@ module Grape
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_options_from_endpoint(endpoint)
|
def build_options_from_endpoint(endpoint)
|
||||||
[endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options].reduce(:merge)
|
[endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options, endpoint.options, endpoint.options.fetch(:route_options)].reduce(:merge)
|
||||||
end
|
end
|
||||||
|
|
||||||
# array root is the innermost namespace name ('space') if there is one,
|
# array root is the innermost namespace name ('space') if there is one,
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,32 @@ require 'grape-active_model_serializers/formatter'
|
||||||
describe Grape::Formatter::ActiveModelSerializers do
|
describe Grape::Formatter::ActiveModelSerializers do
|
||||||
subject { Grape::Formatter::ActiveModelSerializers }
|
subject { Grape::Formatter::ActiveModelSerializers }
|
||||||
|
|
||||||
|
describe 'serializer options from namespace' do
|
||||||
|
let(:app) { Class.new(Grape::API) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
app.format :json
|
||||||
|
app.formatter :json, Grape::Formatter::ActiveModelSerializers
|
||||||
|
|
||||||
|
app.namespace('space') do |ns|
|
||||||
|
ns.get('/', root: false) do
|
||||||
|
{ user: { first_name: 'JR', last_name: 'HE' } }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should read serializer options like "root"' do
|
||||||
|
expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '.fetch_serializer' do
|
describe '.fetch_serializer' do
|
||||||
let(:user) { User.new(first_name: 'John') }
|
let(:user) { User.new(first_name: 'John') }
|
||||||
|
|
||||||
if Grape::Util.const_defined?('InheritableSetting')
|
if Grape::Util.const_defined?('InheritableSetting')
|
||||||
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo') }
|
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', root: false) }
|
||||||
else
|
else
|
||||||
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo') }
|
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', root: false) }
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:env) { { 'api.endpoint' => endpoint } }
|
let(:env) { { 'api.endpoint' => endpoint } }
|
||||||
|
|
@ -37,5 +56,9 @@ describe Grape::Formatter::ActiveModelSerializers do
|
||||||
expect(subject.instance_variable_get('@only')).to eq([:only])
|
expect(subject.instance_variable_get('@only')).to eq([:only])
|
||||||
expect(subject.instance_variable_get('@except')).to eq([:except])
|
expect(subject.instance_variable_get('@except')).to eq([:except])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should read serializer options like "root"' do
|
||||||
|
expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue