From 1c31719ea16521904fdd3c69727fa5a96b5f3a7a Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Thu, 22 Aug 2024 15:43:03 +0100 Subject: [PATCH] feat: unify email validation and rubocop --- app/models/customer.rb | 1 + app/services/customers/create_service.rb | 7 ------- lib/tasks/auto_annotate_models.rake | 4 +++- spec/services/customers/create_service_spec.rb | 2 +- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index bc0b522b8b8..62a00de716e 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -60,6 +60,7 @@ class Customer < ApplicationRecord validates :net_payment_term, numericality: {greater_than_or_equal_to: 0}, allow_nil: true validates :payment_provider, inclusion: {in: PAYMENT_PROVIDERS}, allow_nil: true validates :timezone, timezone: true, allow_nil: true + validates :email, email: true, if: :email? def self.ransackable_attributes(_auth_object = nil) %w[id name external_id email] diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index b66a209b95a..b6dad22ed1b 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -9,13 +9,6 @@ def create_from_api(organization:, params:) new_customer = customer.new_record? shipping_address = params[:shipping_address] ||= {} - unless valid_email?(params[:email]) - return result.single_validation_failure!( - field: :email, - error_code: 'invalid_format' - ) - end - unless valid_metadata_count?(metadata: params[:metadata]) return result.single_validation_failure!( field: :metadata, diff --git a/lib/tasks/auto_annotate_models.rake b/lib/tasks/auto_annotate_models.rake index 56890dadbf6..650b6663e02 100644 --- a/lib/tasks/auto_annotate_models.rake +++ b/lib/tasks/auto_annotate_models.rake @@ -1,9 +1,11 @@ +# frozen_string_literal: true + # NOTE: only doing this in development as some production environments (Heroku) # NOTE: are sensitive to local FS writes, and besides -- it's just not proper # NOTE: to have a dev-mode tool do its thing in production. if Rails.env.development? require 'annotate' - task :set_annotation_options do + task set_annotation_options: :environment do # You can override any of these by setting an environment variable of the # same name. Annotate.set_defaults( diff --git a/spec/services/customers/create_service_spec.rb b/spec/services/customers/create_service_spec.rb index 3b999fc4353..a5128106445 100644 --- a/spec/services/customers/create_service_spec.rb +++ b/spec/services/customers/create_service_spec.rb @@ -136,7 +136,7 @@ expect(result).not_to be_success expect(result.error).to be_a(BaseService::ValidationFailure) expect(result.error.messages.keys).to include(:email) - expect(result.error.messages[:email]).to include('invalid_format') + expect(result.error.messages[:email]).to include('invalid_email_format') end end end