Skip to content

Commit 5250982

Browse files
deivid-rodriguezvaryonic
authored andcommitted
Support helpers with kwargs (activeadmin#202).
1 parent 78f97f5 commit 5250982

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

lib/arbre/context.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,21 @@ def respond_to_missing?(method, include_all)
7474
# Webservers treat Arbre::Context as a string. We override
7575
# method_missing to delegate to the string representation
7676
# of the html.
77-
def method_missing(method, *args, &block)
78-
if cached_html.respond_to? method
79-
cached_html.send method, *args, &block
80-
else
81-
super
77+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.a")
78+
def method_missing(method, *args, **kwargs, &block)
79+
if cached_html.respond_to? method
80+
cached_html.send method, *args, **kwargs, &block
81+
else
82+
super
83+
end
84+
end
85+
else
86+
def method_missing(method, *args, &block)
87+
if cached_html.respond_to? method
88+
cached_html.send method, *args, &block
89+
else
90+
super
91+
end
8292
end
8393
end
8494

lib/arbre/element.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,34 @@ def clear_children!
189189
# 3. Call the method on the helper object
190190
# 4. Call super
191191
#
192-
def method_missing(name, *args, &block)
193-
if current_arbre_element.respond_to?(name)
194-
current_arbre_element.send name, *args, &block
195-
elsif assigns && assigns.has_key?(name)
196-
assigns[name]
197-
elsif helpers.respond_to?(name)
198-
helper_capture(name, *args, &block)
199-
else
200-
super
192+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.a")
193+
def method_missing(name, *args, **kwargs, &block)
194+
if current_arbre_element.respond_to?(name)
195+
current_arbre_element.send name, *args, **kwargs, &block
196+
elsif assigns && assigns.has_key?(name)
197+
assigns[name]
198+
elsif helpers.respond_to?(name)
199+
s = ""
200+
within(Element.new) { s = helpers.send(name, *args, **kwargs, &block) }
201+
s.is_a?(Element) ? s.to_s : s
202+
else
203+
super
204+
end
205+
end
206+
else
207+
def method_missing(name, *args, &block)
208+
if current_arbre_element.respond_to?(name)
209+
current_arbre_element.send name, *args, &block
210+
elsif assigns && assigns.has_key?(name)
211+
assigns[name]
212+
elsif helpers.respond_to?(name)
213+
s = ""
214+
within(Element.new) { s = helpers.send(name, *args, &block) }
215+
s.is_a?(Element) ? s.to_s : s
216+
else
217+
super
218+
end
201219
end
202-
end
203-
204-
# The helper might have a block that builds Arbre elements
205-
# which will be rendered (#to_s) inside ActionView::Base#capture.
206-
# We do not want such elements added to self, so we push a dummy
207-
# current_arbre_element.
208-
def helper_capture(name, *args, &block)
209-
s = ""
210-
within(Element.new) { s = helpers.send(name, *args, &block) }
211-
s.is_a?(Element) ? s.to_s : s
212220
end
213221

214222
def method_distance(name)

0 commit comments

Comments
 (0)