Skip to content
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

Roar decorator support #10

Closed
wants to merge 11 commits into from
Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Next
------------------

* [#10](https://github.com/dblock/grape-roar/pull/10): Support for Roar decorator - [@sdbondi](https://github.com/sdbondi).

0.1.0 (18/07/2014)
------------------

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ module ProductRepresenter
end
```

### Decorators

If you prefer to use a decorator class instead of modules.

```ruby
class ProductRepresenter < Grape::Roar::Decorator
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia

link :self do |opts|
"#{request(opts).url}/#{represented.id}"
end

private

def request(opts)
Grape::Request.new(opts[:env])
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am nitpicky I would make this request and then do request.url because URL is not the first or the last thing you might need inside the link, so request should be similar to represented.

end
```

```ruby
get 'products' do
present Product.all, with: ProductsRepresenter
end
```

Contributing
------------

Expand Down
1 change: 1 addition & 0 deletions lib/grape/roar.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'grape/roar/version'
require 'grape/roar/formatter'
require 'grape/roar/representer'
require 'grape/roar/decorator'
11 changes: 11 additions & 0 deletions lib/grape/roar/decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'roar/decorator'

module Grape
module Roar
class Decorator < ::Roar::Decorator
def self.represent(object, _options = {})
new(object)
end
end
end
end
29 changes: 29 additions & 0 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

describe Grape::Roar::Decorator do
subject do
Class.new(Grape::API)
end

before do
subject.format :json
subject.formatter :json, Grape::Formatter::Roar
end

def app
subject
end

context 'decorator' do
before do
subject.get('/user/:id') do
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
end
end

it 'returns a hypermedia representation' do
get '/user/666'
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
end
end
end
9 changes: 9 additions & 0 deletions spec/support/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class User
attr_accessor :id, :name

def initialize(attrs = {})
attrs.each_pair do |k, v|
send("#{k}=", v)
end
end
end
11 changes: 11 additions & 0 deletions spec/support/user_representer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UserRepresenter < Grape::Roar::Decorator
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia

property :name
property :id

link :self do
"/user/#{represented.id}"
end
end