forked from ruby-grape/grape-active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue ruby-grape#13] Add render syntactic sugar
As in issue ruby-grape#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
- Loading branch information
Showing
5 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
spec/features/grape-active_model_serializers/render_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'spec_helper' | ||
require 'support/models/user' | ||
require 'support/serializers/user_serializer' | ||
require 'grape-active_model_serializers' | ||
require 'securerandom' | ||
|
||
describe '#render' do | ||
let(:app) { Class.new(Grape::API) } | ||
|
||
before do | ||
app.format :json | ||
app.formatter :json, Grape::Formatter::ActiveModelSerializers | ||
end | ||
|
||
def get_resource_with(meta) | ||
url = "/#{SecureRandom.hex}" | ||
app.get(url) do | ||
user = User.new(first_name: 'Jeff') | ||
render [user, user], meta | ||
end | ||
get url | ||
JSON.parse last_response.body | ||
end | ||
|
||
context 'with meta key' do | ||
it 'includes meta key and content' do | ||
result = get_resource_with({ meta: { total: 2 }}) | ||
expect(result).to have_key('meta') | ||
expect(result.fetch('meta')).to eq({ 'total' => 2 }) | ||
end | ||
end | ||
|
||
context 'with a custom meta_key' do | ||
|
||
it 'includes the custom meta key name' do | ||
result = get_resource_with({ meta: { total: 2 }, meta_key: :custom_key_name }) | ||
expect(result).to have_key('custom_key_name') | ||
expect(result.fetch('custom_key_name')).to eq({ 'total' => 2 }) | ||
end | ||
|
||
it 'ignores a lonely meta_key' do | ||
result = get_resource_with({ meta_key: :custom_key_name }) | ||
expect(result).not_to have_key('meta') | ||
expect(result).not_to have_key('custom_key_name') | ||
end | ||
end | ||
|
||
context 'junk keys' do | ||
|
||
it 'ignores junk keys' do | ||
result = get_resource_with({ junk_key: { total: 2 } }) | ||
expect(result).not_to have_key('junk_key') | ||
end | ||
|
||
it 'ignores empty meta_key' do | ||
result = get_resource_with({ meta: { total: 2 }, meta_key: nil }) | ||
expect(result).to have_key('meta') | ||
end | ||
|
||
it 'ignores empty meta' do | ||
result = get_resource_with({ meta: nil }) | ||
expect(result).not_to have_key('meta') | ||
end | ||
|
||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
spec/grape-active_model_serializers/endpoint_extension_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters