From 913224ad84c7be64341056da3b5ad0a34213fb53 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 22 Sep 2022 14:08:48 -0700 Subject: [PATCH] chore: Normalize portal slug to nil (#5487) --- app/models/application_record.rb | 8 ++++++++ app/models/portal.rb | 1 + spec/models/portal_spec.rb | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 37b839992..ee91205eb 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -11,4 +11,12 @@ class ApplicationRecord < ActiveRecord::Base "#{self.class.name}Drop".constantize.new(self) end + + private + + def normalize_empty_string_to_nil(attrs = []) + attrs.each do |attr| + self[attr] = nil if self[attr].blank? + end + end end diff --git a/app/models/portal.rb b/app/models/portal.rb index a52a69253..9d36c8217 100644 --- a/app/models/portal.rb +++ b/app/models/portal.rb @@ -38,6 +38,7 @@ class Portal < ApplicationRecord source: :user has_one_attached :logo + before_validation -> { normalize_empty_string_to_nil(%i[custom_domain homepage_link]) } validates :account_id, presence: true validates :name, presence: true validates :slug, presence: true, uniqueness: true diff --git a/spec/models/portal_spec.rb b/spec/models/portal_spec.rb index 3e574ff3f..925ec3938 100644 --- a/spec/models/portal_spec.rb +++ b/spec/models/portal_spec.rb @@ -32,6 +32,11 @@ RSpec.describe Portal, type: :model do expect(portal).not_to be_valid expect(portal.errors.full_messages[0]).to eq('Cofig in portal on some_other_key is not supported.') end + + it 'converts empty string to nil' do + portal.update(custom_domain: '') + expect(portal.custom_domain).to be_nil + end end end end