# frozen_string_literals: true require "rails/version" require "jobshop/cli" require "jobshop/cli/spinner" module Jobshop class DummyApp WIDGET_TYPES = [ "A", "B", "C" ] WIDGET_SHAPES = [ "triangle", "square", "pentagon", "hexagon", "heptagon" ] WIDGET_COLLECTION_SCHEMA = { "properties": { "shape": { "title": "Widget Shape", "description": "The shape of the widget", "enum": WIDGET_SHAPES }, "size": { "title": "Nominal Size", "description": "Nominal size of new widget", "type": "number" } }, "required": [ "shape", "size" ], "additionalProperties": false } WIDGET_PACK_POSITIONS = [ "A", "B", "C", "D" ] WIDGET_PACK_COLLECTION_SCHEMA = { "properties": { "widgets": { "type": "object", "patternProperties": { "^[A-Z]$": { "type": "string", "format": "uri" } }, "additionalProperties": false } }, "additionalProperties": false } class << self include CLI::UI def destroy! FileUtils.rmtree(path) end def exist? Dir.exist?(path) end def rakefile File.join(path, "Rakefile") end def path @path ||= File.expand_path("spec/dummy") end def factory_paths [ Jobshop::Engine.root.join("spec", "factories") ] end def seed # rubocop:disable Metrics/MethodLength with_dummy_app do do_with_spinner("Loading factories") do require "factory_bot_rails" FactoryBot.definition_file_paths = factory_paths FactoryBot.find_definitions if Rails.env.development? end do_with_spinner("Cleaning database") do clean_database end @org = do_with_spinner("Creating test Organization") do FactoryBot.create(:organization) end @ceo = do_with_spinner("Creating test Person") do FactoryBot.create(:person, :ceo, organization: @org) end @places = do_with_spinner("Creating test Places") do FactoryBot.create_list(:place, 10, organization: @org) end @companies = do_with_spinner("Creating test companies") do FactoryBot.create_list(:company, 10, organization: @org, created_by: @ceo) end @products = do_with_spinner("Creating test Products") do FactoryBot.create_list(:product, 10, organization: @org, created_by: @ceo) end do_with_spinner("Creating test Routers for Products") do @products.each do |product| rp = FactoryBot.create(:routing_process, organization: @org, product: product) FactoryBot.create_list(:routing_step, rand(1..4),organization: @org, routing_process: rp) end end do_with_spinner("Creating sample Orders") do 50.times do ord = FactoryBot.create(:order, organization: @org, company: @companies.sample, created_by: @ceo) FactoryBot.create_list(:order_line, rand(1..4), organization: @org, order: ord, product: @products.sample, created_by: @ceo) end end @widgets = do_with_spinner("Creating Collection of widgets") do wc = FactoryBot.create(:widget_collection, organization: @org) FactoryBot.create_list(:widget_thing, 50, organization: @org, collection: wc) end @widget_packs = do_with_spinner("Creating Collection of widget packs") do wpc = FactoryBot.create(:widget_pack_collection, organization: @org) FactoryBot.create_list(:widget_pack_thing, 50, organization: @org, collection: wpc, widgets: @widgets) end do_with_spinner("Creating Inspection Reports") do 5.times do report = FactoryBot.create(:inspection_report, organization: @org) 5.times do |index| FactoryBot.create(Jobshop::Inspection::CRITERION_TYPES.sample, organization: @org, report: report, position: (index + 1)) end 5.times do |index| tuple = FactoryBot.create(:inspection_tuple, organization: @org, report: report, position: (index + 1)) report.criteria.each do |criterion| FactoryBot.create(:inspection_result, organization: @org, report: report, criterion: criterion, tuple: tuple) end end end end do_with_spinner("Creating RFQ Mailman") do @org.mailmen.create!(address: "rfq@jobshop.io", handler_type: "Jobshop::RFQHandler") end do_with_spinner("Creating sample RFQs") do 50.times do FactoryBot.create(:rfq, organization: @org, company: @companies.sample) end end end end private def with_dummy_app(*) begin require "#{Jobshop::DummyApp.path}/config/environment" rescue LoadError abort "Dummy app does not exist. Run `jobshop dummy` to create it." end yield end private def clean_database require "database_cleaner" DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean end end end end