Skip to content

Commit c0eb0e3

Browse files
author
Kush Kella
committed
use tilt_options and path as cache key for templates
1 parent 58bb997 commit c0eb0e3

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
rvm:
2-
- 1.9.3
32
- 2.0.0
43
- 2.1.0
54
- ruby-head
65
- jruby-19mode
76
- jruby-head
8-
- rbx-2.2.10
7+
- rbx-2
98

109
matrix:
1110
allow_failures:
1211
- rvm: jruby-19mode
1312
- rvm: jruby-head
1413
- rvm: ruby-head
14+
- rvm: rbx-2

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#### Next
22

3+
* Fix template caching for multiple formats. [#43](https://github.com/ruby-grape/grape-rabl/pull/43) [@kushkella](https://github.com/kushkella)
34
* Your contribution here.
45

56
#### v0.4.1

lib/grape-rabl/formatter.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def set_view_root
6767

6868
def tilt_template(template)
6969
if Grape::Rabl.configuration.cache_template_loading
70-
Grape::Rabl::Formatter.tilt_cache.fetch(template) { ::Tilt.new(view_path(template), tilt_options) }
70+
Grape::Rabl::Formatter.tilt_cache.fetch(tilt_cache_key(template)) { ::Tilt.new(view_path(template), tilt_options) }
7171
else
7272
::Tilt.new(view_path(template), tilt_options)
7373
end
@@ -80,11 +80,15 @@ def tilt_options
8080
def layout_template
8181
layout_path = view_path(env['api.tilt.layout'] || 'layouts/application')
8282
if Grape::Rabl.configuration.cache_template_loading
83-
Grape::Rabl::Formatter.tilt_cache.fetch(layout_path) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
83+
Grape::Rabl::Formatter.tilt_cache.fetch(tilt_cache_key(layout_path)) { ::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path) }
8484
else
8585
::Tilt.new(layout_path, tilt_options) if File.exist?(layout_path)
8686
end
8787
end
88+
89+
def tilt_cache_key(path)
90+
Digest::MD5.hexdigest("#{path}#{tilt_options}")
91+
end
8892
end
8993
end
9094
end

spec/grape_rabl_spec.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
end
77

88
before do
9-
subject.format :json
9+
subject.default_format :json
1010
subject.formatter :json, Grape::Formatter::Rabl
11+
subject.formatter :xml, Grape::Formatter::Rabl
1112
subject.helpers MyHelper
1213
end
1314

@@ -166,7 +167,27 @@ def app
166167
old_response.should == new_response
167168
end
168169

169-
it 'should serve new template if cache_template_loading' do
170+
it 'should maintain different cached templates for different formats' do
171+
Grape::Rabl.configure do |config|
172+
config.cache_template_loading = true
173+
end
174+
get '/home'
175+
last_response.status.should be == 200
176+
json_response = last_response.body
177+
get '/home.xml'
178+
last_response.status.should be == 200
179+
xml_response = last_response.body
180+
json_response.should_not be == xml_response
181+
open(@template, 'a') { |f| f << 'node(:test) { "test" }' }
182+
get '/home.xml'
183+
last_response.status.should be == 200
184+
last_response.body.should be == xml_response
185+
get '/home.json'
186+
last_response.status.should be == 200
187+
last_response.body.should be == json_response
188+
end
189+
190+
it 'should serve new template unless cache_template_loading' do
170191
get '/home'
171192
last_response.status.should be == 200
172193
old_response = last_response.body

0 commit comments

Comments
 (0)