每次 rails 控制台启动时都执行一个命令 [英] Executing a command every time the rails console starts

查看:48
本文介绍了每次 rails 控制台启动时都执行一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置命令,我想每次我启动rails控制台 -

MyClass.some_method()

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

谢谢!

解决方案

我们这样做是为了在每次控制台启动时询问租户.这需要一些调查,但我们让它工作得相当优雅.请注意,这适用于 Rails 5.2,但自 Rails 4 以来,它的工作方式大致相同.

另外要注意的是,这是专门编写的,因为我们希望能够在启动时运行该方法一次,然后能够在使用控制台时再次运行它,比如我们是否想在会话期间切换租户.

第一步是在一个 lib 文件中创建一组模块和类.这是从我们的示例中提取的示例:

# lib/console_extension.rb模块控制台扩展# 该模块提供了只能在控制台中使用的方法模块控制台助手def do_someting把做某事"结尾结尾# 这是一个简单的类,允许我们之前访问ConsoleHelpers# 我们进入控制台类 ConsoleRunner包括 ConsoleExtension::ConsoleHelpers结尾# 这专门用于修补控制台的启动行为.## 在 console_command.rb 文件中,它在启动之前执行此操作:## 如果已定义?(控制台::ExtendCommandBundle)# console::ExtendCommandBundle.include(Rails::ConsoleMethods)# 结尾## 这有点棘手.我们正在这个模块上定义一个包含的方法# 以便 Rails::ConsoleMethods 模块获得一个 self.included 方法.## 这会导致 Rails::ConsoleMethods 在包含此代码时运行它# 在控制台中::ExtendCommandBundle 在控制台之前的最后一步# 开始,而不是在较早的 load_console 阶段.模块控制台方法包含定义(_klass)ConsoleExtension::ConsoleRunner.new.do_someting结尾结尾结尾

下一步是将以下内容添加到您的 application.rb 文件中:

模块 MyApp类应用

现在,每次运行 rails 控制台时,它都会做一些事情:

如果您只是想在每次控制台启动时运行一次,这将比实际需要的更复杂.相反,您可以在 MyApp::Application 中使用 console() 方法,它将运行您想要的任何代码作为

你可能不像我们那么挑剔.做任何让你和你的团队最快乐的事情.

I have a setup command that I want executed every time I start the rails console -

MyClass.some_method()

I get tired of retyping it each time I fire up rails c - is there a way to have it automatically get run every time a new console is started?

Thanks!

解决方案

We do this in order to ask for the tenant every time the console starts. It took a bit of investigation, but we got it working fairly elegantly. Note that this works with Rails 5.2 but it has worked mostly the same way since Rails 4.

Another thing to note is that this is written specifically because we wanted to be able to run the method once on start and then be able to run it again while using the console, say if we wanted to switch the tenant during a session.

The first step is to create a set of modules and classes in a lib file. Here is an example extracted from ours:

# 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

The next step is to add the following into your application.rb file:

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

Now, every time you run rails console, it will do something:

If you're just looking to run something once every time the console starts, this is more complicated than it needs to be. Instead, you can just use the console() method in MyApp::Application and it will run whatever code you want as part of the load_console step.

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

    console do
      puts "do something"
    end
  end
end

One issue we had with this was that it runs the code before it prints out the environment, so if you're doing any printing or interaction it feels a bit weird:

You may not be as picky as we are though. Do whatever makes you and your team the happiest.

这篇关于每次 rails 控制台启动时都执行一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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