diff --git a/app/models/form.rb b/app/models/form.rb index da7637e1c..98bec0225 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -14,6 +14,7 @@ class Form < ApplicationRecord has_one :live_welsh_form_document, -> { where tag: "live", language: :cy }, class_name: "FormDocument" has_one :archived_form_document, -> { where tag: "archived", language: :en }, class_name: "FormDocument" has_one :archived_welsh_form_document, -> { where tag: "archived", language: :cy }, class_name: "FormDocument" + has_one :draft_welsh_form_document, -> { where tag: "draft", language: :cy }, class_name: "FormDocument" has_one :draft_form_document, -> { where tag: "draft", language: :en }, class_name: "FormDocument" has_many :conditions, through: :pages, source: :routing_conditions diff --git a/app/presenters/form_list_presenter.rb b/app/presenters/form_list_presenter.rb index 94af01761..03222395c 100644 --- a/app/presenters/form_list_presenter.rb +++ b/app/presenters/form_list_presenter.rb @@ -50,7 +50,7 @@ def head def rows rows = forms.sort_by { |form| [form.name.downcase, form.created_at] }.map do |form| - row = [{ text: form_name_link(form) }, + row = [{ text: form_name_link(form) + welsh_status(form) }, { text: find_creator_name(form) }, { text: form_status_tags(form), numeric: true }] @@ -74,6 +74,16 @@ def change_group_link(form) ) end + def welsh_status(form) + if form.live_welsh_form_document.present? + "
With Welsh Version
".html_safe + elsif form.draft_welsh_form_document.present? + "With Welsh draft
".html_safe + elsif form.archived_welsh_form_document.present? + "With archived Welsh version
".html_safe + end + end + def form_status_tags(form) # Create an instance of controller. We are using ApplicationController here. view_context = ApplicationController.new.view_context diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 5e8b6728b..843b3b43a 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -444,6 +444,36 @@ end end + describe "draft_welsh_form_document" do + context "when there is no draft Welsh form document" do + subject(:form) { create :form } + + it "returns nil" do + expect(form.draft_welsh_form_document).to be_nil + end + end + + context "when there is a draft Welsh form document" do + subject(:form) { create :form, :draft, :with_welsh_translation } + + it "returns nil" do + expect(form.draft_welsh_form_document).to be_a(FormDocument) + end + end + + context "when there is only a live Welsh form document" do + subject(:form) { create :form, :live, :with_welsh_translation } + + before do + FormDocument.find_by(form:, tag: "draft").destroy + end + + it "returns nil" do + expect(form.draft_welsh_form_document).to be_nil + end + end + end + describe "draft_form_document" do context "when there is no archived form document" do it "returns nil" do diff --git a/spec/presenters/form_list_presenter_spec.rb b/spec/presenters/form_list_presenter_spec.rb index 7f388c120..084b17266 100644 --- a/spec/presenters/form_list_presenter_spec.rb +++ b/spec/presenters/form_list_presenter_spec.rb @@ -114,6 +114,32 @@ expect(rows[3][0][:text]).to eq("b") end end + + context "when a welsh version of the form exists" do + let(:forms) do + [ + create(:form, :live, :with_welsh_translation, id: 1, name: "form with a live Welsh Version"), + create(:form, :archived, :with_welsh_translation, id: 2, name: "form with an archived and a draft Welsh version"), + create(:form, :draft, :with_welsh_translation, id: 3, name: "form with Welsh draft"), + create(:form, id: 4, name: "form with no Welsh version"), + create(:form, :archived, :with_welsh_translation, id: 5, name: "form with only an archived Welsh version"), + ] + end + + before do + # remove the draft version of the welsh translation for the form with only archived version. + FormDocument.find_by(form: forms[4], tag: "draft", language: "cy").destroy! + end + + it "appends the correct text" do + rows = presenter.data[:rows] + expect(rows[0][0][:text]).to eq("form with a live Welsh VersionWith Welsh Version
") + expect(rows[1][0][:text]).to eq("form with an archived and a draft Welsh versionWith Welsh draft
") + expect(rows[2][0][:text]).to eq("form with no Welsh version") + expect(rows[3][0][:text]).to eq("form with only an archived Welsh versionWith archived Welsh version
") + expect(rows[4][0][:text]).to eq("form with Welsh draftWith Welsh draft
") + end + end end end end