2

我們將MiniProfiler移植到Ruby,並希望爲Rails視圖和partials添加自動檢測。爲什麼此方法無法檢測ActionView :: Template:render?

我知道existing instrumentation支持,但理想情況下想獲得「開始」和「停止」事件。我們還想支持沒有通知支持的早期版本的Rails。

我砍死了一個短instrumenter和測試的掛鉤與渲染之前之後電話:

def prof(klass, method) 
    with_profiling = (method.to_s + "_with_profiling").intern 
    without_profiling = (method.to_s + "_without_profiling").intern 

    klass.send :alias_method, without_profiling, method 
    klass.send :define_method, with_profiling do |*args, &orig| 
    puts "before #{method} #{args}" 
    self.send without_profiling, *args, &orig 
    puts "after #{method}" 
    end 
    klass.send :alias_method, method, with_profiling 
end 

prof ActionView::Template, :render 

然而,一旦這種被激活render沒有正確儀表。這尤其適用於一些諧音的,但有一個爆炸:

::的ActionView ::模板錯誤(未定義的方法`html_safe」的零:NilClass)

有什麼不對本方法鉤子?什麼是掛鉤的方法,使他們不易碎這一問題的正確穩健的方式(保持普通的API:prof klass, method

+0

任何理由青睞'(method.to_s + 「_with_profiling」)intern'過': 「#{}方法_with_profiling」'? – tadman 2012-04-13 05:01:10

+0

不是真的,我會清理它 – 2012-04-13 05:12:28

回答

2

你重新回到你的最終puts其結果通常是nil的方法。一個修復程序可能是:

klass.send :define_method, with_profiling do |*args, &orig| 
    puts "before #{method} #{args}" 
    result = self.send without_profiling, *args, &rig 
    puts "after #{method}" 

    result 
end 
+0

哇...不能相信我錯過了,這將學習我從紅寶石休息:) – 2012-04-13 05:11:38

+0

現在的後續問題,我需要問的是如何得到一個理智的視圖名稱來自鉤子 – 2012-04-13 08:18:28

相關問題