From 9e0570968e2c2babe6c92af433a46910d3ac8fae Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Tue, 14 Dec 2021 14:42:06 -0500 Subject: [PATCH] Strip surrounding whitespace from HTMLVariant name (#15776) --- app/models/html_variant.rb | 6 ++++++ spec/models/html_variant_spec.rb | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/app/models/html_variant.rb b/app/models/html_variant.rb index 0d383fd1d..35c5a3efb 100644 --- a/app/models/html_variant.rb +++ b/app/models/html_variant.rb @@ -8,6 +8,8 @@ class HtmlVariant < ApplicationRecord has_many :html_variant_successes, dependent: :destroy has_many :html_variant_trials, dependent: :destroy + before_validation :strip_whitespace + validates :group, inclusion: { in: GROUP_NAMES } validates :html, presence: true validates :name, uniqueness: true @@ -77,4 +79,8 @@ class HtmlVariant < ApplicationRecord def allowed_image_host?(src) src.start_with?("https://res.cloudinary.com/") || src.start_with?(Images::Optimizer.get_imgproxy_endpoint) end + + def strip_whitespace + name.strip! + end end diff --git a/spec/models/html_variant_spec.rb b/spec/models/html_variant_spec.rb index fb31dfa1f..3178a395e 100644 --- a/spec/models/html_variant_spec.rb +++ b/spec/models/html_variant_spec.rb @@ -70,4 +70,9 @@ RSpec.describe HtmlVariant, type: :model do html_variant.save expect(Images::Optimizer).not_to have_received(:call) end + + it "strips whitespace from the name" do + variant = create(:html_variant, name: " hello world ") + variant.reload.name.should eq "hello world" + end end