How can I tell if Rails code is being run via rake or script/generate?

前端 未结 1 1065
抹茶落季
抹茶落季 2021-02-11 16:42

I\'ve got a plugin that is a bit heavy-weight. (Bullet, configured with Growl notifications.) I\'d like to not enable it if I\'m just running a rake task or a generator, since i

相关标签:
1条回答
  • 2021-02-11 17:20

    It's as simple as that:

    if $rails_rake_task
      puts 'Guess what, I`m running from Rake'
    else
      puts 'No; this is not a Rake task'
    end
    

    Rails 4+

    Instead of $rails_rake_task, use:

    File.basename($0) == 'rake'
    
    0 讨论(0)
提交回复
热议问题