[Fix #1479] Create a sanitize email concern

This commit is contained in:
Mathieu Magnin 2018-02-28 16:23:50 +01:00
parent 9c8e3531c0
commit b7de632d6c
2 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,10 @@
module EmailSanitizableConcern
extend ActiveSupport::Concern
def sanitize_email(attribute)
value_to_sanitize = self.send(attribute)
if value_to_sanitize.present?
self[attribute] = value_to_sanitize.gsub(/[[:space:]]/, ' ').strip.downcase
end
end
end

View file

@ -0,0 +1,51 @@
describe EmailSanitizableConcern, type: :model do
describe 'sanitize_email' do
let(:email_concern) do
(Class.new do
include EmailSanitizableConcern
attr_accessor :email
def initialize(email)
self.email = email
end
def [](key)
self.send(key)
end
def []=(key, value)
self.send("#{key}=", value)
end
end).new(email)
end
before do
email_concern.sanitize_email(:email)
end
context 'on an empty email' do
let(:email) { '' }
it { expect(email_concern.email).to eq('') }
end
context 'on a valid email' do
let(:email) { 'michel@toto.fr' }
it { expect(email_concern.email).to eq('michel@toto.fr') }
end
context 'on an email with trailing spaces' do
let(:email) { ' michel@toto.fr ' }
it { expect(email_concern.email).to eq('michel@toto.fr') }
end
context 'on an email with trailing nbsp' do
let(:email) { ' michel@toto.fr  ' }
it { expect(email_concern.email).to eq('michel@toto.fr') }
end
context 'on an invalid email' do
let(:email) { 'mich el@toto.fr' }
it { expect(email_concern.email).to eq('mich el@toto.fr') }
end
end
end