Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions app/models/form.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
class Form < ActiveResource::Base
self.site = Settings.forms_api.base_url
self.prefix = "/api/v2/"
self.include_format_in_path = false
class Form
attr_reader :document_json
private attr_reader :form_document

class Step < ActiveResource::Base
self.site = Form.site
self.prefix = Form.prefix_source
self.include_format_in_path = false
def initialize(form_document, form_document_json = nil)
@form_document = form_document
@document_json = form_document_json
end

has_many :steps, class_name: "Api::V2::StepResource"
attr_accessor :document_json

alias_method :form_document_steps, :steps

def form_id
@attributes["form_id"] || @attributes["id"]
end
delegate :steps, to: :form_document, prefix: true
delegate :declaration_text,
:form_id,
:form_slug,
:name,
:payment_url,
:privacy_policy_url,
:s3_bucket_aws_account_id,
:s3_bucket_name,
:s3_bucket_region,
:start_page,
:send_daily_submission_batch,
:send_weekly_submission_batch,
:submission_email,
:submission_type,
:support_email,
:support_phone,
:support_url,
:support_url_text,
:what_happens_next_markdown,
to: :form_document

alias_method :id, :form_id

Expand All @@ -25,27 +36,27 @@ def step_by_id(step_id)
end

def payment_url_with_reference(reference)
return nil if payment_url.blank?
return nil if form_document.payment_url.blank?

"#{payment_url}?reference=#{reference}"
"#{form_document.payment_url}?reference=#{reference}"
end

def submission_format
@attributes["submission_format"] || []
form_document.try(:submission_format) || []
end

def support_details
OpenStruct.new({
email: support_email,
phone: support_phone,
email: form_document.support_email,
phone: form_document.support_phone,
call_charges_url: "https://www.gov.uk/call-charges",
url: support_url,
url_text: support_url_text,
url: form_document.support_url,
url_text: form_document.support_url_text,
})
end

def language
@attributes["language"]&.to_sym || :en
form_document.try(:language)&.to_sym || :en
end

def english?
Expand All @@ -57,6 +68,14 @@ def welsh?
end

def multilingual?
@attributes["available_languages"].present? && @attributes["available_languages"].count > 1
available_languages.count > 1
end

def available_languages
form_document.try(:available_languages) || []
end

def declaration_markdown
form_document.try(:declaration_markdown)
end
end
3 changes: 2 additions & 1 deletion app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def answer_store
end

def form_from_document
Form.new(form_document, true)
form_document_resource = Api::V2::FormDocumentResource.new(form_document, true)
Form.new(form_document_resource, form_document)
end
end
9 changes: 3 additions & 6 deletions app/services/api/v2/form_document_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ def find(form_id:, tag:, language: :en)
return nil unless form_id.to_s =~ /^[[:alnum:]]+$/

begin
form_document = Api::V2::FormDocumentResource.get(form_id, tag, **options_for_language(language))

form = Form.new(form_document, true)
form.document_json = form_document
form.prefix_options = { form_id:, tag: }
form
form_document_json = Api::V2::FormDocumentResource.get(form_id, tag, **options_for_language(language))
form_document = Api::V2::FormDocumentResource.new(form_document_json)
Form.new(form_document, form_document_json)
rescue ActiveResource::ResourceNotFound
nil
end
Expand Down
2 changes: 1 addition & 1 deletion spec/components/check_your_answers_component/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe CheckYourAnswersComponent::View, type: :component do
include Rails.application.routes.url_helpers

let(:form) { build :form, id: 1 }
let(:form) { build :form }
let(:question) { build :text, question_text: "Do you want to remain anonymous?", text: "Yes" }
let(:optional_question) { build :text, question_text: "Optional question", is_optional: true, text: "" }
let(:steps) do
Expand Down
40 changes: 8 additions & 32 deletions spec/factories/models/forms.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FactoryBot.define do
factory :form, class: "Form" do
initialize_with { new(build(:v2_form_document, **attributes), document_json) }

form_id { Faker::Number.number(digits: 5) }
sequence(:name) { |n| "Form #{n}" }
sequence(:form_slug) { |n| "form-#{n}" }
has_draft_version { true }
has_live_version { false }
submission_email { Faker::Internet.email(domain: "example.gov.uk") }
privacy_policy_url { Faker::Internet.url(host: "gov.uk") }
org { "test-org" }
what_happens_next_markdown { nil }
support_email { nil }
support_phone { nil }
Expand All @@ -17,40 +17,26 @@
document_json { nil }

declaration_text { nil }
declaration_section_completed { false }

submission_type { "email" }

s3_bucket_name { nil }
s3_bucket_aws_account_id { nil }

trait :new_form do
submission_email { nil }
privacy_policy_url { nil }
end

trait :ready_for_live do
with_pages
trait :live do
with_steps
support_email { Faker::Internet.email(domain: "example.gov.uk") }
what_happens_next_markdown { "We usually respond to applications within 10 working days." }
end

trait :live? do
ready_for_live
has_draft_version { false }
has_live_version { true }
end

trait :with_pages do
trait :with_steps do
transient do
pages_count { 5 }
steps_count { 5 }
end

form_document_steps do
Array.new(pages_count) { association(:form_document_step) }
Array.new(steps_count) { association(:form_document_step) }
end

question_section_completed { true }
end

trait :with_support do
Expand All @@ -60,16 +46,6 @@
support_url_text { Faker::Lorem.sentence(word_count: 1, random_words_to_add: 4) }
end

trait :ready_for_routing do
transient do
pages_count { 5 }
end

form_document_steps do
Array.new(pages_count) { association(:form_document_step, :with_selections_settings) }
end
end

trait :with_payment_url do
payment_url { "https://www.gov.uk/payments/test-service/pay-for-licence" }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/v2_form_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
what_happens_next_markdown { "We usually respond to applications within 10 working days." }
end

trait :live? do
trait :live do
ready_for_live
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/email_confirmation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

feature "Email confirmation", type: :feature do
let(:steps) { [build(:v2_question_step, :with_text_settings, id: 1, routing_conditions: [], question_text:)] }
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Apply for a juggling license", steps:, start_page: 1 }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Apply for a juggling license", steps:, start_page: 1 }
let(:question_text) { Faker::Lorem.question }
let(:text_answer) { Faker::Lorem.sentence }

Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_and_submit_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

feature "Fill in and submit a form", type: :feature do
let(:steps) { [build(:v2_question_step, :with_text_settings, id: 1, routing_conditions: [], question_text:)] }
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:question_text) { Faker::Lorem.question }
let(:answer_text) { "Answer text" }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_and_submit_form_with_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
build(:v2_selection_question_step, only_one_option: false, id: 2, question_text: "Skipped question", next_step_id: 3),
]
end
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: steps.first.id, submission_type: "email", submssion_format: %w[csv] }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Fill in this form", steps:, start_page: steps.first.id, submission_type: "email", submssion_format: %w[csv] }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
let(:req_headers) { { "Accept" => "application/json" } }

Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_autocomplete_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

feature "Fill in and submit a form with an autocomplete question", type: :feature do
let(:steps) { [build(:v2_selection_question_step, id: 1, routing_conditions: [], question_text:, selection_options:)] }
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:selection_options) { Array.new(31).each_with_index.map { |_element, index| { name: "Answer #{index}", value: "Answer #{index}" } } }
let(:question_text) { Faker::Lorem.question }
let(:answer_text) { "Answer 1" }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_file_upload_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include ActiveJob::TestHelper

let(:steps) { [build(:v2_question_step, answer_type: "file", id: 1, routing_conditions: [], question_text:)] }
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:question_text) { Faker::Lorem.question }
let(:answer_text) { "Answer 1" }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_form_with_exit_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
feature "Fill in and submit a form with an exit page", type: :feature do
let(:routing_conditions) { [DataStruct.new(routing_page_id: 1, check_page_id: 1, answer_value: "Option 1", goto_page_id: nil, exit_page_heading: "This is an exit_page", exit_page_markdown: "This is the contents", validation_errors: [])] }
let(:steps) { [build(:v2_selection_question_step, id: 1, routing_conditions:, question_text:)] }
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:form) { build :v2_form_document, :live, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:question_text) { Faker::Lorem.question }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }

Expand Down
2 changes: 1 addition & 1 deletion spec/features/fill_in_single_repeatable_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

feature "Fill in and submit a form with a single repeatable question", type: :feature do
let(:steps) { [build(:v2_question_step, :with_repeatable, answer_type: "number", question_text:)] }
let(:form) { build :v2_form_document, :live?, form_id: 42, name: "Form with repeating question", steps:, start_page: steps.first.id }
let(:form) { build :v2_form_document, :live, form_id: 42, name: "Form with repeating question", steps:, start_page: steps.first.id }

let(:question_text) { Faker::Lorem.question }
let(:first_answer_text) { "99" }
Expand Down
2 changes: 0 additions & 2 deletions spec/fixtures/all_question_types_form.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"support_url": null,
"support_url_text": null,
"declaration_text": "",
"question_section_completed": true,
"declaration_section_completed": true,
"created_at": "2024-09-05T06:25:25.558Z",
"updated_at": "2024-09-05T06:25:25.637Z",
"creator_id": null,
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

context "when an exception is raised containing personally identifying information" do
let(:form) { build :form, id: 1, submission_email: }
let(:form) { build :form, submission_email: }

before do
raise "Something went wrong: #{form.inspect}"
Expand Down
1 change: 0 additions & 1 deletion spec/lib/flow/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

let(:form) do
build(:form, :with_support,
id: 2,
start_page: 1,
privacy_policy_url: "http://www.example.gov.uk/privacy_policy",
what_happens_next_markdown: "Good things come to those that wait",
Expand Down
1 change: 0 additions & 1 deletion spec/lib/flow/journey_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

let(:form) do
build(:form, :with_support,
id: 2,
start_page: first_step_id,
steps: form_document_steps)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/flow/step_factory_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe Flow::StepFactory do
let(:form) { build :form, id: "form-123", form_slug: "test-form", start_page: "page-1", steps: [] }
let(:form) { build :form, form_slug: "test-form", start_page: "page-1", steps: [] }
let(:factory) { described_class.new(form:) }

describe "#create_step" do
Expand Down
Loading
Loading