Merge pull request #40 from dblock/one-meta

Use env to pass AMS meta around.
This commit is contained in:
Daniel Doubrovkine (dB.) @dblockdotorg 2015-01-15 11:16:59 -05:00
commit f13d008506
4 changed files with 11 additions and 53 deletions

View file

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

View file

@ -22,34 +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_key.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 ? { meta: value } : nil
end
def meta_key
@meta_key || {}
end
def meta_key=(key)
@meta_key = key ? { meta_key: key } : nil
end
def build_options_from_endpoint(endpoint)
[endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options].reduce(:merge)
end

View file

@ -20,20 +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_content)
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_content)
expect(serializer).to receive(:meta_key=).with(meta_key[: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

View file

@ -3,34 +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=) }
it { should respond_to(:meta_key) }
it { should respond_to(:meta_key=) }
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 = { total: 2 }
expect(subject.meta).to eq(meta: { total: 2 })
end
end
context '#meta_key' do
it 'will silently accept falsy input but return empty Hash' do
subject.meta_key = nil
expect(subject.meta_key).to eq({})
end
it 'will wrap valid input in the meta_key: {} wrapper' do
subject.meta_key = :custom_key_name
expect(subject.meta_key).to eq(meta_key: :custom_key_name)
end
end
describe '.fetch_serializer' do
let(:user) { User.new(first_name: 'John') }