Skip to content

Delegate to Roar's represent w/o options argument #18

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ rvm:
- 2.0.0
- 1.9.3
- jruby-19mode
- rbx-2.2.10
- rbx-2

matrix:
allow_failures:
- rvm: rbx-2
2 changes: 1 addition & 1 deletion lib/grape/roar/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Grape
module Roar
class Decorator < ::Roar::Decorator
def self.represent(object, _options = {})
new(object)
super(object)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/grape/roar/representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module Grape
module Roar
module Representer
def self.included(base)
super
base.send(:include, ::Roar::Representer)
base.extend(ClassMethods)
end

module ClassMethods
def represent(object, _options = {})
object.extend self
object
super(object)
end
end
end
Expand Down
30 changes: 24 additions & 6 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,33 @@ def app
end

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

it 'returns a single hypermedia representation' do
get '/user/666'
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
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"}]}'
context 'with an array of resources' do
before do
subject.get('/users') do
present [User.new(name: 'Texassee', id: 1), User.new(name: 'Lonestar', id: 2)], with: UserRepresenter
end
end

it 'returns an array of hypermedia representations' do
get 'users'
expect(last_response.body).to eq [
{ 'name' => 'Texassee', 'id' => 1, 'links' => [{ 'rel' => 'self', 'href' => '/user/1' }] },
{ 'name' => 'Lonestar', 'id' => 2, 'links' => [{ 'rel' => 'self', 'href' => '/user/2' }] }
].to_json
end
end
end
end
3 changes: 2 additions & 1 deletion spec/nested_representer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def app

it 'returns a hypermedia representation' do
get '/order/666'
expect(last_response.body).to eq '{"id":"666","client_id":42,"articles":[{"title":"One","id":1,"links":[{"rel":"self","href":"/article/1"}]},{"title":"Two","id":2,"links":[{"rel":"self","href":"/article/2"}]}],"links":[{"rel":"self","href":"/order/666"},{"rel":"items","href":"/order/666/items"}]}'
expect(last_response.body).to eq '{"id":"666","client_id":42,"articles":[{"title":"One","id":1,"links":[{"rel":"self","href":"/article/1"}]},{"title":"Two","id":2,"links":[{"rel":"self","href":"/article/2"}]}],' \
'"links":[{"rel":"self","href":"http://example.org/order/666"},{"rel":"items","href":"/order/666/items"}]}'
end
end
end
43 changes: 37 additions & 6 deletions spec/present_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,46 @@ def app
end

context 'using present' do
before do
subject.get('/product/:id') do
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
context 'with a single resource' do
before do
subject.get('/product/:id') do
present Product.new(title: 'Lonestar', id: params[:id]), with: ProductRepresenter
end
end

it 'returns a hypermedia representation' do
get '/product/666'
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"/product/666"}]}'
end
end

context 'with an array of resources' do
before do
subject.get('/products') do
present [Product.new(title: 'Texassee', id: 1), Product.new(title: 'Lonestar', id: 2)], with: ProductRepresenter
end
end

it 'returns an array of hypermedia representations' do
get 'products'
expect(last_response.body).to eq '[{"title":"Texassee","id":1,"links":[{"rel":"self","href":"/product/1"}]},{"title":"Lonestar","id":2,"links":[{"rel":"self","href":"/product/2"}]}]'
end
end

it 'returns a hypermedia representation' do
get '/product/666'
expect(last_response.body).to eq '{"title":"Lonestar","id":"666","links":[{"rel":"self","href":"http://example.org/product/666"}]}'
context 'with an array of resources as a resource' do
Copy link
Author

Choose a reason for hiding this comment

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

@dblock - failing spec here

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I'll try to spend some time on this.

before do
subject.get('/products') do
present [Product.new(title: 'Texassee', id: 1), Product.new(title: 'Lonestar', id: 2)], with: ProductsRepresenter
end
end

it 'returns an array of hypermedia representations' do
get 'products'
expect(last_response.body).to eq [
{ 'title' => 'Texassee', 'id' => 1, 'links' => [{ 'rel' => 'self', 'href' => '/product/1' }] },
{ 'title' => 'Lonestar', 'id' => 2, 'links' => [{ 'rel' => 'self', 'href' => '/product/2' }] }
]
end
end
end
end
5 changes: 3 additions & 2 deletions spec/support/order_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module OrderRepresenter

collection :articles, class: Article

link :self do
"/order/#{id}"
link :self do |opts|
request = Grape::Request.new(opts[:env])
"#{request.url}"
end

link :items do
Expand Down
5 changes: 2 additions & 3 deletions spec/support/product_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module ProductRepresenter
property :title
property :id

link :self do |opts|
request = Grape::Request.new(opts[:env])
"#{request.url}"
link :self do
"/product/#{id}"
end
end
10 changes: 10 additions & 0 deletions spec/support/products_representer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'roar/json/hal'
require 'support/product_representer'

module ProductsRepresenter
include Roar::JSON::HAL
include Roar::Hypermedia
include Grape::Roar::Representer

collection :entries, extend: ProductRepresenter, as: :products, embedded: true
end