grape-active_model_serializers/spec/grape-active_model_serializers/endpoint_extension_spec.rb
Zander Hill a7ce076ec7 [Issue #13] Add render syntactic sugar
As in issue #13 an example solution provided by @jrhe an implementation
of said feature has been created.

As per the discussion in the thread this is only a helper to be able to
reach the available options provided in the active_model_serializer gem.

usage is as follows:

```ruby
get '/some_path' do
  collection = Collection.all
  render collection, { meta: { current_page: 5 }, meta_key:
    :pagination_info }
end
```

The return value would be:
`{ pagination_info: { current_page: 5 }, collection: [item, item] }`

If given without a `meta_key` it would return as:
`{ meta: { current_page: 5 }, collection: [item, item] }`

Any feedback appreciated.

@zph, @olleolleolle and @bjoska
2014-02-09 22:51:17 +00:00

39 lines
1.2 KiB
Ruby

require 'spec_helper'
describe 'Grape::EndpointExtension' do
subject { Grape::Endpoint.new(nil, {path: '/', method: 'foo'}) }
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
let(:user) do
Object.new do
def name
'sven'
end
end
end
let(:users) { [user, user] }
describe "#render" do
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)
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])
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
end
end
end
end