Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Default renderer

  • Loading branch information...
commit 80e65bbe8ed343c47ac50f7987fe08d86b352621 1 parent bca33fc
Joe Ferris and Mike Burns authored jferris committed
View
16 lib/turning/controller.rb
@@ -1,7 +1,7 @@
module Turning
class Controller
- def initialize(renderer)
- @renderer = renderer
+ def initialize(renderer = nil)
+ @renderer = renderer || default_renderer
end
private
@@ -9,5 +9,17 @@ def initialize(renderer)
def render(template_name, assigns = {})
@renderer.render_to_file(template_name, assigns)
end
+
+ def default_renderer
+ Renderer.new(view_path, storage)
+ end
+
+ def view_path
+ self.class.name.underscore.sub(/_controller$/, '')
+ end
+
+ def storage
+ FileStorage.new(Rails.root.join('public', 'static'))
+ end
end
end
View
13 lib/turning/renderer.rb
@@ -9,8 +9,9 @@
module Turning
class Renderer
- def initialize(controller_path)
+ def initialize(controller_path, storage)
@controller_path = controller_path
+ @storage = storage
@renderable = Class.new(AbstractController::Base) {
include AbstractController::Rendering
include AbstractController::Layouts
@@ -45,16 +46,8 @@ def self.name
def render_to_file(template_name, assigns)
@renderable.view_assigns = assigns
- target = target_path.join("#{template_name}.html")
contents = @renderable.render_to_string(action: template_name)
- FileUtils.mkdir_p(target_path)
- File.open(target, 'w') { |file| file.write(contents) }
- end
-
- private
-
- def target_path
- Rails.root.join('public', 'static', @controller_path)
+ @storage.put("#{@controller_path}/#{template_name}", contents)
end
end
end
View
13 spec/turning/integration_spec.rb
@@ -6,11 +6,15 @@
it 'listens to events and renders the view to a static file' do
controller_class = Class.new(Turning::Controller) do
- def initialize(renderer, model)
- super(renderer)
+ def initialize(model)
+ super()
@model = model
end
+ def self.name
+ 'ExamplesController'
+ end
+
def listen
@model.on_update do
render 'index', greeting: 'Hello'
@@ -30,14 +34,13 @@ def trigger_update
create_view('examples/index.html.erb', '<%= @greeting %>')
- renderer = Turning::Renderer.new('examples')
model = model_class.new
- controller = controller_class.new(renderer, model)
+ controller = controller_class.new(model)
controller.listen
model.trigger_update
- get '/examples/index.html'
+ get '/examples/index'
last_response.body.should == 'Hello'
end
View
30 spec/turning/renderer_spec.rb
@@ -4,18 +4,20 @@
describe Turning::Renderer do
it 'renders to a file' do
create_view('examples/say_hello.html.erb', '<%= @greeting %>')
- renderer = Turning::Renderer.new('examples')
+ storage = mock_storage
+ renderer = Turning::Renderer.new('examples', storage)
renderer.render_to_file('say_hello', greeting: 'Hello')
- read_static_view('examples/say_hello.html').should include 'Hello'
+ storage.should have_static_view('examples/say_hello', 'Hello')
end
it 'supplies built-in helpers' do
create_view('examples/go_home.html.erb', '<%= link_to "go home", root_path %>')
- renderer = Turning::Renderer.new('examples')
+ storage = mock_storage
+ renderer = Turning::Renderer.new('examples', storage)
renderer.render_to_file('go_home', {})
- read_static_view('examples/go_home.html').should match(%r{<a href=".*">go home</a>})
+ storage.should have_static_view('examples/go_home', %{<a href="/">go home</a>})
end
it 'supplies custom helpers' do
@@ -27,24 +29,34 @@ def greet
end
HELPER
create_view('examples/greet_hello.html.erb', '<%= greet %>')
- renderer = Turning::Renderer.new('examples')
+ storage = mock_storage
+ renderer = Turning::Renderer.new('examples', storage)
renderer.render_to_file('greet_hello', {})
- read_static_view('examples/greet_hello.html').should include 'Hello'
+ storage.should have_static_view('examples/greet_hello', 'Hello')
end
it 'disables forgery protection for static forms' do
create_view('examples/form.html.erb', "<%= form_tag '/' do %><% end %>")
- renderer = Turning::Renderer.new('examples')
+ renderer = Turning::Renderer.new('examples', mock_storage)
expect { renderer.render_to_file('form', {}) }.not_to raise_error
end
it 'renders with a layout' do
create_view('examples/simple.html.erb', 'Hello')
create_view('layouts/static.html.erb', 'Check this: <%= yield %>')
- renderer = Turning::Renderer.new('examples')
+ storage = mock_storage
+ renderer = Turning::Renderer.new('examples', storage)
renderer.render_to_file('simple', {})
- read_static_view('examples/simple.html').should == 'Check this: Hello'
+ storage.should have_static_view('examples/simple', 'Check this: Hello')
+ end
+
+ def mock_storage
+ stub('storage', put: nil)
+ end
+
+ def have_static_view(path, contents)
+ have_received(:put).with(path, contents)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.