Skip to content

Commit 4e1a489

Browse files
committed
Merge pull request #11 from fixate/feature/decorator_support_squashed
Added support for Roar decorators
2 parents 86a4ffd + 470b266 commit 4e1a489

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Next
2+
------------------
3+
4+
* [#10](https://github.com/dblock/grape-roar/pull/10): Support for Roar decorator - [@sdbondi](https://github.com/sdbondi).
5+
16
0.1.0 (18/07/2014)
27
------------------
38

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ module ProductRepresenter
8989
end
9090
```
9191

92+
### Decorators
93+
94+
If you prefer to use a decorator class instead of modules.
95+
96+
```ruby
97+
class ProductRepresenter < Grape::Roar::Decorator
98+
include Roar::Representer::JSON
99+
include Roar::Representer::Feature::Hypermedia
100+
101+
link :self do |opts|
102+
"#{request(opts).url}/#{represented.id}"
103+
end
104+
105+
private
106+
107+
def request(opts)
108+
Grape::Request.new(opts[:env])
109+
end
110+
end
111+
```
112+
113+
```ruby
114+
get 'products' do
115+
present Product.all, with: ProductsRepresenter
116+
end
117+
```
118+
92119
Contributing
93120
------------
94121

lib/grape/roar.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'grape/roar/version'
22
require 'grape/roar/formatter'
33
require 'grape/roar/representer'
4+
require 'grape/roar/decorator'

lib/grape/roar/decorator.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'roar/decorator'
2+
3+
module Grape
4+
module Roar
5+
class Decorator < ::Roar::Decorator
6+
def self.represent(object, _options = {})
7+
new(object)
8+
end
9+
end
10+
end
11+
end

spec/decorator_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Roar::Decorator do
4+
subject do
5+
Class.new(Grape::API)
6+
end
7+
8+
before do
9+
subject.format :json
10+
subject.formatter :json, Grape::Formatter::Roar
11+
end
12+
13+
def app
14+
subject
15+
end
16+
17+
context 'decorator' do
18+
before do
19+
subject.get('/user/:id') do
20+
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
21+
end
22+
end
23+
24+
it 'returns a hypermedia representation' do
25+
get '/user/666'
26+
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
27+
end
28+
end
29+
end

spec/support/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class User
2+
attr_accessor :id, :name
3+
4+
def initialize(attrs = {})
5+
attrs.each_pair do |k, v|
6+
send("#{k}=", v)
7+
end
8+
end
9+
end

spec/support/user_representer.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class UserRepresenter < Grape::Roar::Decorator
2+
include Roar::Representer::JSON
3+
include Roar::Representer::Feature::Hypermedia
4+
5+
property :name
6+
property :id
7+
8+
link :self do
9+
"/user/#{represented.id}"
10+
end
11+
end

0 commit comments

Comments
 (0)