Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
View
40 lib/raptor/templates.rb
@@ -7,9 +7,9 @@ def self.find(path)
in_same_dir = File.join('views', path, LAYOUT_FILENAME)
in_root_dir = File.join('views', LAYOUT_FILENAME)
if File.exist?(in_same_dir)
- Layout.new(in_same_dir)
+ Layout.from_path(in_same_dir)
elsif File.exist?(in_root_dir)
- Layout.new(in_root_dir)
+ Layout.from_path(in_root_dir)
else
NullLayout
end
@@ -23,18 +23,44 @@ def self.render(inner, presenter)
end
class Layout
- attr_reader :path
- def initialize(path)
- @path = path
+ attr_reader :tilt
+ def initialize(tilt)
+ @tilt = tilt
+ end
+
+ def self.from_path(path)
+ new(Tilt.new(path))
end
def ==(other)
other.is_a?(Layout) &&
- other.path == path
+ other.tilt == tilt
end
def render(inner, presenter)
- Tilt.new(@path).render { inner.render(presenter) }
+ context = ViewContext.new(presenter)
+ rendered = inner.render(context)
+ @tilt.render(context) { rendered }
+ end
+ end
+
+ class ViewContext < BasicObject
+ def initialize(presenter)
+ @presenter = presenter
+ @areas = {}
+ end
+
+ def content_for(name, &block)
+ if block
+ @areas[name] ||= []
+ @areas[name] << block.call
+ else
+ @areas[name].join
+ end
+ end
+
+ def method_missing(name, *args, &block)
+ @presenter.send(name, *args, &block)
end
end
View
1  spec/fixtures/layout_with_content_for.html.erb
@@ -0,0 +1 @@
+<%= content_for :head %>
View
3  spec/fixtures/provides_content_for.html.erb
@@ -0,0 +1,3 @@
+<% content_for :head do %>
+ <script></script>
+<% end %>
View
37 spec/template_spec.rb
@@ -25,16 +25,49 @@
end
describe Raptor::Layout do
+ let(:presenter) { stub }
+
it "renders a yielded template" do
inner = stub(:render => 'inner')
- presenter = stub
- rendered = Raptor::Layout.new('spec/fixtures/layout.html.erb').
+ rendered = Raptor::Layout.from_path('spec/fixtures/layout.html.erb').
render(inner, presenter)
rendered.strip.should == "<div>inner</div>"
end
+
+ it "integrates content_for" do
+ inner = Raptor::Template.from_path("../spec/fixtures/provides_content_for.html.erb")
+ layout = Raptor::Layout.from_path('spec/fixtures/layout_with_content_for.html.erb')
+ rendered = layout.render(inner, presenter)
+ rendered.strip.should include("<script></script")
+ end
+end
+
+describe Raptor::ViewContext do
+ it "delegates to the presenter" do
+ Raptor::ViewContext.new(stub(:cheese => 'walrus')).cheese.should == 'walrus'
+ end
+
+ it "stores content_for" do
+ context = Raptor::ViewContext.new(stub)
+ context.content_for(:head) { 'what' }
+ context.content_for(:head).should == 'what'
+ end
+
+ it "appends multiple content_for blocks" do
+ context = Raptor::ViewContext.new(stub)
+ context.content_for(:head) { 'what' }
+ context.content_for(:head) { 'what' }
+ context.content_for(:head).should == 'whatwhat'
+ end
end
describe Raptor::FindsLayouts do
+ before do
+ Tilt.stub(:new) do |path|
+ path
+ end
+ end
+
it "finds a layout in the path directory" do
path = 'views/post/layout.html.erb'
File.stub(:exist?).with(path) { true }
View
0  views/ping/layout.html.erb
No changes.

No commit comments for this range

Something went wrong with that request. Please try again.