Skip to content

Commit daaae4a

Browse files
committed
Ensure Arbre context is reset
When you have an Arbre component with a block form, it pushes the current node onto a stack so that it can use [method_missing][1] to provide a dynamic evaluation context (eg. so tab is available only directly inside of tabs). Unfortunately, when an error is raised inside inside one of these, if we have a rescue to catch that error so the rest of the page can be rendered - the context is still stuck inside the last block that was added. Other than likely rendering some of the wrong markup (depending on usage), it means the context is not put back into the block where the rescue is located. As a result, even though that block is effectively thrown away when a rescue is located above the erroring call, certain features are not available. [1]: https://github.com/activeadmin/arbre/blob/v2.2.0/lib/arbre/element.rb#L176-L186
1 parent 310e64b commit daaae4a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/arbre/context.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def with_current_arbre_element(tag)
9090
raise ArgumentError, "Can't be in the context of nil. #{@_current_arbre_element_buffer.inspect}" unless tag
9191
@_current_arbre_element_buffer.push tag
9292
yield
93+
ensure
9394
@_current_arbre_element_buffer.pop
9495
end
9596
alias_method :within, :with_current_arbre_element

spec/arbre/unit/context_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,30 @@
3232
expect(context.index('<')).to eq(0)
3333
end
3434

35+
context "when an error is raised in a nested block" do
36+
let(:context) do
37+
described_class.new do
38+
div do
39+
para 'hello!'
40+
div do
41+
ul do
42+
li do
43+
'item one'
44+
end
45+
li do
46+
raise 'error talking to the db'
47+
end
48+
end
49+
rescue StandardError
50+
para 'Uh oh! DB call failed'
51+
end
52+
end
53+
end
54+
end
55+
56+
it "properly resets the element buffer" do
57+
# When this example fails, it causes a SystemStackError
58+
expect(context.current_arbre_element).to be_a(described_class)
59+
end
60+
end
3561
end

0 commit comments

Comments
 (0)