Skip to content

Commit 540bef3

Browse files
committed
Added an example for presenting collections.
1 parent 21e71e2 commit 540bef3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ get 'product/:id' do
4949
end
5050
```
5151

52+
Presenting collections works the same way.
53+
54+
```ruby
55+
module ProductsRepresenter
56+
include Roar::Representer::JSON
57+
include Roar::Representer::Feature::Hypermedia
58+
include Grape::Roar::Representer
59+
60+
collection :entries, extend: ProductPresenter, as: :products, embedded: true
61+
end
62+
```
63+
64+
```ruby
65+
get 'products' do
66+
present Product.all, with: ProductsRepresenter
67+
end
68+
```
69+
5270
### Accessing the Request Inside a Presenter
5371

5472
The formatter invokes `to_json` on presented objects and provides access to the requesting environment via the `env` option. The following example renders a full request URL in a presenter.

0 commit comments

Comments
 (0)