1

Using rails 4.2.1.

I want to call a rake task on every config/routes.rb file save. How would I go about doing this?

Note: the rake task does require the rails environment. i.e. it's of the task :task_name => :environment do #... nature.

Derek
  • 11,980
  • 26
  • 103
  • 162
  • using something like `guard` I guess – apneadiving May 13 '15 at 07:14
  • how the going with the issue? – Andrey Deineko May 13 '15 at 16:11
  • @AndreyDeineko Not solved yet. I always thought that `rails` itself detects a change to the `routes.rb` file and runs something like `Rails.application.reload_routes!`. I want to find the function that calls `reload_routes!` and extend it to also run my `rake` task. No luck yet. – Derek May 13 '15 at 16:16

2 Answers2

1

Take a look into filewatcher gem.

You can define actions to be taken depending on what changes are made to specified file (or directory):

require 'filewatcher'

FileWatcher.new("config/routes.rb").watch() do |filename, event|
  if(event == :changed)
    your_rake_task
  end
  if(event == :delete)
    # ...
  end
  if(event == :new)
    # ...
  end
end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
1

In rails 4.2.1

# development.rb
require 'rake'
Rails.application.configure do
#...
  Rails.application.load_tasks
  # code executes on routes change
  ActionDispatch::Reloader.to_prepare do
    Rails.application.reload_routes!

    if JsRoutes.assert_usable_configuration!
      Rake::Task['js_routes:generate'].reenable
      Rake::Task['js_routes:generate'].invoke
    end
  end
#...
end

Based off of https://stackoverflow.com/a/27046271/749477

Community
  • 1
  • 1
Derek
  • 11,980
  • 26
  • 103
  • 162