seeds.rb revision ac2169141f0b549fc8917a4b1d778f4ba3cab0bf
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# This file should contain all the record creation needed to seed the database with its default values.
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenActiveRecord::Base.logger = Logger.new($stdout)
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Do not create background jobs
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenOntologyVersion.send :alias_method, :parse_async, :parse
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenOopsRequest.send :define_method, :async_run, ->{}
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Remove existing repositories
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenFileUtils.rm_rf Ontohub::Application.config.git_root
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Create Admin User
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Create some other users
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen%w(Bob Alice Carol Dave Ted).each_with_index do |name, i|
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen # add two users to the first team
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Create a repository
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenrepository = Repository.create! \
66fbbac231b0385273b11f9b4c43ad252330dea5Timo Sirainen name: 'Default'
66fbbac231b0385273b11f9b4c43ad252330dea5Timo Sirainen# Add permissions
66fbbac231b0385273b11f9b4c43ad252330dea5Timo SirainenRepository.find_each do |o|
66fbbac231b0385273b11f9b4c43ad252330dea5Timo Sirainen o.permissions.create! \
66fbbac231b0385273b11f9b4c43ad252330dea5Timo Sirainen subject: Team.first,
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen role: 'owner'
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenRepository.first.permissions.create! \
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen subject: User.first,
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen role: 'editor'
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# initially import logics
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenRake::Task['logicgraph:import'].invoke
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Import ontologies
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo SirainenDir["#{Rails.root}/test/fixtures/ontologies/*/*.{casl,clf,clif,owl}"].each do |file|
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen basename = File.basename(file)
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen version = repository.save_file file, basename, "#{basename} added", user
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen version.ontology.update_attribute :description, Faker::Lorem.paragraph
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Add comments
a545bcd54312b543ddc46a872ec79d0416032b0fTimo Sirainen5.times do |n|
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen c = Ontology.first.comments.build \
a545bcd54312b543ddc46a872ec79d0416032b0fTimo Sirainen text: (1 + rand(4)).times.map{ Faker::Lorem.paragraph(5+rand(10)) }.join("\n\n")
a545bcd54312b543ddc46a872ec79d0416032b0fTimo Sirainen c.user = User.first
a545bcd54312b543ddc46a872ec79d0416032b0fTimo Sirainen c.created_at = (60 - n*5).minutes.ago
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen# Add OOPS! requests and responses to pizza ontology
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenontology = Ontology.where(name: "Pizza").first!
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenversion = ontology.versions.first
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenrequest = version.build_request({state: 'done'}, without_protection: true)
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenresponses = %w( Pitfall Warning Warning Suggestion ).map do |type|
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen request.responses.create! \
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen name: Faker::Name.name,
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen description: Faker::Lorem.paragraph,
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainen element_type: type
b28a1c61a5d262fd16b46bebe47dbfb90ac9c5fcTimo Sirainenontology.entities.all.select{ |entity| entity.oops_responses = responses.sample(rand(responses.count)) }