每次启动 Rails 控制台时执行命令

5
我有一个设置命令,我希望每次启动rails console时执行。
MyClass.some_method()

每次启动rails c时,我都厌倦了重新输入它 - 有没有办法在每次启动新控制台时自动运行它?

谢谢!


有什么阻止你编写一个别名,在启动应用程序时执行所需操作的吗?这是特定于一个应用程序还是您编写的所有应用程序都适用? - MageeWorld
你尝试过这个吗?https://dev59.com/ZGoy5IYBdhLWcg3wcNvh - Mauro Dias
1
谢谢@MauroDias - 但是我想在Rails 控制台启动后运行代码,而不是Rails服务器。 - user2490003
3个回答

12
我们这样做是为了在控制台启动时每次请求租户。经过一些调查,我们使它的工作相当优雅。请注意,这适用于Rails 5.2,但自Rails 4以来基本上都是同样的方式。
另一个需要注意的事项是,这是专门编写的,因为我们希望能够在启动时运行该方法一次,然后能够在使用控制台时再次运行它,例如,如果我们想在会话期间切换租户。
第一步是在lib文件中创建一组模块和类。以下是从我们的示例中提取的示例:
# lib/console_extension.rb
module ConsoleExtension
  # This module provides methods that are only available in the console
  module ConsoleHelpers
    def do_someting
      puts "doing something"
    end
  end

  # This is a simple class that allows us access to the ConsoleHelpers before
  # we get into the console
  class ConsoleRunner
    include ConsoleExtension::ConsoleHelpers
  end

  # This is specifically to patch into the startup behavior for the console.
  #
  # In the console_command.rb file, it does this right before start:
  #
  # if defined?(console::ExtendCommandBundle)
  #   console::ExtendCommandBundle.include(Rails::ConsoleMethods)
  # end
  #
  # This is a little tricky. We're defining an included method on this module
  # so that the Rails::ConsoleMethods module gets a self.included method.
  #
  # This causes the Rails::ConsoleMethods to run this code when it's included
  # in the console::ExtendCommandBundle at the last step before the console
  # starts, instead of during the earlier load_console stage.
  module ConsoleMethods
    def included(_klass)
      ConsoleExtension::ConsoleRunner.new.do_someting
    end
  end
end

下一步是将以下内容添加到你的application.rb文件中:
module MyApp
  class Application < Rails::Application
    ...

    console do
      require 'console_extension' # lib/console_extension.rb
      Rails::ConsoleMethods.send :include, ConsoleExtension::ConsoleHelpers
      Rails::ConsoleMethods.send :extend, ConsoleExtension::ConsoleMethods
    end
  end
end

现在,每次运行rails控制台时,它都会执行一些操作:

enter image description here

如果你只是想在控制台启动时运行某个代码片段,那么这样做比较复杂。相反,你可以在MyApp::Application中使用console()方法,它将作为"load_console"步骤的一部分来运行你希望运行的任何代码。(参考链接)

module MyApp
  class Application < Rails::Application
    ...

    console do
      puts "do something"
    end
  end
end

我们遇到的一个问题是在打印环境之前运行代码,如果你要进行任何打印或交互,它会感觉有点奇怪:

enter image description here

当然,你可能没有我们这么讲究。做任何让你和你的团队感到最幸福的事情。


2

我不知道这是否是一种好的实践方法,但是您可以像Aditya所回答的那样在控制台上检查服务器是否正在运行。

if defined?(Rails::Console)
  MyClass.some_method()
end

请注意,在运行类似Swartz的Spring期间进行Rails初始化时,此方法将不起作用。

1
希望这也能对你有所帮助:https://dev59.com/NInda4cB1Zd3GeqPD8hT - Mauro Dias

0
我会尝试为此创建一个Rake任务,并在after_initialize中调用它。
config.after_initialize do
  IndividualProject::Application.load_tasks
  Rake::Task[ 'foo:bar' ].invoke
end

1
我可以将这个功能添加到应用程序中,但我希望只在启动Rails控制台时运行它,而不是在启动应用程序(Rails服务器)时运行。有没有办法我可以找出正在运行的内容,以便我可以将其包装在“if”块中? - user2490003
每次运行控制台时,您希望它执行什么操作? - dan-klasson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接