Sha256: 6af5211a47b96f0f0eddc8d581e492ab9945bcd0bdfe6721b2d9218a227fedaa

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

require 'data/lu/states'
require 'generators/lookylu/base_generator'

module Lookylu
  module Generators
    class StateDataGenerator < Lookylu::Generators::BaseGenerator

      def populate_data
        connect_to_db
        unless ActiveRecord::Base.connection.table_exists? plural_name
          raise "Table #{plural_name} does not exist. Please build the needed migration and migrate your database"
        end
        begin
          class_object = eval(object_name)
          LookyLu::States.united_states.each do |data|
            class_object.where(data).first_or_create
          end
          puts "There are now #{pluralize(class_object.count, model_name)}"
        rescue => e
          raise "Could not find object for #{model_name}"
        end

      end

      private

      def connect_to_db
        ActiveRecord::Base.establish_connection db_config
      end

      def db_config
        Rails.configuration.database_configuration[::Rails.env]
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
looky-lu-0.0.1 lib/generators/lookylu/state_data_generator.rb