aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2012-12-18 04:24:24 +0100
committerGibheer <gibheer@gmail.com>2012-12-18 04:24:24 +0100
commitb20c0c527c89ec08548849163388d43207609fcd (patch)
tree31b2e2296ffd896e04745e6fcb423edc71d1491e
parentb0954de7dc95e44d02a8e5315ae6c2966adeaa13 (diff)
reuse render for actual rendering
The splitting of rendering and processing is good, but on the other side it makes some double steps or even introduce workarounds. That is why I made the method #render just a call to the renderer. This should help much more than the strict seperation. The method will only return the resulting string and not add it to the body of the response. That still has to be taken care of by the user.
-rw-r--r--lib/zero/controller.rb17
-rw-r--r--spec/unit/controller/render_spec.rb19
2 files changed, 32 insertions, 4 deletions
diff --git a/lib/zero/controller.rb b/lib/zero/controller.rb
index b42e500..ce9e247 100644
--- a/lib/zero/controller.rb
+++ b/lib/zero/controller.rb
@@ -56,13 +56,22 @@ module Zero
# build the response and return it
#
- # This method calls #process if it was defined so make it easier to process
- # the request before rendering stuff.
+ # This method calls #process. #process has to be provided by the actual
+ # implementation and should do all processing necessary to provide the
+ # content.
# @return Response a rack conform response
def response
- process if respond_to?(:process)
- render
+ process
@response.to_a
end
+
+ # renders a template
+ #
+ # This method calls #render of the provided renderer and gives it the
+ # template name and accept types, so that the renderer can search for the
+ # appropiate template to render.
+ def render(template)
+ @renderer.render(template, @request.accept.types, self)
+ end
end
end
diff --git a/spec/unit/controller/render_spec.rb b/spec/unit/controller/render_spec.rb
new file mode 100644
index 0000000..771db28
--- /dev/null
+++ b/spec/unit/controller/render_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Zero::Controller, '#render' do
+ subject { Zero::Controller.new(env) }
+ let(:env) { EnvGenerator.get('/foo') }
+ let(:renderer) { mock }
+ let(:template) { '/foo' }
+
+ before :each do
+ Zero::Controller.renderer = renderer
+ renderer.should_receive(:render).with(template,
+ kind_of(Zero::Request::AcceptType), subject)
+ end
+ after :each do
+ Zero::Controller.renderer = nil
+ end
+
+ it { subject.render(template) }
+end