Skip to content

Commit 7f05849

Browse files
committed
Spec for custom and default renderers
1 parent 63241cc commit 7f05849

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Empty template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Empty template

example_app_generator/generate_stuff.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def setup_tasks
1414

1515
def final_tasks
1616
copy_file 'spec/verify_active_record_spec.rb'
17+
copy_file 'app/views/foo.html'
18+
copy_file 'app/views/some_templates/bar.html'
19+
copy_file 'spec/verify_custom_renderers_spec.rb'
1720
copy_file 'spec/verify_fixture_warning_spec.rb'
1821
run('bin/rake db:migrate')
1922
if ::Rails::VERSION::STRING.to_f < 4.1
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "template rendering", :type => :controller do
4+
context 'with the standard renderers' do
5+
controller do
6+
def index
7+
render :template => :foo
8+
end
9+
end
10+
11+
it "renders the 'foo' template" do
12+
get :index
13+
14+
expect(response).to render_template(:foo)
15+
end
16+
end
17+
18+
context 'with a String path prepended to the view path' do
19+
controller do
20+
def index
21+
prepend_view_path('app/views/some_templates')
22+
23+
render :template => :bar
24+
end
25+
end
26+
27+
it "renders the 'bar' template" do
28+
get :index
29+
30+
expect(response).to render_template(:bar)
31+
end
32+
end
33+
34+
context 'with a custom renderer prepended to the view path' do
35+
controller do
36+
def index
37+
prepend_view_path(MyResolver.new)
38+
39+
render :template => :baz
40+
end
41+
end
42+
43+
it "renders the 'baz' template" do
44+
get :index
45+
46+
expect(response).to render_template(:baz)
47+
end
48+
end
49+
50+
class MyResolver < ActionView::Resolver
51+
def find_all(*args)
52+
find_templates(*args)
53+
end
54+
55+
def find_all_anywhere(*args)
56+
find_templates(*args)
57+
end
58+
59+
private
60+
61+
def find_templates(name, prefix = nil, partial = false, details = {}, key = nil, locals = [])
62+
name.prepend("_") if partial
63+
path = [prefix, name].compact.join("/")
64+
template = find_template(name, path)
65+
66+
[template]
67+
end
68+
69+
def find_template(name, path)
70+
ActionView::Template.new(
71+
"",
72+
name,
73+
lambda { |_template| %("") },
74+
:virtual_path => path,
75+
:format => :html
76+
)
77+
end
78+
end
79+
end

0 commit comments

Comments
 (0)