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

Fix/blank contact email #1246

Merged
merged 5 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def self.custom_error_messages(attribute, *errors)
messages = {}

errors.each do |error|
messages[error] = I18n.t("activemodel.errors.models."\
"waste_carriers_engine/#{form_name}"\
messages[error] = I18n.t("activemodel.errors.models." \
"waste_carriers_engine/#{form_name}" \
".attributes.#{attribute}.#{error}")
end

Expand Down
4 changes: 4 additions & 0 deletions app/forms/waste_carriers_engine/contact_email_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class ContactEmailForm < ::WasteCarriersEngine::BaseForm
after_initialize :populate_confirmed_email

def submit(params)
# Blank email address vluaes should be processed as nil
params[:contact_email] = nil if params[:contact_email].blank?
params[:confirmed_email] = nil if params[:confirmed_email].blank?

# Assign the params for validation and pass them to the BaseForm method for updating
self.confirmed_email = params[:confirmed_email]
self.no_contact_email = params[:no_contact_email]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ContactEmailValidator < ActiveModel::EachValidator

def validate(record)
if WasteCarriersEngine.configuration.host_is_back_office? && record.no_contact_email == "1"
if record.contact_email.present?
unless record.contact_email.nil?
add_validation_error(record, :no_contact_email, :not_blank)
return false
end
Expand Down
22 changes: 17 additions & 5 deletions spec/forms/waste_carriers_engine/contact_email_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ module WasteCarriersEngine
end

context "when the form is valid" do
before { contact_email_form.transient_registration.contact_email = nil }

context "when running in the front office" do
before { allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(false) }

context "with an email address" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }

it_behaves_like "should submit"
end
Expand All @@ -44,16 +46,26 @@ module WasteCarriersEngine
before { allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(true) }

context "with an email address" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }

it_behaves_like "should submit"

it "populates contact_email" do
expect { contact_email_form.submit(ActionController::Parameters.new(params)) }
.to change { contact_email_form.transient_registration.contact_email }.to(contact_email)
end
end

context "without an email address" do
let(:contact_email) { nil }
context "with a blank email address" do
let(:contact_email) { "" }
let(:no_contact_email) { "1" }

it_behaves_like "should submit"

it "does not populate contact_email" do
expect { contact_email_form.submit(ActionController::Parameters.new(params)) }
.not_to change { contact_email_form.transient_registration.contact_email }.from(nil)
end
end
end
end
Expand All @@ -80,7 +92,7 @@ module WasteCarriersEngine
end

context "with an email address and with the no-email-address option selected" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }
let(:no_contact_email) { "1" }

it_behaves_like "should not submit"
Expand Down