Skip to content

Commit 535db0a

Browse files
committed
Support present with Grape::Presenters:Presenter
1 parent f3e82d9 commit 535db0a

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
0.12.0 (Next)
22
=============
33

4+
#### Features
5+
6+
* [#956](https://github.com/intridea/grape/issues/956): Support `present` with `Grape::Presenters::Presenter` - [@u2](https://github.com/u2).
7+
48
#### Fixes
59

610
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,31 @@ Grape will automatically detect that there is a `Status::Entity` class and use t
19391939
representative entity. This can still be overridden by using the `:with` option or an explicit
19401940
`represents` call.
19411941

1942+
You can present `hash` with `Grape::Presenters::Presenter` to keep things consistent.
1943+
1944+
```ruby
1945+
get '/users' do
1946+
present { id: 10, name: :dgz }, with: Grape::Presenters::Presenter
1947+
end
1948+
````
1949+
The response will be
1950+
1951+
```ruby
1952+
{
1953+
id: 10,
1954+
name: 'dgz'
1955+
}
1956+
```
1957+
1958+
It has the same result with
1959+
1960+
```ruby
1961+
get '/users' do
1962+
present :id, 10
1963+
present :name, :dgz
1964+
end
1965+
```
1966+
19421967
### Hypermedia and Roar
19431968

19441969
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.

lib/grape.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ module DSL
127127
class API
128128
autoload :Helpers, 'grape/api/helpers'
129129
end
130+
131+
module Presenters
132+
autoload :Presenter, 'grape/presenters/presenter'
133+
end
130134
end
131135

132136
require 'grape/validations/validators/base'

lib/grape/presenters/presenter.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Grape
2+
module Presenters
3+
class Presenter
4+
def self.represent(object, options = {})
5+
object
6+
end
7+
end
8+
end
9+
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require 'spec_helper'
2+
3+
module Grape
4+
module Presenters
5+
module InsideRouteSpec
6+
class Dummy
7+
include Grape::DSL::InsideRoute
8+
9+
attr_reader :env, :request, :new_settings
10+
11+
def initialize
12+
@env = {}
13+
@header = {}
14+
@new_settings = { namespace_inheritable: {}, namespace_stackable: {} }
15+
end
16+
end
17+
end
18+
19+
describe Presenter do
20+
describe 'represent' do
21+
let(:object_mock) do
22+
Object.new
23+
end
24+
25+
it 'represent object' do
26+
expect(Presenter.represent(object_mock)).to eq object_mock
27+
end
28+
end
29+
30+
subject { InsideRouteSpec::Dummy.new }
31+
32+
describe 'present' do
33+
let(:hash_mock) do
34+
{ key: :value }
35+
end
36+
37+
describe 'instance' do
38+
before do
39+
subject.present hash_mock, with: Grape::Presenters::Presenter
40+
end
41+
it 'presents dummy hash' do
42+
expect(subject.body).to eq hash_mock
43+
end
44+
end
45+
46+
describe 'multiple presenter' do
47+
let(:hash_mock1) do
48+
{ key1: :value1 }
49+
end
50+
51+
let(:hash_mock2) do
52+
{ key2: :value2 }
53+
end
54+
55+
describe 'instance' do
56+
before do
57+
subject.present hash_mock1, with: Grape::Presenters::Presenter
58+
subject.present hash_mock2, with: Grape::Presenters::Presenter
59+
end
60+
61+
it 'presents both dummy presenter' do
62+
expect(subject.body[:key1]).to eq hash_mock1[:key1]
63+
expect(subject.body[:key2]).to eq hash_mock2[:key2]
64+
end
65+
end
66+
end
67+
end
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)