From fa07a97815143f4bd9c9c016ce21c8d18592d654 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 29 Jul 2024 15:40:20 -0400 Subject: [PATCH 1/3] Convert `EmailHelper` to `CanonicalEmail` model concern --- app/helpers/email_helper.rb | 18 ------------------ app/models/canonical_email_block.rb | 4 ++-- app/models/concerns/canonical_email.rb | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 app/helpers/email_helper.rb create mode 100644 app/models/concerns/canonical_email.rb diff --git a/app/helpers/email_helper.rb b/app/helpers/email_helper.rb deleted file mode 100644 index 0800601f98b..00000000000 --- a/app/helpers/email_helper.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module EmailHelper - def self.included(base) - base.extend(self) - end - - def email_to_canonical_email(str) - username, domain = str.downcase.split('@', 2) - username, = username.delete('.').split('+', 2) - - "#{username}@#{domain}" - end - - def email_to_canonical_email_hash(str) - Digest::SHA2.new(256).hexdigest(email_to_canonical_email(str)) - end -end diff --git a/app/models/canonical_email_block.rb b/app/models/canonical_email_block.rb index d09df6f5e2a..19e9d6bbf76 100644 --- a/app/models/canonical_email_block.rb +++ b/app/models/canonical_email_block.rb @@ -12,7 +12,7 @@ # class CanonicalEmailBlock < ApplicationRecord - include EmailHelper + include CanonicalEmail include Paginable belongs_to :reference_account, class_name: 'Account', optional: true @@ -26,7 +26,7 @@ class CanonicalEmailBlock < ApplicationRecord end def email=(email) - self.canonical_email_hash = email_to_canonical_email_hash(email) + self.canonical_email_hash = self.class.email_to_canonical_email_hash(email) end def self.block?(email) diff --git a/app/models/concerns/canonical_email.rb b/app/models/concerns/canonical_email.rb new file mode 100644 index 00000000000..69d00a804cf --- /dev/null +++ b/app/models/concerns/canonical_email.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module CanonicalEmail + extend ActiveSupport::Concern + + class_methods do + def email_to_canonical_email(str) + username, domain = str.downcase.split('@', 2) + username, = username.delete('.').split('+', 2) + + "#{username}@#{domain}" + end + + def email_to_canonical_email_hash(str) + Digest::SHA2 + .new(256) + .hexdigest( + email_to_canonical_email(str) + ) + end + end +end From 2ac99032ebcb995c483a78be78561b015561eddc Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 29 Jul 2024 16:44:20 -0400 Subject: [PATCH 2/3] Add private method declaration --- app/models/concerns/canonical_email.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/canonical_email.rb b/app/models/concerns/canonical_email.rb index 69d00a804cf..0a1d94ebb1d 100644 --- a/app/models/concerns/canonical_email.rb +++ b/app/models/concerns/canonical_email.rb @@ -4,13 +4,6 @@ module CanonicalEmail extend ActiveSupport::Concern class_methods do - def email_to_canonical_email(str) - username, domain = str.downcase.split('@', 2) - username, = username.delete('.').split('+', 2) - - "#{username}@#{domain}" - end - def email_to_canonical_email_hash(str) Digest::SHA2 .new(256) @@ -18,5 +11,14 @@ module CanonicalEmail email_to_canonical_email(str) ) end + + private + + def email_to_canonical_email(str) + username, domain = str.downcase.split('@', 2) + username, = username.delete('.').split('+', 2) + + "#{username}@#{domain}" + end end end From ef87fb8a5dbc3d72fc10f7faaceefe1bdfe87e52 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 29 Jul 2024 16:59:17 -0400 Subject: [PATCH 3/3] Improve var names --- app/models/concerns/canonical_email.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/concerns/canonical_email.rb b/app/models/concerns/canonical_email.rb index 0a1d94ebb1d..7dae59e52f6 100644 --- a/app/models/concerns/canonical_email.rb +++ b/app/models/concerns/canonical_email.rb @@ -4,18 +4,18 @@ module CanonicalEmail extend ActiveSupport::Concern class_methods do - def email_to_canonical_email_hash(str) + def email_to_canonical_email_hash(value) Digest::SHA2 .new(256) .hexdigest( - email_to_canonical_email(str) + email_to_canonical_email(value) ) end private - def email_to_canonical_email(str) - username, domain = str.downcase.split('@', 2) + def email_to_canonical_email(value) + username, domain = value.downcase.split('@', 2) username, = username.delete('.').split('+', 2) "#{username}@#{domain}"