Skip to content

Commit

Permalink
Merge pull request #12 from co-cddo/add-identifier-and-related-resour…
Browse files Browse the repository at this point in the history
…ce-steps

Add identifier and related resource forms
  • Loading branch information
RobNicholsGDS authored Feb 15, 2024
2 parents dd950cf + 3d12f05 commit 802a72e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/forms/identifier_form.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/forms/related_resource_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RelatedResourceForm < BaseForm
def update_item
item.metadata["relatedResource"] = from_params(:related_resource)
end
end
1 change: 1 addition & 0 deletions app/views/items/forms/_identifier.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= form.govuk_text_field :identifier, value: form.object.metadata['identifier'] %>
5 changes: 5 additions & 0 deletions app/views/items/forms/_related_resource.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= form.govuk_text_field(
:related_resource,
value: form.object.metadata['related_resource'],
label: { text: 'Related resource' }
) %>
2 changes: 2 additions & 0 deletions app/workflows/item_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ItemWorkflow
creator: CreatorForm,
publisher: PublisherForm,
security_classification: SecurityClassificationForm,
identifier: IdentifierForm,
related_resource: RelatedResourceForm,
}.freeze

def self.form_for(**options)
Expand Down
25 changes: 25 additions & 0 deletions spec/forms/identifier_form_spec.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions spec/forms/related_resource_form_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/requests/item_workflow_intergration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 802a72e

Please sign in to comment.