class Mexican::States::SeedGenerator

Seed generator class

Public Instance Methods

seed_models() click to toggle source
# File lib/generators/mexican/states/seed_generator.rb, line 18
def seed_models
  root = File.expand_path '../../../../support/', __FILE__
  Dir.glob(root + '/*.csv') do |rb_file|
    state = rb_file.scan(/[á-óa-zA-Z]+[_[á-óa-zA-Z]+]*/)[-3]
    if states.empty? || states.include?(state)
      puts "#{Rainbow('Installing').red} #{Rainbow(state).green}"
      save_datum(File.join(rb_file))
    end
  end
end

Private Instance Methods

save_datum(file) click to toggle source
# File lib/generators/mexican/states/seed_generator.rb, line 31
def save_datum(file)
  CSV.foreach(file) do |row|
    city_name = row[2]
    neighborhood_name = row[1]
    neighborhood_zip_code = row[0]
    state = State.find_or_create_by(name: row[3])
    city = City.find_or_create_by(name: city_name, state_id: state.id)
    Neighborhood.find_or_create_by(name: neighborhood_name,
                                   zip_code: neighborhood_zip_code,
                                   state_id: state.id,
                                   city_id: city.id)
  end
end