use*_*769 22 ruby overriding ruby-on-rails helpers
我想使用rails熟悉的助手,但功能略有改变.我看待它的方式,我希望能够做到这样的事情:
module AwesomeHelper
#... create alias of stylesheet_link_tag to old_stylesheet_link_tag
def stylesheet_link_tag(*args)
if @be_awesome
awesome_stylesheet_link_tag *args
else
old_stylesheet_link_tag *args
end
end
end
Run Code Online (Sandbox Code Playgroud)
我看到它的方式,我有三个选择:
所以这里的问题是,我是否坚持使用其中一种次优解决方案,还是有其他方式我没有考虑过?如果我选择选项3,有没有办法在不直接寻址rails helper模块的情况下执行此操作?
(注意:我删除了上下文,因为它没有添加任何问题.)
Cad*_*ade 34
有比你列出的任何选项更好的方法.只需使用super:
module AwesomeHelper
def stylesheet_link_tag(*sources)
if @be_awesome
awesome_stylesheet_link_tag *sources
else
super
end
end
end
Run Code Online (Sandbox Code Playgroud)
覆盖stylesheet_link_tagAwesomeHelper将确保在stylesheet_link_tag被调用时,Ruby会在方法查找路径中遇到它ActionView::Helpers::AssetTagHelper.如果@be_awesome是true,你可以在那里负责并停止,如果没有,那么super没有括号的调用将透明地传递到Rails实现的所有参数.这样你就不用担心Rails核心团队会在你身上移动东西!
我不使用这个宝石,所以我会以更通用的方式回答你.
假设您要将调用记录到link_to帮助程序(是的,人为的例子,但显示了这个想法).查看API可让您了解link_to位于ActionView::Helpers::UrlHelper模块内部的内容.因此,您可以在您的config/initializers目录中创建一些文件,其中包含以下内容:
# like in config/initializers/link_to_log.rb
module ActionView::Helpers::UrlHelper
def link_to_with_log(*args, &block)
logger.info '**** LINK_TO CALL ***'
link_to_without_log(*args, &block) # calling the original helper
end
alias_method_chain :link_to, :log
end
Run Code Online (Sandbox Code Playgroud)
此功能的核心 - alias_method_chain(可点击).在定义方法后使用它xxx_with_feature.
尝试使用alias_method:
module AwesomeHelper
alias_method :original_stylesheet_link_tag, :stylesheet_link_tag
def stylesheet_link_tag(*sources)
if @be_awesome
awesome_stylesheet_link_tag *sources
else
original_stylesheet_link_tag *sources
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9466 次 |
| 最近记录: |