Skip to content

Commit 9cb2b90

Browse files
committed
Add any object that repsonds to to_s to the DOM in Arbre blocks.
Before this commit: index do column("My Column"){ 1 } end Would result in an empty column. Now the object is appended to the DOM and will result in a colum of "1"s. The return value is ignored if the user has already appended other elements to the DOM. This might trip up some users, however it is more likely that you are either going to build up a DOM structure or return a simple value, not both. Thanks for the pull requests from @bobbytables and @utkarshkukreti References isses activeadmin#470, 154
1 parent fc2d939 commit 9cb2b90

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

lib/active_admin/arbre/builder.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def build_tag(klass, *args, &block)
6565
# Render the blocks contents
6666
if block_given?
6767
with_current_dom_context tag do
68-
insert_text_node_if_string(yield)
68+
append_return_block(yield)
6969
end
7070
end
7171
end
@@ -98,10 +98,13 @@ def with_current_dom_context(tag)
9898
end
9999
alias_method :within, :with_current_dom_context
100100

101-
# Inserts a text node if the tag is a string
102-
def insert_text_node_if_string(tag)
103-
if tag.is_a?(String)
104-
current_dom_context << Arbre::HTML::TextNode.from_string(tag)
101+
# Appends the value to the current DOM element if there are no
102+
# existing DOM Children and it responds to #to_s
103+
def append_return_block(tag)
104+
return nil if current_dom_context.children.any?
105+
106+
if !tag.is_a?(Arbre::HTML::Element) && tag.respond_to?(:to_s)
107+
current_dom_context << Arbre::HTML::TextNode.from_string(tag.to_s)
105108
end
106109
end
107110
end

lib/active_admin/arbre/html/element.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def tag_name
3535

3636
def build(*args, &block)
3737
# Render the block passing ourselves in
38-
insert_text_node_if_string(block.call(self)) if block
38+
append_return_block(block.call(self)) if block
3939
end
4040

4141
def add_child(child)

spec/unit/arbre/html_spec.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,22 @@
131131
item.parent.should == list
132132
end
133133

134-
it "should set a string content return value with no children" do
135-
li do
136-
"Hello World"
137-
end.to_s.should == <<-HTML
138-
<li>Hello World</li>
139-
HTML
134+
135+
["Hello World", 1, 1.5].each do |value|
136+
it "should append the return value of '#{value}' when no other children added to the DOM" do
137+
li do
138+
value
139+
end.to_s.should == "<li>#{value}</li>\n"
140+
end
141+
end
142+
143+
["Hello World", 1, 1.5].each do |value|
144+
it "should not append the return value of '#{value}' when children have been added to the DOM" do
145+
li do
146+
text_node("Already Added")
147+
value
148+
end.to_s.should == "<li>Already Added</li>\n"
149+
end
140150
end
141151

142152
describe "text nodes" do

0 commit comments

Comments
 (0)