Skip to content
Snippets Groups Projects
Commit 0f6ebfea authored by brendandillon's avatar brendandillon Committed by Alex Reisner
Browse files

Use manual batching in rake task (#1140)

Changes batching in the rake task to work using a loop instead of the find_each method which does not work with Mongoid. This should fix #1126.
parent b7515a1a
No related branches found
No related tags found
No related merge requests found
require "geocoder/models/mongoid"
namespace :geocode do
desc "Geocode all objects without coordinates."
task :all => :environment do
......@@ -8,22 +10,28 @@ namespace :geocode do
raise "Please specify a CLASS (model)" unless class_name
klass = class_from_string(class_name)
batch = (batch.to_i unless batch.nil?) || 1000
orm = (klass < Geocoder::Model::Mongoid) ? 'mongoid' : 'active_record'
reverse = false unless reverse.to_s.downcase == 'true'
if reverse
klass.not_reverse_geocoded.find_each(batch_size: batch) do |obj|
obj.reverse_geocode; obj.save
sleep(sleep_timer.to_f) unless sleep_timer.nil?
geocode_record = lambda { |obj|
reverse ? obj.reverse_geocode : obj.geocode
obj.save
sleep(sleep_timer.to_f) unless sleep_timer.nil?
}
scope = reverse ? klass.not_reverse_geocoded : klass.not_geocoded
if orm == 'mongoid'
scope.each do |obj|
geocode_record.call(obj)
end
else
klass.not_geocoded.find_each(batch_size: batch) do |obj|
obj.geocode; obj.save
sleep(sleep_timer.to_f) unless sleep_timer.nil?
elsif orm == 'active_record'
scope.find_each(batch_size: batch) do |obj|
geocode_record.call(obj)
end
end
end
end
##
# Get a class object from the string given in the shell environment.
# Similar to ActiveSupport's +constantize+ method.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment