Rails,创建回调
我想使用 ActiveModel 回调 在对对象进行投票后调用,问题是我正在使用的 gem (voteable_mongo) 使模型可投票 在我的应用程序中不提供投票模型或回调,那么我该如何创建回调吗?
set_callback(:vote, :before) do |object|
object.do_something
end
显然,我编造了投票行动,但我正在使用的 gem 有 这个方法,你如何正确扩展这个方法来触发回调?
I want to use an ActiveModel callback to be called after an object has been voted on, the issue is that the gem I'm using (voteable_mongo) to make the model votable doesnt provide like a vote model or callback in my app, so how can I create a callback for it?
set_callback(:vote, :before) do |object|
object.do_something
end
Obviously that vote action I made up, but the gem I'm using has this method, how would you properly extend this method to trigger a callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将插件示例作为源代码,您可以执行以下操作:
我没有运行此代码,因此我不确定这是否能正常工作,但在最坏的情况下,您可以使用 alias_method_chain< 为投票方法添加别名/strong> 或者只是将源代码复制并粘贴到 _run_vote_callbacks 块内(真的非常难看,但无论如何它都是一个解决方案)。
编辑
如果上面的代码不起作用,也可以使用 alias_method_chain 来完成:
Taking the plugin example as source here's what you could do:
I did not run this code so I am not sure if this is going to work correctly or not, but in the worst case you could alias the vote method using alias_method_chain or just copy and paste the source to inside the _run_vote_callbacks block (really, really ugly, but it's a solution anyway).
EDIT
This could also be done using alias_method_chain, if the code above does not work: