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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ rvm:
- 1.9.3
- jruby-19mode
- rbx-2.2.10

matrix:
allow_failures:
- rvm: rbx-2.2.10
Copy link
Member

Choose a reason for hiding this comment

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

Change this one to rbx-2, that means "the latest rbx 2", otherwise we constantly chase our tails.

Copy link
Author

Choose a reason for hiding this comment

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

Change both references or just this allow_failures reference? Sorry, I'm not very familiar with travis configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Both. The first one says that we use rbx-2 and the second that we don't care ;)

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
27 changes: 21 additions & 6 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ 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"}]}]'
Copy link
Member

Choose a reason for hiding this comment

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

Expand the string into a Hash and compare JSON.parse to that, so it's more readable, multiple lines, etc.

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
27 changes: 21 additions & 6 deletions spec/present_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ 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

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' 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
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