mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-03-25 08:45:55 +00:00
Use env to pass meta to the formatter.
This commit is contained in:
parent
19bb6b50f7
commit
f773bcbb8e
4 changed files with 11 additions and 29 deletions
|
|
@ -36,7 +36,7 @@ module Grape
|
|||
end
|
||||
|
||||
def render(resources, meta = {})
|
||||
Formatter::ActiveModelSerializers.meta = meta
|
||||
env['ams_meta'] = meta
|
||||
resources
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -22,26 +22,19 @@ module Grape
|
|||
options[:scope] = endpoint unless options.key?(:scope)
|
||||
# ensure we have an root to fallback on
|
||||
options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
|
||||
serializer.new(resource, options.merge(other_options))
|
||||
serializer.new(resource, options.merge(other_options(env)))
|
||||
end
|
||||
|
||||
def other_options
|
||||
def other_options(env)
|
||||
options = {}
|
||||
meta = Formatter::ActiveModelSerializers.meta.delete(:meta)
|
||||
meta_key = Formatter::ActiveModelSerializers.meta.delete(:meta_key)
|
||||
ams_meta = env['ams_meta'] || {}
|
||||
meta = ams_meta.delete(:meta)
|
||||
meta_key = ams_meta.delete(:meta_key)
|
||||
options[:meta_key] = meta_key if meta && meta_key
|
||||
options[meta_key || :meta] = meta if meta
|
||||
options
|
||||
end
|
||||
|
||||
def meta
|
||||
@meta || {}
|
||||
end
|
||||
|
||||
def meta=(value)
|
||||
@meta = value
|
||||
end
|
||||
|
||||
def build_options_from_endpoint(endpoint)
|
||||
[endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options].reduce(:merge)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,19 +20,22 @@ describe 'Grape::EndpointExtension' do
|
|||
let(:users) { [user, user] }
|
||||
|
||||
describe '#render' do
|
||||
before do
|
||||
allow(subject).to receive(:env).and_return({})
|
||||
end
|
||||
it { should respond_to(:render) }
|
||||
let(:meta_content) { { total: 2 } }
|
||||
let(:meta_full) { { meta: meta_content } }
|
||||
context 'supplying meta' do
|
||||
it 'passes through the Resource and uses given meta settings' do
|
||||
expect(serializer).to receive(:meta=).with(meta_full)
|
||||
allow(subject).to receive(:env).and_return(meta: meta_full)
|
||||
expect(subject.render(users, meta_full)).to eq(users)
|
||||
end
|
||||
end
|
||||
context 'supplying meta and key' do
|
||||
let(:meta_key) { { meta_key: :custom_key_name } }
|
||||
it 'passes through the Resource and uses given meta settings' do
|
||||
expect(serializer).to receive(:meta=).with(meta_full.merge(meta_key))
|
||||
allow(subject).to receive(:env).and_return(meta: meta_full.merge(meta_key))
|
||||
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,20 +3,6 @@ require 'grape-active_model_serializers/formatter'
|
|||
|
||||
describe Grape::Formatter::ActiveModelSerializers do
|
||||
subject { Grape::Formatter::ActiveModelSerializers }
|
||||
it { should respond_to(:meta) }
|
||||
it { should respond_to(:meta=) }
|
||||
|
||||
context '#meta' do
|
||||
it 'will silently accept falsy input but return empty Hash' do
|
||||
subject.meta = nil
|
||||
expect(subject.meta).to eq({})
|
||||
end
|
||||
|
||||
it 'will wrap valid input in the meta: {} wrapper' do
|
||||
subject.meta = { meta: { total: 2 } }
|
||||
expect(subject.meta).to eq({ meta: { total: 2 } })
|
||||
end
|
||||
end
|
||||
|
||||
describe '.fetch_serializer' do
|
||||
let(:user) { User.new(first_name: 'John') }
|
||||
|
|
|
|||
Loading…
Reference in a new issue