Skip to content

Commit

Permalink
Merge pull request #956 from u2/present_hash
Browse files Browse the repository at this point in the history
Support present hash
  • Loading branch information
dblock committed Mar 19, 2015
2 parents f3e82d9 + 535db0a commit 44914da
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
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

0 comments on commit 44914da

Please sign in to comment.