Make rake task from gem available everywhere?

后端 未结 5 1961
独厮守ぢ
独厮守ぢ 2020-12-24 07:17

So I\'m writing a small gem and I have a \'/tasks\' dir in it with some specific rake tasks. How do I make those tasks available automatically everywhere, where the gem is r

5条回答
  •  眼角桃花
    2020-12-24 07:46

    For Rails3 applications, you might want to look into making a Railtie for your gem.

    You can do so with:

    lib/your_gem/railtie.rb

    require 'your_gem'
    require 'rails'
    module YourGem
      class Railtie < Rails::Railtie
        rake_tasks do
          require 'path/to/rake.task'
        end
      end
    end
    

    lib/your_gem.rb

    module YourGem
      require "lib/your_gem/railtie" if defined?(Rails)
    end
    

    Though, I had my share of difficulties with requiring the rake.task file in my railtie.rb. I opted to just define my measley one or two tasks within the rake_tasks block.

提交回复
热议问题