Created
October 25, 2011 18:33
-
-
Save flsafe/1313765 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMMON_WORDS = [] | |
desc 'Move buisnesses to one sub category to another' | |
task :move_businesses, [:from, :to, :heuristic_file] => :environment do |t, args| | |
puts args | |
if valid_arguments?(args) | |
read_heuristic_file(args[:heuristic_file]) | |
move_businesses(args[:from], args[:to]) | |
else | |
print_usage | |
print_whats_wrong_with_args(args) | |
end | |
end | |
def move_businesses(from, to) | |
@from_sub_cat = GCS::SubCategory.first_by_title(from) | |
@to_sub_cat = GCS::SubCategory.first_by_title(to) | |
@to_sub_cat_parent = @to_sub_cat.categories.first | |
@businesses_to_migrate = @from_sub_cat.businesses | |
result = move_and_collect_failed(@businesses_to_migrate) | |
print_results(result) | |
end | |
def move_and_collect_failed(businesses_to_migrate) | |
success_count = 0; | |
failed_to_save = [] | |
businesses_to_migrate.each do |biz| | |
if heuristic_says_move?(biz) | |
move_to_sub_category(biz) | |
move_to_category(biz) | |
if biz.save | |
success_count += 1 | |
else | |
failed_to_save << biz | |
end | |
end | |
end | |
{failed:failed_to_save, success:success_count} | |
end | |
def heuristic_says_move?(biz) | |
COMMON_WORDS.detect{|w| biz.name =~ /#{w}/xi} | |
end | |
def move_to_sub_category(biz) | |
biz.sub_categories.delete_if{|cat| cat.title == @from_sub_cat.title} | |
biz.sub_categories << @to_sub_cat | |
biz.primary_sub_cat = @to_sub_cat | |
end | |
def move_to_category(biz) | |
biz.categories << @to_sub_cat_parent | |
biz.primary_cat = @to_sub_cat_parent | |
end | |
def valid_arguments?(args) | |
return false unless args[:from] and args[:to] and args[:heuristic_file] and File.exists?(args[:heuristic_file]) and !File.zero?(args[:heuristic_file]) | |
from_sub_cat = GCS::SubCategory.first_by_title(args[:from]) | |
to_sub_cat = GCS::SubCategory.first_by_title(args[:to]) | |
return false unless from_sub_cat and to_sub_cat | |
true | |
end | |
def print_whats_wrong_with_args(args) | |
puts "You did not specify the sub category to move busnineses from." if !args[:from] | |
puts "You did not specify the sub category to move busnineses to." if !args[:to] | |
from_sub_cat = GCS::SubCategory.first_by_title(args[:from]) | |
to_sub_cat = GCS::SubCategory.first_by_title(args[:to]) | |
puts "The sub category '#{args[:from]}' doesn't exist in the database." if !from_sub_cat | |
puts "The category '#{args[:to]}' doesn't exist in the database." if !to_sub_cat | |
puts "The file #{args[:heuristic_file]} wasn't found." if !File.exists?(args[:heuristic_file]||'') | |
puts "The file #{args[:heuristic_file]} empty." if File.zero?(args[:heuristic_file]||'') | |
end | |
def print_usage | |
puts "Usage: rake move_businesses[<from_sub_cat>,<to_sub_cat>,<filename>]" | |
puts "Make sure the both sub categories and the heuristic file exists and is not empty." | |
end | |
def print_results(results) | |
failed_to_save = results[:failed] | |
success = results[:success] | |
puts "Moved #{success} businesses out of #{@businesses_to_migrate.count}" | |
failed_to_save.each do |failed| | |
puts "Failed to save #{failed.name}" | |
failed.errors.each{|e| puts e} | |
end | |
end | |
def read_heuristic_file(filename) | |
File.open(filename, 'r') do |f| | |
f.each_line do |l| | |
COMMON_WORDS << l.split[0] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment