我们这样做是为了在每次控制台启动时询问租户。这需要一些调查,但我们让它工作得相当优雅。请注意,这适用于 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 控制台,它都会做一些事情:
如果您只是希望在每次控制台启动时运行一次,这比需要的要复杂得多。相反,您可以只使用 MyApp::Application 中的 console() 方法,它将运行您想要的任何代码作为the load_console step 的一部分。
module MyApp
class Application < Rails::Application
...
console do
puts "do something"
end
end
end
我们遇到的一个问题是它在打印出环境之前运行代码,所以如果您正在执行任何打印或交互,感觉有点奇怪:
不过,你可能不像我们那么挑剔。做任何让你和你的团队最开心的事情。