Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renderer should support partial caching.

  • Loading branch information...
commit 2dddb3e705c11b87f9c270fe71adc55e5543bf82 1 parent 5e879e0
@siong1987 siong1987 authored
View
4 fixtures/ashared/views_rails_3/posts/renderer.rabl
@@ -2,3 +2,7 @@ object @post
cache @post
attributes :title, :body
+
+node :partial do |p|
+ partial('posts/renderer_partial', object: p)
+end
View
4 fixtures/ashared/views_rails_3/posts/renderer_partial.rabl
@@ -0,0 +1,4 @@
+object @post
+cache @post
+
+attributes :title, :body
View
4 lib/rabl/engine.rb
@@ -287,11 +287,11 @@ def cache_results(&block)
end
def cache_key_with_digest(cache_key)
- virtual_path = @_virtual_path ? @_virtual_path : @virtual_path
+ template = @_options[:template] || @virtual_path
Array(cache_key) + [
@_options[:root_name],
@_options[:format],
- Digestor.digest(virtual_path, :rabl, lookup_context, dependencies: view_cache_dependencies)
+ Digestor.digest(template, :rabl, lookup_context)
]
end
View
9 lib/rabl/partials.rb
@@ -10,7 +10,7 @@ def partial(file, options={}, &block)
raise ArgumentError, "Must provide an :object option to render a partial" unless options.has_key?(:object)
object, view_path = options.delete(:object), options[:view_path] || @_view_path
source, location = self.fetch_source(file, :view_path => view_path)
- engine_options = options.merge(:source => source, :source_location => location)
+ engine_options = options.merge(:source => source, :source_location => location, :template => file)
self.object_to_hash(object, engine_options, &block)
end
@@ -43,16 +43,11 @@ def fetch_source(file, options={})
fetch_manual_template(view_paths, file)
end
- # being used by cache digestor
- if defined?(Rails) && Rails.version =~ /^[4]/
- @_virtual_path = file_path.gsub('app/views/', '').gsub('.rabl', '')
- end
-
unless File.exist?(file_path.to_s)
raise "Cannot find rabl template '#{file}' within registered (#{view_paths.map(&:to_s).inspect}) view paths!"
end
- [File.read(file_path.to_s), file_path.to_s] if file_path
+ [File.read(file_path.to_s), file_path.to_s]
end
end
View
3  lib/rabl/renderer.rb
@@ -26,7 +26,8 @@ def initialize(source, object = nil, options = {})
options = {
:format => :json,
:scope => self,
- :view_path => []
+ :view_path => [],
+ :template => source
}.update(options)
@options = options
View
2  test/engine_test.rb
@@ -379,7 +379,7 @@
scope.instance_variable_set :@user, @user
any_instance_of(Rabl::Engine) do |b|
mock(b).fetch_source("foo/bar", :view_path => nil).once
- mock(b).object_to_hash(@user, :locals => { :foo => "bar" }, :source => nil, :source_location => nil).returns({ :name => 'leo', :city => 'LA', :age => 12 })
+ mock(b).object_to_hash(@user, :locals => { :foo => "bar" }, :source => nil, :source_location => nil, :template => 'foo/bar').returns({ :name => 'leo', :city => 'LA', :age => 12 })
end
JSON.parse(template.render(scope))
end.equals JSON.parse("{ \"foo\" : {\"name\":\"leo\",\"city\":\"LA\",\"age\":12} }")
View
4 test/integration/rails4/posts_controller_test.rb
@@ -149,6 +149,10 @@
# Attributes (regular)
asserts("contains post title") { topic['title'] }.equals { @post1.title }
asserts("contains post body") { topic['body'] }.equals { @post1.body }
+
+ # Attributes (partial)
+ asserts("contains post partial title") { topic['partial']['title'] }.equals { @post1.title }
+ asserts("contains post partial body") { topic['partial']['body'] }.equals { @post1.body }
end # renderer action, json
context "for index action rendering JSON within HTML" do
Please sign in to comment.
Something went wrong with that request. Please try again.