Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.95 KB | None | 0 0
  1. namespace :db do
  2.   desc "Import maps from userlevel format. When passing the file path argument, escape the [] with \."
  3.   task :import_userlevels, :file, :needs => :environment do |t, args|
  4.     path = "#{Rails.root}/#{args["file"]}"
  5.     unless File.exist?(path)
  6.       puts "That file could not be found. (Looking for #{path})"
  7.       exit
  8.     else
  9.       f = File.new(path).read
  10.       maps = f.split(/[\$]/).map{|x|x.split("#")}
  11.       for m in maps[1..maps.length] do
  12.         map = Map.create(:title => m[0], :mapdata => m[3])
  13.         map.tag_list = m[2]
  14.         if((user = User.where(:login => m[1])[0]).nil?)
  15.           puts "Couldn't find user #{m[1]} in the database, creating."
  16.           user = User.create(:login => m[1], :crypted_password => "", :password_salt => "")
  17.           user.persistence_token = ""
  18.           user.save(false)
  19.         end
  20.         map.users << user
  21.         map.save
  22.       end
  23.       puts "All imported."
  24.     end
  25.   end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement