Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contact point #8

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
23 changes: 23 additions & 0 deletions app/forms/contact_point_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ContactPointForm < BaseForm
validates :contact_name, :email, presence: true
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }

def update_item
item.metadata["contactPoint"] = contact_point
end

def contact_point
{
"contactName" => contact_name,
"email" => email,
}
end

def contact_name
@contact_name ||= params.dig :item, :contact_name
end

def email
@email ||= params.dig :item, :email
end
end
9 changes: 9 additions & 0 deletions app/views/items/forms/_contact_point.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= form.govuk_text_field(
:contact_name,
label: { text: 'Contact name' },
value: form.object.metadata.dig('contactPoint','contactName')
)%>
<%= form.govuk_email_field(
:email,
value: form.object.metadata.dig('contactPoint','email')
)%>
1 change: 1 addition & 0 deletions app/workflows/item_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ItemWorkflow
description: DescriptionForm,
keyword: KeywordForm,
theme: ThemeForm,
contact_point: ContactPointForm,
}.freeze

def self.form_for(**options)
Expand Down
46 changes: 46 additions & 0 deletions spec/forms/contact_point_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
RSpec.describe ContactPointForm, type: :form do
let(:item) { create :item }
let(:contact_name) { Faker::Name.name }
let(:email) { Faker::Internet.email(domain: "example.com") }
let(:params) do
{
item: {
contact_name:,
email:,
},
}
end
let(:contact_point_form) { described_class.new(item:, params:) }

describe "#save" do
it "saves the name and email to a contact point object in metadata" do
contact_point_form.save
expected = { "contactName" => contact_name, "email" => email }
expect(item.reload.metadata["contactPoint"]).to eq(expected)
end

context "if contact name missing" do
let(:contact_name) { "" }

it "invalidates form" do
expect(contact_point_form).to be_invalid
end
end

context "if email missing" do
let(:email) { "" }

it "invalidates form" do
expect(contact_point_form).to be_invalid
end
end

context "if email not valid email" do
let(:email) { "invalid" }

it "invalidates form" do
expect(contact_point_form).to be_invalid
end
end
end
end
12 changes: 10 additions & 2 deletions spec/requests/item_workflow_intergration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
description: Faker::Lorem.paragraph,
keyword: Faker::Internet.slug,
theme: Faker::Lorem.word,
contact_point: {
contact_name: Faker::Name.name,
email: Faker::Internet.email(domain: "example.com"),
},
}
end

it "visits each step in the flow" do
item = nil
steps.each do |step, input|
params = input.is_a?(Hash) ? input : { step => input }

if step == steps.keys.first
post items_url, params: { item: { step => input } }
post items_url, params: { item: params }
item = Item.last
else
patch item_url(item), params: { item: { step => input } }
patch item_url(item), params: { item: params }
end

if step == steps.keys.last
Expand All @@ -28,6 +34,8 @@
expect(response).to redirect_to(edit_item_url(item)), "#{step} didn't redirect to next step"
end

input.transform_keys! { |key| key.to_s.camelize(:lower) } if input.is_a?(Hash)

expect(item.reload.metadata[step.to_s.camelize(:lower)]).to include(input), "#{step} in metadata was not: #{input}"
end
end
Expand Down
Loading