Я пытаюсь сделать обезьяна патч для граблями задач в Rails 3.2 и я хочу, чтобы реализовать это в гем (назовем его my_plugin)обезьяны патч рельсы 3,2 грабли задача
Например, databases.rake имеет следующее содержание:
db_namespace = namespace :db do
# ...
# desc "Retrieves the charset for the current environment's database"
task :charset => [:environment, :load_config] do
config = ActiveRecord::Base.configurations[Rails.env]
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.charset
when /postgresql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /sqlite/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
else
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
# ...
end
И я хочу, чтобы заменить стандартное поведение на мине:
db_namespace = namespace :db do
# ...
# desc "Retrieves the charset for the current environment's database"
task :charset => [:environment, :load_config] do
puts 'Here is my gem override'
config = ActiveRecord::Base.configurations[Rails.env]
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.charset
when /postgresql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /sqlite/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /OTHER_ADAPTER/
puts 'OTHER_ADAPTER_ENCODING'
else
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
# ...
end
Итак, я пишу следующий код в my_plugin:
Файл: my_plugin.rb
if defined?(Rake)
Rake::TaskManager.class_eval do
def replace_task(task_name, task_scope)
scope_backup = @scope
@scope = Rake::Scope.new(task_scope)
task_name_full = @scope.path_with_task_name(task_name)
@tasks[task_name_full] = yield
@scope = scope_backup
end
end
Rake.application.replace_task('charset', 'db') do
task :charset => [:environment, :load_config] do
puts 'Here is my gem override'
config = ActiveRecord::Base.configurations[Rails.env]
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.charset
when /postgresql/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /redshift/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /sqlite/
ActiveRecord::Base.establish_connection(config)
puts ActiveRecord::Base.connection.encoding
when /OTHER_ADAPTER/
puts 'OTHER_ADAPTER_ENCODING'
else
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
end
end
Но когда я называю расслоение EXEC грабли БД: кодировок:
** Invoke db:charset (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:charset
UTF8 -> First call from rails default method
Here is my gem override
UTF8 -> Second call, my monkey patch
Shutdown completed cleanly
Почему это первый звонок?