pakada-render 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pakada-render.rb CHANGED
@@ -1 +1 @@
1
- require "pakada/theming"
1
+ require "pakada/render"
data/lib/pakada/render.rb CHANGED
@@ -12,6 +12,8 @@ require "pakada/render/version"
12
12
  class Pakada::Render
13
13
  include Pakada::Module
14
14
 
15
+ LAYOUT_HEADER = "X-Pakada-Render-Layout"
16
+
15
17
  attr_reader :load_path, :template_map
16
18
 
17
19
  def initialize
@@ -21,7 +23,7 @@ class Pakada::Render
21
23
  def hooks
22
24
  if Pakada[:dispatch]
23
25
  Pakada[:dispatch].after :create_controller, method(:make_renderable)
24
- Pakada[:dispatch].around :request, method(:render_layout)
26
+ Pakada[:dispatch].after :request, method(:render_layout)
25
27
  end
26
28
  end
27
29
 
@@ -38,7 +40,7 @@ class Pakada::Render
38
40
 
39
41
  def render_controller(controller)
40
42
  unless controller.options[:layout]
41
- controller.request.env["pakada.render.layout"] = false
43
+ controller.response.headers[LAYOUT_HEADER] = "no"
42
44
  end
43
45
 
44
46
  if controller.options[:render]
@@ -46,17 +48,14 @@ class Pakada::Render
46
48
  end
47
49
  end
48
50
 
49
- def render_layout(request, env)
50
- key = "pakada.render.layout"
51
- request.call(env).tap {|response|
52
- env[key] = true unless env.key? key
53
- if env[key]
54
- response[1].delete "Content-Length"
55
- content = []
56
- response[2].each {|chunk| content << chunk }
57
- response[2] = [render!(:layout, :content => content)]
58
- end
59
- }
51
+ def render_layout(response)
52
+ if !response[1].key?(LAYOUT_HEADER) || response[1][LAYOUT_HEADER] == "yes"
53
+ response[1].delete "Content-Length"
54
+
55
+ content = []
56
+ response[2].each {|chunk| content << chunk }
57
+ response[2] = [render!(:layout, :content => content)]
58
+ end
60
59
  end
61
60
 
62
61
  def boot
@@ -1,8 +1,15 @@
1
1
  class Pakada::Render
2
2
  module Rendering
3
- def render(template, locals = {})
4
- engine = Pakada[:render].template_map[template.to_sym]
5
- raise ArgumentError, "Unknown template #{template.inspect}" unless engine
3
+ def render(templates, locals = {})
4
+ templates, engine = Array(templates), nil
5
+ templates.each {|template|
6
+ e = Pakada[:render].template_map[template.to_sym]
7
+ engine = e if e
8
+ }
9
+
10
+ unless engine
11
+ raise ArgumentError, "Unknown template #{templates.join(",")}"
12
+ end
6
13
 
7
14
  Pakada.safety(Pakada::Render::Renderer).new engine, locals
8
15
  end
@@ -1,5 +1,5 @@
1
1
  class Pakada
2
2
  class Render
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
data/spec/render_spec.rb CHANGED
@@ -19,19 +19,11 @@ describe Pakada::Render do
19
19
  dispatch.stub :after
20
20
  }
21
21
 
22
- it "hooks Pakada::Dispatch#request with #render_layout" do
23
- dispatch.should_receive(:around) {|method, block|
24
- method.should equal(:request)
25
- block.should == render.method(:render_layout)
26
- }
27
- render.hooks
28
- end
29
-
30
- it "hooks Pakada::Dispatch#create_controller with #make_renderable" do
31
- dispatch.should_receive(:after) {|method, block|
32
- method.should equal(:create_controller)
33
- block.should == render.method(:make_renderable)
34
- }
22
+ it "hooks Pakada::Dispatch#request and #create_controller" do
23
+ dispatch.should_receive(:after).with :request,
24
+ render.method(:render_layout)
25
+ dispatch.should_receive(:after).with :create_controller,
26
+ render.method(:make_renderable)
35
27
  render.hooks
36
28
  end
37
29
  end
@@ -183,16 +175,17 @@ describe Pakada::Render do
183
175
  context "#render_controller(controller)" do
184
176
  let(:env) { {} }
185
177
  let(:controller) { Pakada[:dispatch].create_controller {}.new env }
178
+ let(:headers) { controller.response.headers }
186
179
 
187
- it "sets env[pakada.render.layout] to false if :layout option is false" do
180
+ it "sets the layout header to 'no' if :layout option is false" do
188
181
  controller.options[:layout] = false
189
182
  render.render_controller controller
190
- controller.request.env["pakada.render.layout"].should be_false
183
+ headers["X-Pakada-Render-Layout"].should == "no"
191
184
 
