-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from co-cddo/add-contact-point
Add contact point
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
)%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters