Skip to content

Commit

Permalink
Support layouts in the Rails handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Oct 31, 2009
1 parent 9be2072 commit 3542970
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/effigy/rails/template_handler.rb
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion 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
Expand Down
31 changes: 28 additions & 3 deletions spec/effigy/rails_spec.rb
Expand Up @@ -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
Expand All @@ -27,7 +29,19 @@ def transform
RUBY

create_rails_file 'app/templates/magic/index.html', <<-HTML
<html><h1>placeholder title</h1></html>
<h1 class="success">placeholder title</h1>
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><body></body></html>
HTML

@controller = MagicController.new
Expand All @@ -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

0 comments on commit 3542970

Please sign in to comment.