在插件中的过滤器之前运行。还有其他办法吗?

发布于 2024-11-09 22:35:34 字数 402 浏览 3 评论 0原文

我正在编写我的第一个插件,在该插件中,我需要为某些控制器/操作对运行一个方法。对于这个插件,配置 yml 看起来像这样 -

track1:
   start_action: "home", "index"
   end_action: "user", "create"

所以,在我的插件中,我将首先阅读上面的 yml 文件。之后,我想运行一个操作,比如 -first_function 作为 before_filter 到 home-controller 索引操作,并且我将运行 secondary_function 作为 after_filter 用于用户控制器创建操作。

但我不知道如何为此编写过滤器,该过滤器将在插件中声明,并将针对用户在上述 yml 文件中指定的操作运行。

请帮忙!

I am writing my first plugin, and in that plugin, I need to run a method for some controller/action pairs. For this plugin the configuration yml looks like this -

track1:
   start_action: "home", "index"
   end_action: "user", "create"

So, in my plugin I will first read above yml file. After that I want to run an action say - first_function as before_filter to home-controller index-action and I would be running second_function as after_filter for user-controller create-action.

But I couldn't figure out how can I write filters for this, which will be declared in plugin and will run for actions specified by user in above yml files.

Please help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

极致的悲 2024-11-16 22:35:34

我看到有两种选择可以实现您的目标。

第一种方法:在 ApplicationController 内声明过滤器,并在过滤器内检查 controller_nameaction_name 是否与 yaml 配置中的任何内容匹配。如果匹配则执行,如果不匹配则忽略。

在代码中

class ApplicationController

  before_filter :start_action_with_check
  after_filter :end_action_with_check

  def start_action_with_check
    c_name, a_name = CONFIG['track1']['start_action'].split(', ')
    if c_name == controller_name && a_name == action_name 
      do_start_action
    end
  end

  ...

我希望你能明白。

第二种方法:定义 before_filter 的一种简洁方法是在模块中定义它们。通常您会使用 self.included 方法来定义 before_filter ,但当然,您可以有条件地定义它们。例如:

class HomeController < ApplicationController

  include StartOrEndAction

  ...
end

lib/start_or_end_action.rb 中编写

module StartOrEndAction

  def self.included(base)
    # e.g. for the start-action        
    c_name, a_name CONFIG['track1']['start_action'].split(', ')
    if c_name == controller_name && a_name == action_name 
      base.before_filter :do_start_action
    end
    # and do the same for the after_filter
  end

  def do_start_action
    ...
  end

  def do_end_action
    ...
  end
end 

第二个解决方案的优点是 before_filterafter_filter 仅在需要时定义。缺点是您必须将该模块包含到每个控制器中,您可以在其中配置要发生的前置过滤器。
第一种方法的优点是可以覆盖任何控制器,并且检查前后过滤器会产生一些开销。

希望这有帮助。

I see two options to reach your goal.

First approach: declare both the filters inside the ApplicationController and inside the filter check whether the controller_name and action_name match any in your yaml-configuration. If they match, execute it, if not ignore.

In code that would like

class ApplicationController

  before_filter :start_action_with_check
  after_filter :end_action_with_check

  def start_action_with_check
    c_name, a_name = CONFIG['track1']['start_action'].split(', ')
    if c_name == controller_name && a_name == action_name 
      do_start_action
    end
  end

  ...

I hope you get the idea.

Second approach: a clean way to define before_filter is to define them in a module. Normally you would use the self.included method to define the before_filter, but of course, you can define them conditionally. For example:

class HomeController < ApplicationController

  include StartOrEndAction

  ...
end

and in lib/start_or_end_action.rb you write

module StartOrEndAction

  def self.included(base)
    # e.g. for the start-action        
    c_name, a_name CONFIG['track1']['start_action'].split(', ')
    if c_name == controller_name && a_name == action_name 
      base.before_filter :do_start_action
    end
    # and do the same for the after_filter
  end

  def do_start_action
    ...
  end

  def do_end_action
    ...
  end
end 

The advantage of the second solution is that the before_filter and after_filter are only defined when needed. The disadvantage is that you will have to include the module into each controller where you could possible configure the before-filter to take place.
The first has the advantage that any controller is covered, and you get a little overhead checking the before- and after filter.

Hope this helps.

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