Permalink
Browse files

Fix missing @virtual_path with Renderer on rails 4.

  • Loading branch information...
1 parent e73a807 commit 1335131b6878b33cdbf390530a113e6a8b72b1c4 @siong1987 siong1987 committed Sep 23, 2013
@@ -0,0 +1,4 @@
+object @post
+cache @post
+
+attributes :title, :body
@@ -8,4 +8,9 @@ def index
def show
@post = Post.find(params[:id])
end
+
+ def renderer
+ post = Post.find(params[:id])
+ render json: Rabl.render(post, 'posts/renderer', view_path: 'app/views', format: :json, scope: view_context)
+ end
end
@@ -1,6 +1,10 @@
Rails32::Application.routes.draw do
resources :users
- resources :posts
+ resources :posts do
+ member do
+ get 'renderer'
+ end
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
View
@@ -1,15 +1,15 @@
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
-gem 'rails', '4.0.0.rc1'
+gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
gem 'riot', :group => "test"
# Use SCSS for stylesheets
-gem 'sass-rails', '~> 4.0.0.rc1'
+gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
@@ -8,4 +8,9 @@ def index
def show
@post = Post.find(params[:id])
end
+
+ def renderer
+ post = Post.find(params[:id])
+ render json: Rabl.render(post, 'posts/renderer', view_path: 'app/views', format: :json, scope: view_context)
+ end
end
@@ -1,6 +1,10 @@
Rails4::Application.routes.draw do
resources :users
- resources :posts
+ resources :posts do
+ member do
+ get 'renderer'
+ end
+ end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
View
@@ -287,17 +287,16 @@ def cache_results(&block)
end
def cache_key_with_digest(cache_key)
+ virtual_path = @_virtual_path ? @_virtual_path : @virtual_path
Array(cache_key) + [
@_options[:root_name],
@_options[:format],
- Digestor.digest(@virtual_path, :rabl, lookup_context, dependencies: view_cache_dependencies)
+ Digestor.digest(virtual_path, :rabl, lookup_context, dependencies: view_cache_dependencies)
]
end
def cache_key_simple(key)
Array(key) + [@_options[:root_name], @_options[:format]]
end
-
-
end
end
View
@@ -43,6 +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
@@ -139,6 +139,18 @@
asserts("contains post attributes via node") { topic["post"] }.equals { [@post1.title, @post1.body] }
end # show action, json
+ context "renderer" do
+ setup do
+ mock(ActionController::Base).perform_caching.any_number_of_times { true }
+ get "/posts/#{@post1.id}/renderer"
+ json_output['post']
+ end
+
+ # Attributes (regular)
+ asserts("contains post title") { topic['title'] }.equals { @post1.title }
+ asserts("contains post body") { topic['body'] }.equals { @post1.body }
+ end # renderer action, json
+
context "for index action rendering JSON within HTML" do
setup do
get "/posts", format: :html
@@ -139,6 +139,18 @@
asserts("contains post attributes via node") { topic["post"] }.equals { [@post1.title, @post1.body] }
end # show action, json
+ context "renderer" do
+ setup do
+ mock(ActionController::Base).perform_caching.any_number_of_times { true }
+ get "/posts/#{@post1.id}/renderer"
+ json_output['post']
+ end
+
+ # Attributes (regular)
+ asserts("contains post title") { topic['title'] }.equals { @post1.title }
+ asserts("contains post body") { topic['body'] }.equals { @post1.body }
+ end # renderer action, json
+
context "for index action rendering JSON within HTML" do
setup do
get "/posts", format: :html

0 comments on commit 1335131

Please sign in to comment.