Strip surrounding whitespace from HTMLVariant name (#15776)

This commit is contained in:
Jamie Gaskins 2021-12-14 14:42:06 -05:00 committed by GitHub
parent 9970a57841
commit 9e0570968e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View file

@ -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

View file

@ -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