Skip to content

Instantly share code, notes, and snippets.

@DKRetzlaff
Created September 25, 2017 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DKRetzlaff/5dbfc73f2b99053ec46234a9e8aea3ca to your computer and use it in GitHub Desktop.
Save DKRetzlaff/5dbfc73f2b99053ec46234a9e8aea3ca to your computer and use it in GitHub Desktop.
adapter: mysql2
encoding: utf8
pool: 5
database: surveyor_development
schema_search_path: public
username: root
password: root
source "https://rubygems.org"
gem 'activerecord', '~> 5.1'
gem 'mysql2', '~> 0.4'
gem 'spreadsheet'
require 'active_record'
require 'yaml'
require 'spreadsheet'
require_relative 'models/application_record.rb'
require_relative 'models/option.rb'
require_relative 'models/question.rb'
require_relative 'models/survey.rb'
require_relative 'models/tag.rb'
db_config = YAML::load(File.open("database.development.yml"))
ActiveRecord::Base.establish_connection(db_config)
attributes_classification = Spreadsheet.open('files/classificacao_atributos.xls')
sheet = attributes_classification.worksheet('Classificação')
options = {}
sheet.each do |row|
next if (row[0].nil? || row[0].eql?('Atributo'))
if(!options.has_key?(row[0]))
options[row[0]] = []
end
rank = 1
(1..3).each do | i |
option_name = !row[i].eql?('-') ? row[i] : ''
options[row[0]] << ["#{option_name}", rank]
rank += 1
options[row[0]] << ['', rank]
rank += 1
end
options[row[0]].pop
end
attributes = Spreadsheet.open('files/atributos_final.xls')
surveys = []
sheet = attributes.worksheet('Plan1')
sheet.each do |row|
next if (row[0].nil? || row[0].eql?('Grupo'))
tags = ["Grupo/#{row[0].strip}", "SubGrupo/#{row[1].strip}","Categoria/#{row[2].strip}"]
questions = []
(5..9).each do | i |
!row[i].eql?('-') ? (questions << ["#{row[i]}", options[row[i]]]) : nil
end
surveys << {tags: tags, questions: questions}
end
Survey.delete_all
surveys.each_with_index do | survey, i |
survey_reference = Survey.create(store_id: 619, title: "Enquete #{i}")
survey[:tags].each do | tag_name |
Tag.create(name: tag_name, survey_id: survey_reference.id)
end
survey[:questions].each do | question |
question_reference = Question.create(kind: 'likert', description: question[0], survey_id: survey_reference.id)
question[1].each do | option |
Option.create(question_id: question_reference.id, description: option[0], weight: option[1])
end
end
end
#models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
#models/option.rb
class Option < ApplicationRecord
belongs_to :question
has_many :answers
end
#models/question.rb
class Question < ApplicationRecord
belongs_to :survey
has_many :options
scope :likerts, -> { where(kind: 'likert') }
scope :recommendations, -> { where(kind: 'recommendation') }
def recommendation?
kind == 'recommendation'
end
end
#models/survey.rb
class Survey < ApplicationRecord
has_many :answers
has_many :questions
has_many :tags
end
#models/tag.rb
class Tag < ApplicationRecord
belongs_to :survey
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment