Skip to content

Commit

Permalink
Merge pull request #26 from hobofan/master
Browse files Browse the repository at this point in the history
allow locals in render method
  • Loading branch information
dblock committed Jul 19, 2014
2 parents ae33e2b + 6c24130 commit 6798b27
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Enable using a layout template in Rabl [@koko1000ban](https://github.com/koko1000ban) [view commit](https://github.com/LTe/grape-rabl/commit/1fbfbd58c3fb320be1b52b3247fda2a23cacc9fc)
* Implemented Rubocop, Ruby style linter [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/1211056de22a5989c063d57b7b37ebb1f1977e83)
* Removed JRuby support [@dblock](https://github.com/dblock) [view commit](https://github.com/LTe/grape-rabl/commit/59905c1b09670fe08501e09bad4ec8714839f2d3)
* Enable using locals with #render [@hobofan](https://github.com/hobofan)

#### v0.2.2

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class UserAPI < Grape::API
# use rabl with 'user.rabl' template
get '/user/:id', :rabl => 'user' do
@user = User.find(params[:id])

if @user.admin?
# overwrite the template (and pass locals) with the #render method
render rabl: 'admin', locals: { details: 'this user is a admin' }
end
end

# do not use rabl, fallback to the defalt Grape JSON formatter
Expand Down
6 changes: 5 additions & 1 deletion lib/grape-rabl/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def call(object, env)
if rablable?
rabl do |template|
engine = ::Tilt.new(view_path(template), tilt_options)
output = engine.render endpoint, {}
output = engine.render endpoint, locals
if layout_template
layout_template.render(endpoint) { output }
else
Expand Down Expand Up @@ -47,6 +47,10 @@ def rabl
yield template
end

def locals
endpoint.options[:route_options][:rabl_locals] || {}
end

def set_view_root
fail "Use Rack::Config to set 'api.tilt.root' in config.ru"
end
Expand Down
3 changes: 2 additions & 1 deletion lib/grape-rabl/render.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module GrapeRabl
module Render
def render(options = {})
env['api.endpoint'].options[:route_options][:rabl] = options.delete(:rabl)
env['api.endpoint'].options[:route_options][:rabl] = options.delete(:rabl) if options.include?(:rabl)
env['api.endpoint'].options[:route_options][:rabl_locals] = options.delete(:locals)
end
end
end
Expand Down
22 changes: 21 additions & 1 deletion spec/grape_rabl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,36 @@ def app
render rabl: 'admin'
end

subject.get('/home-detail', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe')
render rabl: 'admin', locals: { details: 'amazing detail' }
end

subject.get('/about', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe')
end

subject.get('/about-detail', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe')
render locals: { details: 'just a user' }
end
end

it 'renders template passed as argument to reneder method' do
it 'renders template passed as argument to render method' do
get('/home')
last_response.body.should == '{"admin":{"name":"LTe"}}'
end

it 'renders template passed as argument to render method with locals' do
get('/home-detail')
last_response.body.should == '{"admin":{"name":"LTe","details":"amazing detail"}}'
end

it 'renders with locals without overriding template' do
get('/about-detail')
last_response.body.should == '{"user":{"name":"LTe","details":"just a user","project":null}}'
end

it 'does not save rabl options after called #render method' do
get('/home')
get('/about')
Expand Down
4 changes: 4 additions & 0 deletions spec/views/admin.rabl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
object @user => :admin
attributes :name

node :details, :unless => lambda { |n| locals[:details].nil? } do |m|
locals[:details]
end
4 changes: 4 additions & 0 deletions spec/views/user.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ attributes :name, :email
child @project => :project do
attributes :name
end

node :details, :unless => lambda { |n| locals[:details].nil? } do |m|
locals[:details]
end

0 comments on commit 6798b27

Please sign in to comment.