是否可以在 rake 任务中包含模块并使其方法可用于 rails 应用程序中的任务? [英] Is it possible to include modules in rake task and make its methods available for the task in rails app?

查看:22
本文介绍了是否可以在 rake 任务中包含模块并使其方法可用于 rails 应用程序中的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的 rails 3.2.12 应用程序中,在 lib/tasks 下创建了一个 rake 任务.rake 任务 需要调用一个方法 find_config(),该方法驻留在另一个 rails 模块 authentify 中(模块不在/lib/下).我们可以在 rake 任务包含 Authentify 并使方法 find_config() 可用于在 rake 任务中调用吗?

In our rails 3.2.12 app, there is a rake task created under lib/tasks. The rake task needs to call a method find_config() which resides in another rails module authentify (module is not under /lib/). Can we include Authentify in rake task and make method find_config() available to call in the rake task?

这是我们想要在 rake 任务中做的事情:

Here is what we would like to do in the rake task:

include Authentify
config = Authentify::find_config()

感谢您的评论.

推荐答案

 require 'modules/module_name'

 include ModuleName

 namespace :rake_name do

 desc "description of rake task"

  task example_task: :environment do

   result = ModuleName::method_name()


  end #end task


 end

这对我有用.由于您的模块不在/lib 中,您可能需要编辑它的要求.但它应该工作.希望有帮助.

This works for me. Since your Module is not in /lib you might have to edit how it is required. But it should work. Hope it helps.

这篇关于是否可以在 rake 任务中包含模块并使其方法可用于 rails 应用程序中的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