Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Default renderer

  • Loading branch information...
commit 80e65bbe8ed343c47ac50f7987fe08d86b352621 1 parent bca33fc
authored August 03, 2012 jferris committed August 03, 2012
16  lib/turning/controller.rb
... ...
@@ -1,7 +1,7 @@
1 1
 module Turning
2 2
   class Controller
3  
-    def initialize(renderer)
4  
-      @renderer = renderer
  3
+    def initialize(renderer = nil)
  4
+      @renderer = renderer || default_renderer
5 5
     end
6 6
 
7 7
     private
@@ -9,5 +9,17 @@ def initialize(renderer)
9 9
     def render(template_name, assigns = {})
10 10
       @renderer.render_to_file(template_name, assigns)
11 11
     end
  12
+
  13
+    def default_renderer
  14
+      Renderer.new(view_path, storage)
  15
+    end
  16
+
  17
+    def view_path
  18
+      self.class.name.underscore.sub(/_controller$/, '')
  19
+    end
  20
+
  21
+    def storage
  22
+      FileStorage.new(Rails.root.join('public', 'static'))
  23
+    end
12 24
   end
13 25
 end
13  lib/turning/renderer.rb
@@ -9,8 +9,9 @@
9 9
 
10 10
 module Turning
11 11
   class Renderer
12  
-    def initialize(controller_path)
  12
+    def initialize(controller_path, storage)
13 13
       @controller_path = controller_path
  14
+      @storage = storage
14 15
       @renderable = Class.new(AbstractController::Base) {
15 16
         include AbstractController::Rendering
16 17
         include AbstractController::Layouts
@@ -45,16 +46,8 @@ def self.name
45 46
 
46 47
     def render_to_file(template_name, assigns)
47 48
       @renderable.view_assigns = assigns
48  
-      target = target_path.join("#{template_name}.html")
49 49
       contents = @renderable.render_to_string(action: template_name)
50  
-      FileUtils.mkdir_p(target_path)
51  
-      File.open(target, 'w') { |file| file.write(contents) }
52  
-    end
53  
-
54  
-    private
55  
-
56  
-    def target_path
57  
-      Rails.root.join('public', 'static', @controller_path)
  50
+      @storage.put("#{@controller_path}/#{template_name}", contents)
58 51
     end
59 52
   end
60 53
 end
13  spec/turning/integration_spec.rb
@@ -6,11 +6,15 @@
6 6
 
7 7
   it 'listens to events and renders the view to a static file' do
8 8
     controller_class = Class.new(Turning::Controller) do
9  
-      def initialize(renderer, model)
10  
-        super(renderer)
  9
+      def initialize(model)
  10
+        super()
11 11
         @model = model
12 12
       end
13 13
 
  14
+      def self.name
  15
+        'ExamplesController'
  16
+      end
  17
+
14 18
       def listen
15 19
         @model.on_update do
16 20
           render 'index', greeting: 'Hello'
@@ -30,14 +34,13 @@ def trigger_update
30 34
 
31 35
     create_view('examples/index.html.erb', '<%= @greeting %>')
32 36
 
33  
-    renderer = Turning::Renderer.new('examples')
34 37
     model = model_class.new
35  
-    controller = controller_class.new(renderer, model)
  38
+    controller = controller_class.new(model)
36 39
 
37 40
     controller.listen
38 41
     model.trigger_update
39 42
 
40  
-    get '/examples/index.html'
  43
+    get '/examples/index'
41 44
     last_response.body.should == 'Hello'
42 45
   end
43 46
 
30  spec/turning/renderer_spec.rb
@@ -4,18 +4,20 @@
4 4
 describe Turning::Renderer do
5 5
   it 'renders to a file' do
6 6
     create_view('examples/say_hello.html.erb', '<%= @greeting %>')
7  
-    renderer = Turning::Renderer.new('examples')
  7
+    storage = mock_storage
  8
+    renderer = Turning::Renderer.new('examples', storage)
8 9
     renderer.render_to_file('say_hello', greeting: 'Hello')
9 10
 
10  
-    read_static_view('examples/say_hello.html').should include 'Hello'
  11
+    storage.should have_static_view('examples/say_hello', 'Hello')
11 12
   end
12 13
 
13 14
   it 'supplies built-in helpers' do
14 15
     create_view('examples/go_home.html.erb', '<%= link_to "go home", root_path %>')
15  
-    renderer = Turning::Renderer.new('examples')
  16
+    storage = mock_storage
  17
+    renderer = Turning::Renderer.new('examples', storage)
16 18
     renderer.render_to_file('go_home', {})
17 19
 
18  
-    read_static_view('examples/go_home.html').should match(%r{<a href=".*">go home</a>})
  20
+    storage.should have_static_view('examples/go_home', %{<a href="/">go home</a>})
19 21
   end
20 22
 
21 23
   it 'supplies custom helpers' do
@@ -27,24 +29,34 @@ def greet
27 29
       end
28 30
     HELPER
29 31
     create_view('examples/greet_hello.html.erb', '<%= greet %>')
30  
-    renderer = Turning::Renderer.new('examples')
  32
+    storage = mock_storage
  33
+    renderer = Turning::Renderer.new('examples', storage)
31 34
     renderer.render_to_file('greet_hello', {})
32 35
 
33  
-    read_static_view('examples/greet_hello.html').should include 'Hello'
  36
+    storage.should have_static_view('examples/greet_hello', 'Hello')
34 37
   end
35 38
 
36 39
   it 'disables forgery protection for static forms' do
37 40
     create_view('examples/form.html.erb', "<%= form_tag '/' do %><% end %>")
38  
-    renderer = Turning::Renderer.new('examples')
  41
+    renderer = Turning::Renderer.new('examples', mock_storage)
39 42
     expect { renderer.render_to_file('form', {}) }.not_to raise_error
40 43
   end
41 44
 
42 45
   it 'renders with a layout' do
43 46
     create_view('examples/simple.html.erb', 'Hello')
44 47
     create_view('layouts/static.html.erb', 'Check this: <%= yield %>')
45  
-    renderer = Turning::Renderer.new('examples')
  48
+    storage = mock_storage
  49
+    renderer = Turning::Renderer.new('examples', storage)
46 50
     renderer.render_to_file('simple', {})
47 51
 
48  
-    read_static_view('examples/simple.html').should == 'Check this: Hello'
  52
+    storage.should have_static_view('examples/simple', 'Check this: Hello')
  53
+  end
  54
+
  55
+  def mock_storage
  56
+    stub('storage', put: nil)
  57
+  end
  58
+
  59
+  def have_static_view(path, contents)
  60
+    have_received(:put).with(path, contents)
49 61
   end
50 62
 end

0 notes on commit 80e65bb

Please sign in to comment.
Something went wrong with that request. Please try again.