From 3542970e17ca7cf39d1181759d433a27648a8594 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Fri, 30 Oct 2009 22:50:52 -0400 Subject: [PATCH] Support layouts in the Rails handler --- lib/effigy/rails/template_handler.rb | 3 ++- lib/effigy/rails/view.rb | 9 +++++++- spec/effigy/rails_spec.rb | 31 +++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/effigy/rails/template_handler.rb b/lib/effigy/rails/template_handler.rb index b767719..c65bbec 100644 --- a/lib/effigy/rails/template_handler.rb +++ b/lib/effigy/rails/template_handler.rb @@ -14,7 +14,8 @@ def compile(view) hash.update(name => @controller.instance_variable_get(name)) end end - #{view_class_name}.new(assigns).render(#{template_source.inspect}) + view = #{view_class_name}.new(assigns) { |*names| yield(*names) } + view.render(#{template_source.inspect}) RUBY end diff --git a/lib/effigy/rails/view.rb b/lib/effigy/rails/view.rb index 6c50cc4..2ef079c 100644 --- a/lib/effigy/rails/view.rb +++ b/lib/effigy/rails/view.rb @@ -1,10 +1,17 @@ module Effigy module Rails class View < ::Effigy::View - def initialize(assigns) + def initialize(assigns, &layout_block) assigns.each do |name, value| instance_variable_set(name, value) end + @layout_block = layout_block + end + + protected + + def content_for(capture) + @layout_block.call(capture) end end end diff --git a/spec/effigy/rails_spec.rb b/spec/effigy/rails_spec.rb index 4007362..3638796 100644 --- a/spec/effigy/rails_spec.rb +++ b/spec/effigy/rails_spec.rb @@ -11,9 +11,11 @@ @files = [] create_rails_source_file 'app/controllers/magic_controller.rb', <<-RUBY class MagicController < ApplicationController + layout 'application' include ActionController::TestCase::RaiseActionExceptions def index @spell = 'hocus pocus' + render end end RUBY @@ -27,7 +29,19 @@ def transform RUBY create_rails_file 'app/templates/magic/index.html', <<-HTML -

placeholder title

+

placeholder title

+ HTML + + create_rails_file 'app/views/layouts/application.html.effigy', <<-RUBY + class LayoutsApplicationView < Effigy::Rails::View + def transform + inner('body', content_for(:layout)) + end + end + RUBY + + create_rails_file 'app/templates/layouts/application.html', <<-HTML + HTML @controller = MagicController.new @@ -53,13 +67,24 @@ def create_rails_source_file(relative_path, contents) load create_rails_file(relative_path, contents) end + def render + get :index + end + include ActionController::TestProcess it "should use the view to render the template" do - get :index + render @response.should be_success @response.rendered[:template].to_s.should == 'magic/index.html.effigy' assigns(:spell).should_not be_nil - @response.body.should have_selector('h1', :contents => assigns(:spell)) + @response.body.should have_selector('h1.success', :contents => assigns(:spell)) + end + + it "should render an effigy layout" do + render + + @response.should be_success + @response.body.should have_selector('html body h1.success') end end