Skip to content

Commit f221140

Browse files
committed
Component#build now renders in correct context
Before this patch, Component#build's (and any other Element really) contents were being rendered within the parent context instead of the child context. I also refactored and simplified the tag insertion code.
1 parent 9d1b200 commit f221140

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lib/arbre/element/builder_methods.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,12 @@ def build_tag(klass, *args, &block)
2323
tag = klass.new(arbre_context)
2424
tag.parent = current_arbre_element
2525

26-
# If you passed in a block and want the object
27-
if block_given? && block.arity > 0
28-
# Set out context to the tag, and pass responsibility to the tag
29-
with_current_arbre_element tag do
26+
with_current_arbre_element tag do
27+
if block_given? && block.arity > 0
3028
tag.build(*args, &block)
31-
end
32-
else
33-
# Build the tag
34-
tag.build(*args)
35-
36-
# Render the blocks contents
37-
if block_given?
38-
with_current_arbre_element tag do
39-
append_return_block(yield)
40-
end
29+
else
30+
tag.build(*args)
31+
append_return_block(yield) if block_given?
4132
end
4233
end
4334

spec/arbre/unit/component.rb renamed to spec/arbre/unit/component_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
require 'spec_helper'
22

33
# A mock subclass to play with
4-
class MockComponent < Arbre::Component; end
4+
class MockComponent < Arbre::Component
5+
6+
builder_method :mock_component
7+
8+
def build
9+
h2 "Hello World"
10+
end
11+
12+
end
513

614
describe Arbre::Component do
715

16+
let(:assigns) { {} }
17+
let(:helpers) { nil }
18+
819
let(:component_class){ MockComponent }
920
let(:component){ component_class.new }
1021

@@ -20,4 +31,14 @@ class MockComponent < Arbre::Component; end
2031
component.class_list.should include("mock_component")
2132
end
2233

34+
it "should render the object using the builder method name" do
35+
comp = arbre {
36+
mock_component
37+
}.to_s.should == <<-HTML
38+
<div class="mock_component">
39+
<h2>Hello World</h2>
40+
</div>
41+
HTML
42+
end
43+
2344
end

0 commit comments

Comments
 (0)