On the application I am upgrading from Rails 3.2.22.4
to Rails 4.0.13
, the following block of code for enhancing the global environment task has become a road-block by not working on the target Rails version:
Rails.application.class.rake_tasks do
Rake::Task["environment"].enhance do
...
end
end
This works fine on 3.2
, but fails with Don't know how to build task 'environment'
error message in 4.0
.
In 3.2, Rails.application.class.rake_tasks
returns a Proc object
( [#<Proc:0x007f978602a470@.../lib/rails/application.rb:301>]
) pointing to this line in the rails codebase. On 4.0, it returns an empty array.
The line referred to in the above Proc object
seems to be removed in this commit.
What would the preferred way to enhance the environment
rake task be in Rails 4.x
?
The above piece of code is in the lib/subdomain/rake.rb
file, and it is include with the following code in lib/subdomain/engine.rb
:
module Subdomain
class Engine < Rails::Engine
...
rake_tasks do |_app|
require 'subdomain/rake'
end
...
end
end
Rake tasks can't be executed as the command fails with this error. rails server|console
commands work ok.
Option 1
If I'm understanding the question properly, something like this should work by placing these tasks in a standard location like
lib/tasks/environment.rake
. Note: None of this is particularly Rails-specific.The output of this would be:
Option 2
However, the question remains as to why
:environment
needed to be extended. If it's to trigger something before, say, adb:migrate
, you might be better off just re-opening the task in question and adding another dependency to that particular task. For example:The result of this is:
C-C-C-Combo Breaker!!
Combining everything, the output would look like this: