[英]How can I programatically determine which methods have been declared as “helper” methods by a controller in Rails?
我正在編寫一個向控制器添加方法並將其聲明為輔助方法的插件。 如果是靜態完成的(而不是通過插件完成),它將看起來像這樣:
# in RAILS_ROOT/app/controllers/stuffed_animals_controller.rb
class StuffedAnimalsController < ActionController::Base
private
def bear
'Teddy Bear'
end
helper_method :bear
end
# in RAILS_ROOT/app/views/stuffed_animals/index.html.erb:
<%= bear -%>
它工作正常。 我想測試:some_helper_method
實際上是一個輔助方法。 我嘗試了這個:
def test_declared_bear_as_helper_method
assert StuffedAnimalsController.helper_methods.include?(:bear)
end
不幸的是, ActionController::Base
沒有:helper_methods
類方法。 有人知道我可以通過:helper_method
獲取類公開的內容的列表嗎?
得到它了!
def test_declared_bear_as_helper_method
helper = Object.new
helper.extend StuffedAnimalsController.master_helper_module
assert helper.respond_to?(:bear)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.