So you found yourself in the need to override a method but still count on it’s old behaviour?
No problem! Override it with your new code, call super and…. Uh oh!! Suddenly this turned into a problem… Let me give some more context.
I was testing Ferret (and the acts_as_ferret
plugin) in a project to provide full text search capabilities to our
models. One of the things the plugin does is to add a new method to
ActiveRecord, called find_with_ferret. That way, every model can use it. Great!
So I thought that would make sense for me to remove all diatrictics from the input text before letting ferret do its job. You know, like removing umlauts and all that.
I could do that by overriding this method with code to remove the
undesired chars and then call its older version to finally do the
search - something like calling super, but not quite. And I didn’t want
my models to inherit from anything else than ActiveRecord::Base. That
wouldn’t make any sense.
alias_method to the rescue!
You know that to redefine a method in an existing class you can open
it up and rewrite it. But since you don’t wanna loose the behaviour
provided by the original method, this is how you can achieve this:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->module ActiveRecord
class Base
alias_method :find_with_ferret_original, :find_with_ferret
def find_with_ferret(q, options = {}, find_options = {})
remove_diatrictics!(q)
find_with_ferret_original(q, options, find_options)
end
end
end
And you’re good to go. On line 3 you’re just giving the original method an alias, making a copy of it.
Then you redefine it the way you like and on line 6 you call the old version to make sure u still got the same behaviour.
Now all my models can benefit of this change without requiring them to call another method nor inherit from another class.
Cool, huh? <!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
分享到:
相关推荐
「安全威胁」Cloud_DFIR:Why_so_Cirrus - 防火墙 安全防御 数据安全 安全资讯 安全开发 安全运营
从我们的博客阅读更多:http://www.rightfile.com.au/blog/why-archive-your-email/ ====隐私==== 我们知道这是你的电子邮件,而不是我们的。 我们扩展的所有处理都发生在您的计算机上。我们从来没有看到您的任何...
because i like this charactor) 3. 运行G_Snake - 在DOSBOX的DOS提示符下键入: - `Z:\>K:`(回车) - `K:\>cd G_Snake`(回车) - 使用masm 5.0工具编译、链接、运行.asm源程序 - MASM.EXE、LINK.EXE、debug.exe...
:floppy_disk: Linux RAM磁盘具有Systemd计时器,服务和挂起v2021.4.103的持久性 错误显而易见:trade_mark:-MATRIX ... Works like a … sword!!! Why sword?! I don’t know exactly. May be because of Darcul
Watson和SIRICategories: 坑, 语义网看到2011-11-16一篇文章《Misconceptions in AI: Or why Watso
:popcorn: WHY 来自产品需求,在微信中希望用户点击我们的推广链接直接去下载App, 一通搜索之后发现全是国内商家提供的收费服务,xxx一个月的api服务费,x万卖源码(不信可以搜一下相关关键字)。 这么个小功能收费?...
[ :red_question_mark: ] So, why on Earth would I want to block scripts on my own website? 使用yett一种方法是通过如下所示的UI构建 。 在用户之前(在之前)通过在生产中接受之前,Analytics脚本将被阻止。 ...
### Why are we doing this? - ### Screenshots 合并选项 Hackathon:1个或更多代码审查 之后:2个或更多代码审查 建筑学 MVVM 代码风格 目录组织 每个屏幕都有一个大文件夹。 在大文件夹中,有单元格文件夹,...
:white_circle: >>> I want to find out, that's why I created you :purple_circle: >>> You created me? :white_circle: >>> You're my bot :purple_circle: >>> You monster gpt2bot是由神经网络提供支持的多回转...
:rocket: Why not use this? quick install npx create-vite-electron yarn create vite-electron run yarn yarn dev yarn build change log 210331 Update rollup-plugin-esbuild version. 210219 resolve #6 ...
1 FileIO 2 System Why 经常有人问我 问: 你平时自己做个项目或者小工具 答: Python 问:那你都用Python做过啥呢? 答:建站、爬虫、办公自动化 blablabla 问:能举几个例子么?简单的例子可以么 这就是我写这个的...
Serverless Function to Count How Many People are Subscribed to You in Your Favorite Services · · :rocket: NEW: Substats now takes advantage of Cloud...Table of contentsWhy I did this?I initially wan
Keras_#1_Why__(教学_教程_tutorial)
《Why I Love Python》,一份详细介绍python的PPT文档
Git_版本管理_#1_Why__(教学_教程_tutorial)
fix or feature would be useful to other developers, I can add link of your repository to this README file. Use following e-mail to contact me: dmytrodanylyk@gmail.com Thank you for using my libraries....
#1_Why__(爬虫_scraping_基础教学_教程_Tutorial)
关于一篇android安全的文章,Many Android apps have a legitimate need to communicate over the Internet and are then responsible for protecting potentially sensitive data during transit.
Why Coder:red_question_mark: 自己从大一就在用Hexo写博客,对Hexo很喜欢,一直想着自己写一个主题 博客的极简也是很多人追求的,所以我前前后后大概用了一个月开发了这个主题 如果你在使用Coder,欢迎提issue提供...
#1_why__(强化学习_Reinforcement_Learning_教学)