2012-03-07 05:11:05 +00:00
|
|
|
require 'fastercsv'
|
|
|
|
|
|
|
|
|
2012-03-04 16:51:35 +00:00
|
|
|
class Map < ActiveRecord::Base
|
2012-03-05 14:42:09 +00:00
|
|
|
def self.from_city_name(city_name)
|
|
|
|
self.find_or_initialize_by_name(city_name).save!
|
|
|
|
end
|
2012-03-07 05:11:05 +00:00
|
|
|
|
|
|
|
def self.rand(count = 2)
|
|
|
|
self.find(:all, :order => 'RANDOM()', :limit => count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.import(csv_filename)
|
|
|
|
FasterCSV.parse(open(csv_filename), :headers => true,
|
|
|
|
:header_converters => [:downcase, :symbol]).each do |row|
|
|
|
|
map = self.find_or_initialize_by_name(
|
|
|
|
"#{row[:city]}, #{row[:country]}"
|
|
|
|
)
|
|
|
|
map.save
|
|
|
|
if block_given?
|
|
|
|
yield map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-04 16:51:35 +00:00
|
|
|
end
|