192
- controller.request.env["pakada.render.layout"] = true
185
+ headers["X-Pakada-Render-Layout"] = "yes"
193
186
  controller.options[:layout] = true
194
187
  render.render_controller controller
195
- controller.request.env["pakada.render.layout"].should be_true
188
+ headers["X-Pakada-Render-Layout"].should == "yes"
196
189
  end
197
190
 
198
191
  let(:rendered_controller) { stub "rendered controller" }
@@ -216,28 +209,11 @@ describe Pakada::Render do
216
209
  let(:env) { {} }
217
210
  let(:request) { proc {|env| response } }
218
211
  let(:response) { [200, {"Content-Length" => "123"}, []] }
219
-
220
- before { render.boot }
221
-
222
- it "defaults env[pakada.render.layout] to true" do
223
- render.render_layout request, env
224
- env["pakada.render.layout"].should be_true
225
- end
226
-
227
- it "doesn't render if env[pakada.render.layout] is false" do
228
- render.should_not_receive :render
229
- env["pakada.render.layout"] = false
230
- render.render_layout request, env
231
- end
232
-
233
- it "deletes the Content-Length header" do
234
- render.render_layout request, env
235
- response[1].key?("Content-Length").should be_false
236
- end
237
-
238
212
  let(:content) { stub "content" }
239
213
  let(:renderer) { stub "renderer" }
240
214
 
215
+ before { render.boot }
216
+
241
217
  it "renders the layout passing the response body as :content local" do
242
218
  render.should_receive(:render!) {|template, locals|
243
219
  template.should equal(:layout)
@@ -246,8 +222,30 @@ describe Pakada::Render do
246
222
  }
247
223
  response[2] = [content]
248
224
 
249
- render.render_layout request, env
225
+ render.render_layout response
250
226
  response[2].should == [renderer]
251
227
  end
228
+
229
+ it "renders if the layout header isn't set" do
230
+ render.should_receive :render
231
+ render.render_layout response
232
+ end
233
+
234
+ it "renders if the layout header is 'yes'" do
235
+ render.should_receive :render
236
+ response[1]["X-Pakada-Render-Layout"] = "yes"
237
+ render.render_layout response
238
+ end
239
+
240
+ it "doesn't render if the layout header is 'no'" do
241
+ render.should_not_receive :render
242
+ response[1]["X-Pakada-Render-Layout"] = "no"
243
+ render.render_layout response
244
+ end
245
+
246
+ it "deletes the Content-Length header" do
247
+ render.render_layout response
248
+ response[1].key?("Content-Length").should be_false
249
+ end
252
250
  end
253
251
  end
@@ -25,18 +25,29 @@ describe Pakada::Render::Rendering do
25
25
  end
26
26
 
27
27
  it "returns a Renderer for the specified template and locals" do
28
- Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, lcls|
28
+ Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, locls|
29
29
  ngin.should equal(engine)
30
- lcls.should equal(locals)
30
+ locls.should equal(locals)
31
31
  renderer
32
32
  }
33
33
  obj.render(:foo, locals).should equal(renderer)
34
34
  end
35
35
 
36
+ it "will choose the first existing template if supplied with a list" do
37
+ Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, locls|
38
+ ngin.should equal(engine)
39
+ }
40
+ obj.render [:i_dont_exist, :i_also_dont_exist, :foo, :me_neither]
41
+
42
+ expect {
43
+ obj.render [:i_dont_exist, :i_also_dont_exist]
44
+ }.to raise_error(ArgumentError)
45
+ end
46
+
36
47
  it "defaults locals to an empty Hash" do
37
- Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, lcls|
38
- lcls.should be_a(Hash)
39
- lcls.should be_empty
48
+ Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, locls|
49
+ locls.should be_a(Hash)
50
+ locls.should be_empty
40
51
  renderer
41
52
  }
42
53
  obj.render(:foo).should equal(renderer)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pakada-render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-11 00:00:00.000000000Z
12
+ date: 2011-09-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pakada
16
- requirement: &74264980 !ruby/object:Gem::Requirement
16
+ requirement: &71560660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *74264980
24
+ version_requirements: *71560660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tilt
27
- requirement: &74264710 !ruby/object:Gem::Requirement
27
+ requirement: &71560130 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *74264710
35
+ version_requirements: *71560130
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hike
38
- requirement: &74264400 !ruby/object:Gem::Requirement
38
+ requirement: &71559780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *74264400
46
+ version_requirements: *71559780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &74263580 !ruby/object:Gem::Requirement
49
+ requirement: &71559410 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *74263580
57
+ version_requirements: *71559410
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: fakefs
60
- requirement: &74263070 !ruby/object:Gem::Requirement
60
+ requirement: &71559120 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *74263070
68
+ version_requirements: *71559120
69
69
  description:
70
70
  email:
71
71
  - lars.gierth@gmail.com