From 3d12f05904ebea9d049a8e8940f9f2d7c9764538 Mon Sep 17 00:00:00 2001 From: Rob Nichols Date: Thu, 15 Feb 2024 16:56:00 +0000 Subject: [PATCH] Add identifier and related resource forms --- app/forms/identifier_form.rb | 11 ++++++++ app/forms/related_resource_form.rb | 5 ++++ app/views/items/forms/_identifier.html.erb | 1 + .../items/forms/_related_resource.html.erb | 5 ++++ app/workflows/item_workflow.rb | 2 ++ spec/forms/identifier_form_spec.rb | 25 +++++++++++++++++++ spec/forms/related_resource_form_spec.rb | 25 +++++++++++++++++++ .../item_workflow_intergration_spec.rb | 2 ++ 8 files changed, 76 insertions(+) create mode 100644 app/forms/identifier_form.rb create mode 100644 app/forms/related_resource_form.rb create mode 100644 app/views/items/forms/_identifier.html.erb create mode 100644 app/views/items/forms/_related_resource.html.erb create mode 100644 spec/forms/identifier_form_spec.rb create mode 100644 spec/forms/related_resource_form_spec.rb diff --git a/app/forms/identifier_form.rb b/app/forms/identifier_form.rb new file mode 100644 index 0000000..68de182 --- /dev/null +++ b/app/forms/identifier_form.rb @@ -0,0 +1,11 @@ +class IdentifierForm < BaseForm + validates :identifier, presence: true + + def update_item + item.metadata["identifier"] = identifier + end + + def identifier + @identifier ||= from_params(:identifier) + end +end diff --git a/app/forms/related_resource_form.rb b/app/forms/related_resource_form.rb new file mode 100644 index 0000000..b3e82a5 --- /dev/null +++ b/app/forms/related_resource_form.rb @@ -0,0 +1,5 @@ +class RelatedResourceForm < BaseForm + def update_item + item.metadata["relatedResource"] = from_params(:related_resource) + end +end diff --git a/app/views/items/forms/_identifier.html.erb b/app/views/items/forms/_identifier.html.erb new file mode 100644 index 0000000..530c9c6 --- /dev/null +++ b/app/views/items/forms/_identifier.html.erb @@ -0,0 +1 @@ +<%= form.govuk_text_field :identifier, value: form.object.metadata['identifier'] %> diff --git a/app/views/items/forms/_related_resource.html.erb b/app/views/items/forms/_related_resource.html.erb new file mode 100644 index 0000000..1249981 --- /dev/null +++ b/app/views/items/forms/_related_resource.html.erb @@ -0,0 +1,5 @@ +<%= form.govuk_text_field( + :related_resource, + value: form.object.metadata['related_resource'], + label: { text: 'Related resource' } + ) %> diff --git a/app/workflows/item_workflow.rb b/app/workflows/item_workflow.rb index 79c925b..5e2ade8 100644 --- a/app/workflows/item_workflow.rb +++ b/app/workflows/item_workflow.rb @@ -10,6 +10,8 @@ class ItemWorkflow creator: CreatorForm, publisher: PublisherForm, security_classification: SecurityClassificationForm, + identifier: IdentifierForm, + related_resource: RelatedResourceForm, }.freeze def self.form_for(**options) diff --git a/spec/forms/identifier_form_spec.rb b/spec/forms/identifier_form_spec.rb new file mode 100644 index 0000000..f3f057d --- /dev/null +++ b/spec/forms/identifier_form_spec.rb @@ -0,0 +1,25 @@ +require "rails_helper" + +RSpec.describe IdentifierForm, type: :form do + let(:item) { build :item, metadata: {} } + let(:identifier) { Faker::Lorem.word } + let(:params) do + { item: { identifier: } } + end + let(:identifier_form) { described_class.new(item:, params:) } + + describe "#save" do + it "saves the identifier to metadata" do + identifier_form.save + expect(item.reload.metadata["identifier"]).to eq(identifier) + end + + context "when title blank" do + let(:identifier) { "" } + + it "invalidates form" do + expect(identifier_form).to be_invalid + end + end + end +end diff --git a/spec/forms/related_resource_form_spec.rb b/spec/forms/related_resource_form_spec.rb new file mode 100644 index 0000000..414ba08 --- /dev/null +++ b/spec/forms/related_resource_form_spec.rb @@ -0,0 +1,25 @@ +require "rails_helper" + +RSpec.describe RelatedResourceForm, type: :form do + let(:item) { build :item, metadata: {} } + let(:related_resource) { Faker::Lorem.word } + let(:params) do + { item: { related_resource: } } + end + let(:related_resource_form) { described_class.new(item:, params:) } + + describe "#save" do + it "saves the related resource to metadata" do + related_resource_form.save + expect(item.reload.metadata["relatedResource"]).to eq(related_resource) + end + + context "when title blank" do + let(:relatedResource) { "" } + + it "does not invalidates form" do + expect(related_resource_form).to be_valid + end + end + end +end diff --git a/spec/requests/item_workflow_intergration_spec.rb b/spec/requests/item_workflow_intergration_spec.rb index 44b0e7c..4e61d96 100644 --- a/spec/requests/item_workflow_intergration_spec.rb +++ b/spec/requests/item_workflow_intergration_spec.rb @@ -16,6 +16,8 @@ creator: Organisation.all.sample.id, publisher: Organisation.all.sample.id, security_classification: SecurityClassification.values.sample, + identifier: Faker::Lorem.word, + related_resource: Faker::Lorem.word, } end