Skip to content

Commit c4bc440

Browse files
committed
Spec for custom and default renderers
1 parent 7205a35 commit c4bc440

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Static template named 'foo.html'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Static template named 'bar.html'

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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "template rendering", :type => :controller do
4+
context "without render_views" do
5+
context "with the standard renderers" do
6+
controller do
7+
def index
8+
render :template => 'foo', :layout => false
9+
end
10+
end
11+
12+
it "renders the 'foo' template" do
13+
get :index
14+
15+
expect(response).to render_template(:foo)
16+
end
17+
18+
it "renders an empty string" do
19+
get :index
20+
21+
expect(response.body).to eq("")
22+
end
23+
end
24+
25+
context "with a String path prepended to the view path" do
26+
controller do
27+
def index
28+
prepend_view_path('app/views/some_templates')
29+
30+
render :template => 'bar', :layout => false
31+
end
32+
end
33+
34+
it "renders the 'bar' template" do
35+
get :index
36+
37+
expect(response).to render_template(:bar)
38+
end
39+
40+
it "renders an empty string" do
41+
get :index
42+
43+
expect(response.body).to eq("")
44+
end
45+
end
46+
47+
context "with a custom renderer prepended to the view path" do
48+
controller do
49+
def index
50+
prepend_view_path(MyResolver.new)
51+
52+
render :template => 'baz', :layout => false
53+
end
54+
end
55+
56+
it "renders the 'baz' template" do
57+
get :index
58+
59+
expect(response).to render_template(:baz)
60+
end
61+
62+
it "renders an empty string" do
63+
get :index
64+
65+
expect(response.body).to eq("")
66+
end
67+
end
68+
end
69+
70+
context "with render_views enabled" do
71+
render_views
72+
73+
context "with the standard renderers" do
74+
controller do
75+
def index
76+
render :template => 'foo', :layout => false
77+
end
78+
end
79+
80+
it "renders the 'foo' template" do
81+
get :index
82+
83+
expect(response).to render_template(:foo)
84+
end
85+
86+
it "renders the contents of the template" do
87+
get :index
88+
89+
expect(response.body).to include("Static template named 'foo.html'")
90+
end
91+
end
92+
93+
context "with a String path prepended to the view path" do
94+
controller do
95+
def index
96+
prepend_view_path('app/views/some_templates')
97+
98+
render :template => 'bar', :layout => false
99+
end
100+
end
101+
102+
it "renders the 'bar' template" do
103+
get :index
104+
105+
expect(response).to render_template(:bar)
106+
end
107+
108+
it "renders the contents of the template" do
109+
get :index
110+
111+
expect(response.body).to include("Static template named 'bar.html'")
112+
end
113+
end
114+
115+
context "with a custom renderer prepended to the view path" do
116+
controller do
117+
def index
118+
prepend_view_path(MyResolver.new)
119+
120+
render :template => 'baz', :layout => false
121+
end
122+
end
123+
124+
it "renders the 'baz' template" do
125+
get :index
126+
127+
expect(response).to render_template(:baz)
128+
end
129+
130+
it "renders the contents of the template" do
131+
get :index
132+
133+
expect(response.body).to eq("Dynamic template with path '/baz'")
134+
end
135+
end
136+
end
137+
138+
class MyResolver < ActionView::Resolver
139+
private
140+
141+
def find_templates(name, prefix = nil, partial = false, details = {}, key = nil, locals = [])
142+
name.prepend("_") if partial
143+
path = [prefix, name].join("/")
144+
template = find_template(name, path)
145+
146+
[template]
147+
end
148+
149+
def find_template(name, path)
150+
ActionView::Template.new(
151+
"",
152+
name,
153+
lambda { |_template| %("Dynamic template with path '#{_template.virtual_path}'") },
154+
:virtual_path => path,
155+
:format => :html
156+
)
157+
end
158+
end
159+
end

0 commit comments

Comments
 (0)