Permalink
Browse files

Rails views can render partials

  • Loading branch information...
Joe Ferris
Joe Ferris committed Dec 2, 2009
1 parent a6ef902 commit 9246f4de5ce319714949fe96f98606fb1e858ba7
Showing with 89 additions and 14 deletions.
  1. +19 −2 lib/effigy/rails/template_handler.rb
  2. +19 −1 lib/effigy/rails/view.rb
  3. +51 −11 spec/effigy/rails/template_handler_spec.rb
@@ -13,8 +13,11 @@ def compile(view)
assigns = variables.inject({}) do |hash, name|
hash.update(name => @controller.instance_variable_get(name))
end
local_assigns.each do |name, value|
assigns.update("@\#{name}" => value)
end
end
view = #{view_class_name}.new(assigns) { |*names| yield(*names) }
view = #{view_class_name}.new(self, assigns) { |*names| yield(*names) }
view.render(#{template_source.inspect})
RUBY
end
@@ -36,13 +39,27 @@ def view_class_name
end
def view_class_components
[base_path, view_name, layout? ? 'layout' : 'view']
[base_path, view_name.sub(/^_/, ''), view_class_suffix]
end
def view_class_suffix
if layout?
'layout'
elsif partial?
'partial'
else
'view'
end
end
def layout?
base_path =~ /^layouts/
end
def partial?
@view.name =~ /^_/
end
def template_source
template_path = @view.load_path.path.sub(/\/views$/, '/templates')
template_file_name = File.join(template_path, base_path, "#{view_name}.#{@view.format}")
View
@@ -1,13 +1,31 @@
module Effigy
module Rails
class View < ::Effigy::View
def initialize(assigns, &layout_block)
# [ActionView::Base] the instance that is rendering this view
attr_reader :action_view
def initialize(action_view, assigns, &layout_block)
@action_view = action_view
assigns.each do |name, value|
instance_variable_set(name, value)
end
@layout_block = layout_block
end
# Renders the given partial and returns the generated markup.
#
# @param [String] name the name of the partial to render, as given to
# ActionView::Base#render
# @param [Hash] options
# @option options [Hash] :locals a hash of extra variables to be assigned
# on the partial view
def partial(name, options = {})
options[:partial] = name
action_view.render(options)
end
protected
def content_for(capture)
@@ -28,10 +28,15 @@ def create_rails_source_file(relative_path, contents)
load create_rails_file(relative_path, contents)
end
def render
def render(controller, action = :index)
@controller = controller
class << @controller
include ActionController::TestCase::RaiseActionExceptions
end
@request ||= ActionController::TestRequest.new
@response ||= ActionController::TestResponse.new
get :index
@response
end
include ActionController::TestProcess
@@ -58,14 +63,12 @@ def transform
<h1 class="success">placeholder title</h1>
HTML
@controller = MagicController.new
render
response = render(MagicController.new)
@response.should be_success
@response.rendered[:template].to_s.should == 'magic/index.html.effigy'
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.success', :contents => assigns(:spell))
response.body.should have_selector('h1.success', :contents => assigns(:spell))
end
it "should render an effigy layout" do
@@ -96,11 +99,48 @@ def transform
<html><body></body></html>
HTML
@controller = MagicController.new
response = render(MagicController.new)
response.should be_success
response.body.should have_selector('html body h1.success')
end
it "should render an effigy partial" do
create_rails_source_file 'app/controllers/magic_controller.rb', <<-RUBY
class WandController < ApplicationController
end
RUBY
create_rails_file 'app/views/wand/index.html.effigy', <<-RUBY
class WandIndexView < Effigy::Rails::View
def transform
replace_with('p', partial('spell', :locals => { :name => 'hocus pocus' }))
end
end
RUBY
create_rails_file 'app/templates/wand/index.html', <<-HTML
<html><body>
<h1 class="success">spell</h1>
<p>placeholder</p>
</body></html>
HTML
create_rails_file 'app/views/wand/_spell.html.effigy', <<-RUBY
class WandSpellPartial < Effigy::Rails::View
def transform
text('p', @name)
end
end
RUBY
create_rails_file 'app/templates/wand/_spell.html', <<-HTML
<p>put a spell on me</p>
HTML
render
response = render(WandController.new)
@response.should be_success
@response.body.should have_selector('html body h1.success')
response.should be_success
response.body.should have_selector('html body p', :contents => 'hocus pocus')
end
end

0 comments on commit 9246f4d

Please sign in to comment.