diff --git a/app/services/spike/form_generator_service.rb b/app/services/spike/form_generator_service.rb new file mode 100644 index 000000000..0a6a07fb6 --- /dev/null +++ b/app/services/spike/form_generator_service.rb @@ -0,0 +1,92 @@ +module Spike + class FormGeneratorService + def initialize; end + + def call + form = Form.new("config/forms/2022_2023.json") + + form.sections.reject { |subsection| subsection.id == "setup" }.each do |section| + section.subsections.each do |subsection| + subsection.pages.each do |page| + page.questions.each do |question| + create_question(question) + end + create_page(page) + end + create_subsection(subsection) + end + create_section(section) + end + end + + def create_section(section) + subsections_array = section.subsections.map { |s| "Form::Lettings::Subsections::#{section.id.camelize}::#{s.id.camelize}.new(nil, nil, self)" } + out_file = File.new("app/models/form/lettings/sections/#{section.id}.rb", "w") + out_file.puts("class Form::Lettings::Sections::#{section.id.camelize} < ::Form::Section + def initialize(id, hsh, form) + super + @id = \"#{section.id}\" + @label = \"#{section.id}\" + @description = \"#{section.description}\" + @form = form + @subsections = #{subsections_array} + end + end") + out_file.close + end + + def create_subsection(subsection) + pages_array = subsection.pages.map { |page| "Form::Lettings::Pages::#{page.id.camelize}.new(nil, nil, self)" } + out_file = File.new("app/models/form/lettings/subsections/#{subsection.id}.rb", "w") + out_file.puts("class Form::Lettings::Subsections::#{subsection.id.camelize} < ::Form::Subsection + def initialize(id, hsh, section) + super + @id = \"#{subsection.id}\" + @label = \"#{subsection.label}\" + @section = section + end + + def pages + @pages ||= #{pages_array}.compact + end") + out_file.close + end + + def create_page(page) + questions_array = page.questions.map { |question| "Form::Lettings::Questions::#{question.id.camelize}.new(nil, nil, self)" } + out_file = File.new("app/models/form/lettings/pages/#{page.id}.rb", "w") + out_file.puts("class Form::Lettings::Pages::#{page.id.camelize} < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = \"#{page.id}\" + @header = \"#{page.header}\" + @depends_on = #{page.depends_on} + end + + def questions + @questions ||= #{questions_array} + end + end") + out_file.close + end + + def create_question(question) + out_file = File.new("app/models/form/lettings/questions/#{question.id}.rb", "w") + out_file.puts("class Form::Sales::Questions::#{question.id.camelize} < ::Form::Question + def initialize(id, hsh, page) + super + @id = \"#{question.id}\" + @check_answer_label = \"#{question.check_answer_label}\" + @header = \"#{question.header}\" + @type = \"#{question.type}\" + @width = #{question.width} + @inferred_check_answers_value = #{question.inferred_check_answers_value} + @check_answers_card_number = #{question.check_answer_label} + @max = #{question.max} + @min = #{question.min} + end + end") + out_file.close + end + end +end diff --git a/lib/tasks/generate_form.rake b/lib/tasks/generate_form.rake new file mode 100644 index 000000000..22f6db3e0 --- /dev/null +++ b/lib/tasks/generate_form.rake @@ -0,0 +1,9 @@ +namespace :form do + desc "Generate form files from JSON" + task generate_form: :environment do |_task, _args| + service = Spike::FormGeneratorService.new + service.call + + pp "Ran" + end +end