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 - 防火墙 安全防御 数据安全 安全资讯 安全开发 安全运营
### 1. 选择合适的连接词 #### 题目: We have entered into an age _______ dreams have the best chance of coming true. A. which B. what C. when D. that #### 解析: 本题考查的是关系代词和关系副词的选择。...
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...
1. 人们对...有不同的看法。有些人建议...。 - There are different opinions among people as to ____. Some people suggest that ____. #### 解析: 此句型用于引出文章将要探讨的主题,并展示存在不同的观点或...
: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
【教育精品资料】——《湖北省》2Unit 5 Why do you like pandas Section A课件2.ppt 本课件深入探讨了英语语法和词汇的应用,特别关注了由"why"和"where"引导的特殊疑问句以及形容词在句子中的用法。以下是详细的...
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脚本将被阻止。 ...
#### 句型六:______ has been playing an increasingly important role in our day-to-day life. It has brought us a lot of benefits but has created some serious problems as well. **应用场景:** 论述某一...
### Why are we doing this? - ### Screenshots 合并选项 Hackathon:1个或更多代码审查 之后:2个或更多代码审查 建筑学 MVVM 代码风格 目录组织 每个屏幕都有一个大文件夹。 在大文件夹中,有单元格文件夹,...
- **解析:** 由于后半句提到“I don’t know why”,可以推断出前半句表达的是他从未告诉过我这件事。 #### 9. He is popular ____ his classmates. - **选项:** A. to B. for C. with D. by - **正确答案:** C....
: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是由神经网络提供支持的多回转...
1. **Jenny: Shall I go and tell Mr. Fairbanks about our proposal?** **Jackie: ________.** - A. Yes, you go. - B. Yes, go. - C. Yes, let's. - D. Yes, please. **正确答案:** D. Yes, please. **...
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...
文章标题为:“Why Eve and Mallory Love Android:An Analysis of Android SSL(In)Security”,从标题我们可以看出,本文将重点分析Android操作系统中SSL/TLS协议安全性的潜在问题,以及这些安全漏洞为何受到攻击者...
: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 问:能举几个例子么?简单的例子可以么 这就是我写这个的...
- 题目:___, is there a pay phone in the neighborhood? 解析:打扰别人时通常用"Excuse me",所以正确答案是C. Excuse me。 3. 来自哪里: - 题目:They’re ___ Australia. 解析:表示来自某地用be from,...
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