Rails,创建回调

发布于 2024-11-25 16:15:14 字数 637 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-12-02 16:15:14

将插件示例作为源代码,您可以执行以下操作:

class Post
  include Mongoid::Document
  include Mongo::Voteable
  extend ActiveModel::Callbacks

  define_model_callbacks :vote

  # set points for each vote
  voteable self, :up => +1, :down => -1

  def vote(options, value = nil)
    _run_vote_callbacks do
      super( options, value )
    end
  end

end

我没有运行此代码,因此我不确定这是否能正常工作,但在最坏的情况下,您可以使用 alias_method_chain< 为投票方法添加别名/strong> 或者只是将源代码复制并粘贴到 _run_vote_callbacks 块内(真的非常难看,但无论如何它都是一个解决方案)。

编辑

如果上面的代码不起作用,也可以使用 alias_method_chain 来完成:

class Post
  include Mongoid::Document
  include Mongo::Voteable
  extend ActiveModel::Callbacks

  define_model_callbacks :vote

  # set points for each vote
  voteable self, :up => +1, :down => -1

  alias_method_chain :vote, :callback

  def vote_with_callback(options, value = nil)
    _run_vote_callbacks do
      vote_without_callbacks( options, value )
    end
  end

end

Taking the plugin example as source here's what you could do:

class Post
  include Mongoid::Document
  include Mongo::Voteable
  extend ActiveModel::Callbacks

  define_model_callbacks :vote

  # set points for each vote
  voteable self, :up => +1, :down => -1

  def vote(options, value = nil)
    _run_vote_callbacks do
      super( options, value )
    end
  end

end

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:

class Post
  include Mongoid::Document
  include Mongo::Voteable
  extend ActiveModel::Callbacks

  define_model_callbacks :vote

  # set points for each vote
  voteable self, :up => +1, :down => -1

  alias_method_chain :vote, :callback

  def vote_with_callback(options, value = nil)
    _run_vote_callbacks do
      vote_without_callbacks( options, value )
    end
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文