mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Simplify meta assignment to a single hash.
This commit is contained in:
parent
5bba714457
commit
19bb6b50f7
4 changed files with 7 additions and 31 deletions
|
|
@ -36,8 +36,7 @@ module Grape
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(resources, meta = {})
|
def render(resources, meta = {})
|
||||||
Formatter::ActiveModelSerializers.meta = meta[:meta]
|
Formatter::ActiveModelSerializers.meta = meta
|
||||||
Formatter::ActiveModelSerializers.meta_key = meta[:meta_key]
|
|
||||||
resources
|
resources
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ module Grape
|
||||||
def other_options
|
def other_options
|
||||||
options = {}
|
options = {}
|
||||||
meta = Formatter::ActiveModelSerializers.meta.delete(:meta)
|
meta = Formatter::ActiveModelSerializers.meta.delete(:meta)
|
||||||
meta_key = Formatter::ActiveModelSerializers.meta_key.delete(:meta_key)
|
meta_key = Formatter::ActiveModelSerializers.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
|
||||||
|
|
@ -39,15 +39,7 @@ module Grape
|
||||||
end
|
end
|
||||||
|
|
||||||
def meta=(value)
|
def meta=(value)
|
||||||
@meta = value ? { meta: value } : nil
|
@meta = value
|
||||||
end
|
|
||||||
|
|
||||||
def meta_key
|
|
||||||
@meta_key || {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def meta_key=(key)
|
|
||||||
@meta_key = key ? { meta_key: key } : nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_options_from_endpoint(endpoint)
|
def build_options_from_endpoint(endpoint)
|
||||||
|
|
|
||||||
|
|
@ -25,15 +25,14 @@ describe 'Grape::EndpointExtension' do
|
||||||
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_content)
|
expect(serializer).to receive(:meta=).with(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_content)
|
expect(serializer).to receive(:meta=).with(meta_full.merge(meta_key))
|
||||||
expect(serializer).to receive(:meta_key=).with(meta_key[: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
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ describe Grape::Formatter::ActiveModelSerializers do
|
||||||
subject { Grape::Formatter::ActiveModelSerializers }
|
subject { Grape::Formatter::ActiveModelSerializers }
|
||||||
it { should respond_to(:meta) }
|
it { should respond_to(:meta) }
|
||||||
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
|
context '#meta' do
|
||||||
it 'will silently accept falsy input but return empty Hash' do
|
it 'will silently accept falsy input but return empty Hash' do
|
||||||
|
|
@ -15,20 +13,8 @@ describe Grape::Formatter::ActiveModelSerializers do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will wrap valid input in the meta: {} wrapper' do
|
it 'will wrap valid input in the meta: {} wrapper' do
|
||||||
subject.meta = { total: 2 }
|
subject.meta = { meta: { total: 2 } }
|
||||||
expect(subject.meta).to eq(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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue