Skip to content

Instantly share code, notes, and snippets.

@mariovisic
Created July 7, 2011 06:09
Show Gist options
  • Select an option

  • Save mariovisic/1068978 to your computer and use it in GitHub Desktop.

Select an option

Save mariovisic/1068978 to your computer and use it in GitHub Desktop.
require 'factory_girl'
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
seed_file = Rails.root.join('db', 'seed.yml')
objects = YAML::load_file(seed_file)
# Create a member login for development and staging
if Rails.env.development? || Rails.env.staging?
[Address, Member, Admin].each do |model|
name = model.to_s.pluralize.underscore
if model.count < 1 then
puts "** Creating #{name}"
if model == Member
puts "** Member created with login: 'user@example.com' and password: 'password'"
elsif model == Admin
puts "** Admin created with login: 'admin@example.com' and password: 'password'"
end
model.create!(objects[name])
else
puts "** Skipping #{name}, already created"
end
end
if Product.count < 1 then
puts "** Importing old site data"
`psql #{ActiveRecord::Base.connection.current_database} -a -f db/dump/partial_export_old_site.sql`
Product.all.each do |product|
subscription = product.subscriptions.build(:member => Member.last)
subscription.save(false)
end
# Remove all of the answers without a body
Answer.joins(:translations).where(:translations => { :body => nil }).destroy_all
end
# Reset all of the primary key sequences
[Answer, Product, Question, SubjectQuestion, Subject, Topic].each do |model|
ActiveRecord::Base.connection.reset_pk_sequence!(model.table_name)
end
# Create mock exams for testing
if MockExam.count < 1 then
first_mock_exam = Factory.build(:mock_exam, :total_questions => 90, :product => Product.first, :title => "Quick Fire MCQ Exam", :sections => [])
first_mock_exam.sections = [ Factory.build(:mock_exam_section, :mock_exam => first_mock_exam, :total_questions => 90, :subject => Product.first.topics.first.subjects.first) ]
first_mock_exam.save!
second_mock_exam = Factory.build(:mock_exam, :total_questions => 90, :product => Product.offset(1).first, :title => "Quick Fire ACEM Exam", :sections => [])
second_mock_exam.sections = [ Factory.build(:mock_exam_section, :mock_exam => second_mock_exam, :total_questions => 90, :subject => Product.offset(1).first.topics.first.subjects.first) ]
second_mock_exam.save!
end
# Add a translation for the first question
question = Product.first.topics.first.subjects.first.questions.last
if question.translations.size <= 1 then
question.translations.create!(:locale => 'de', :body => 'German translation for the first question')
question.answers.each_with_index do |answer, index|
answer.translations.create!(:locale => 'de', :body => "Translation for answer #{index + 1}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment