Skip to content

Support present hash #956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
0.12.0 (Next)
=============

#### Features

* [#956](https://github.com/intridea/grape/issues/956): Support `present` with `Grape::Presenters::Presenter` - [@u2](https://github.com/u2).

#### Fixes

* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,31 @@ Grape will automatically detect that there is a `Status::Entity` class and use t
representative entity. This can still be overridden by using the `:with` option or an explicit
`represents` call.

You can present `hash` with `Grape::Presenters::Presenter` to keep things consistent.

```ruby
get '/users' do
present { id: 10, name: :dgz }, with: Grape::Presenters::Presenter
end
````
The response will be

```ruby
{
id: 10,
name: 'dgz'
}
```

It has the same result with

```ruby
get '/users' do
present :id, 10
present :name, :dgz
end
```

### Hypermedia and Roar

You can use [Roar](https://github.com/apotonick/roar) to render HAL or Collection+JSON with the help of [grape-roar](https://github.com/dblock/grape-roar), which defines a custom JSON formatter and enables presenting entities with Grape's `present` keyword.
Expand Down
4 changes: 4 additions & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ module DSL
class API
autoload :Helpers, 'grape/api/helpers'
end

module Presenters
autoload :Presenter, 'grape/presenters/presenter'
end
end

require 'grape/validations/validators/base'
Expand Down
9 changes: 9 additions & 0 deletions lib/grape/presenters/presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Grape
module Presenters
class Presenter
def self.represent(object, options = {})
object
end
end
end
end
70 changes: 70 additions & 0 deletions spec/grape/presenters/presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'spec_helper'

module Grape
module Presenters
module InsideRouteSpec
class Dummy
include Grape::DSL::InsideRoute

attr_reader :env, :request, :new_settings

def initialize
@env = {}
@header = {}
@new_settings = { namespace_inheritable: {}, namespace_stackable: {} }
end
end
end

describe Presenter do
describe 'represent' do
let(:object_mock) do
Object.new
end

it 'represent object' do
expect(Presenter.represent(object_mock)).to eq object_mock
end
end

subject { InsideRouteSpec::Dummy.new }

describe 'present' do
let(:hash_mock) do
{ key: :value }
end

describe 'instance' do
before do
subject.present hash_mock, with: Grape::Presenters::Presenter
end
it 'presents dummy hash' do
expect(subject.body).to eq hash_mock
end
end

describe 'multiple presenter' do
let(:hash_mock1) do
{ key1: :value1 }
end

let(:hash_mock2) do
{ key2: :value2 }
end

describe 'instance' do
before do
subject.present hash_mock1, with: Grape::Presenters::Presenter
subject.present hash_mock2, with: Grape::Presenters::Presenter
end

it 'presents both dummy presenter' do
expect(subject.body[:key1]).to eq hash_mock1[:key1]
expect(subject.body[:key2]).to eq hash_mock2[:key2]
end
end
end
end
end
end
end