Skip to content

Commit 82de536

Browse files
committed
Spec for custom and default renderers
1 parent 6939d95 commit 82de536

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-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/anonymous/foo.html'
18+
copy_file 'app/views/anonymous_2/anonymous/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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "Custom renderers", :type => :controller do
4+
context 'with the standard renderers' do
5+
controller do
6+
def index
7+
render :action => :foo
8+
end
9+
10+
def foo
11+
end
12+
end
13+
14+
it "renders the 'foo' template when requesting GET #index" do
15+
get :index
16+
17+
expect(response).to render_template(:foo)
18+
end
19+
end
20+
21+
context 'with a path String prepended to the view path' do
22+
controller do
23+
def index
24+
prepend_view_path('app/views/anonymous_2')
25+
26+
render :action => :bar
27+
end
28+
29+
def bar
30+
end
31+
end
32+
33+
it "renders the 'bar' template when requesting GET #index" do
34+
get :index
35+
36+
expect(response).to render_template(:bar)
37+
end
38+
end
39+
40+
context 'with a custom renderer prepended to the view path' do
41+
controller do
42+
def index
43+
prepend_view_path(MyResolver.new)
44+
45+
render :action => :baz
46+
end
47+
48+
def baz
49+
end
50+
51+
class MyResolver < ActionView::Resolver
52+
def find_all(*args)
53+
find_templates(*args)
54+
end
55+
56+
def find_all_anywhere(*args)
57+
find_templates(*args)
58+
end
59+
60+
private
61+
62+
def find_templates(name, prefix = nil, partial = false, details = {}, key = nil, locals = [])
63+
name.prepend("_") if partial
64+
path = [prefix, name].compact.join("/")
65+
template = find_template(name, path)
66+
67+
[template]
68+
end
69+
70+
def find_template(name, path)
71+
ActionView::Template.new(
72+
"",
73+
name,
74+
lambda { |_template| %("") },
75+
:virtual_path => path,
76+
:format => :html
77+
)
78+
end
79+
end
80+
end
81+
82+
it "renders the 'baz' template when requesting GET #index" do
83+
get :index
84+
85+
expect(response).to render_template(:baz)
86+
end
87+
end
88+
end

0 commit comments

Comments
 (0)