Skip to content

Commit d24bbcd

Browse files
authored
Merge pull request rails#49458 from eugeneius/broadcast_delegate_block
Delegate block in broadcast logger method_missing
2 parents 5de585e + 5574a2f commit d24bbcd

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

activesupport/lib/active_support/broadcast_logger.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,20 @@ def dispatch(&block)
203203
@broadcasts.each { |logger| block.call(logger) }
204204
end
205205

206-
def method_missing(name, *args)
206+
def method_missing(name, *args, &block)
207207
loggers = @broadcasts.select { |logger| logger.respond_to?(name) }
208208

209209
if loggers.none?
210-
super(name, *args)
210+
super(name, *args, &block)
211211
elsif loggers.one?
212-
loggers.first.send(name, *args)
212+
loggers.first.send(name, *args, &block)
213213
else
214-
loggers.map { |logger| logger.send(name, *args) }
214+
loggers.map { |logger| logger.send(name, *args, &block) }
215215
end
216216
end
217+
218+
def respond_to_missing?(method, include_all)
219+
@broadcasts.any? { |logger| logger.respond_to?(method, include_all) }
220+
end
217221
end
218222
end

activesupport/test/broadcast_logger_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ def info(msg, &block)
268268
assert(logger.foo)
269269
end
270270

271+
test "calling a method that accepts a block" do
272+
logger = BroadcastLogger.new(CustomLogger.new)
273+
274+
called = false
275+
logger.bar do
276+
called = true
277+
end
278+
assert(called)
279+
end
280+
271281
class CustomLogger
272282
attr_reader :adds, :closed, :chevrons
273283
attr_accessor :level, :progname, :formatter, :local_level
@@ -286,6 +296,10 @@ def foo
286296
true
287297
end
288298

299+
def bar
300+
yield
301+
end
302+
289303
def debug(message, &block)
290304
add(::Logger::DEBUG, message, &block)
291305
end

0 commit comments

Comments
 (0)