Use env to pass meta to the formatter.

This commit is contained in:
dB 2015-01-13 17:19:46 -05:00
parent 19bb6b50f7
commit f773bcbb8e
4 changed files with 11 additions and 29 deletions

View file

@ -36,7 +36,7 @@ module Grape
end end
def render(resources, meta = {}) def render(resources, meta = {})
Formatter::ActiveModelSerializers.meta = meta env['ams_meta'] = meta
resources resources
end end

View file

@ -22,26 +22,19 @@ module Grape
options[:scope] = endpoint unless options.key?(:scope) options[:scope] = endpoint unless options.key?(:scope)
# ensure we have an root to fallback on # ensure we have an root to fallback on
options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary) 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 end
def other_options def other_options(env)
options = {} options = {}
meta = Formatter::ActiveModelSerializers.meta.delete(:meta) ams_meta = env['ams_meta'] || {}
meta_key = Formatter::ActiveModelSerializers.meta.delete(:meta_key) 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_key if meta && meta_key
options[meta_key || :meta] = meta if meta options[meta_key || :meta] = meta if meta
options options
end end
def meta
@meta || {}
end
def meta=(value)
@meta = value
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].reduce(:merge)
end end

View file

@ -20,19 +20,22 @@ describe 'Grape::EndpointExtension' do
let(:users) { [user, user] } let(:users) { [user, user] }
describe '#render' do describe '#render' do
before do
allow(subject).to receive(:env).and_return({})
end
it { should respond_to(:render) } it { should respond_to(:render) }
let(:meta_content) { { total: 2 } } let(:meta_content) { { total: 2 } }
let(:meta_full) { { meta: meta_content } } let(:meta_full) { { meta: meta_content } }
context 'supplying meta' do context 'supplying meta' do
it 'passes through the Resource and uses given meta settings' 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) expect(subject.render(users, meta_full)).to eq(users)
end end
end end
context 'supplying meta and key' do context 'supplying meta and key' do
let(:meta_key) { { meta_key: :custom_key_name } } let(:meta_key) { { meta_key: :custom_key_name } }
it 'passes through the Resource and uses given meta settings' do 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) expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
end end
end end

View file

@ -3,20 +3,6 @@ require 'grape-active_model_serializers/formatter'
describe Grape::Formatter::ActiveModelSerializers do describe Grape::Formatter::ActiveModelSerializers do
subject { Grape::Formatter::ActiveModelSerializers } 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 describe '.fetch_serializer' do
let(:user) { User.new(first_name: 'John') } let(:user) { User.new(first_name: 'John') }