From af83bab2d0d5eb66418f8dd158bed95faabaeb23 Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 29 Apr 2019 20:27:26 +0200 Subject: [PATCH] Escape range separator (#2593) The char "-" in regexp defaults a a separator for a range of character, for example: "a-z", Ruby is smart enough to understand "-_" is no range but it does emit a warning, by properly escaping the char "-" we ensure that no warning or issues can arise in the future. --- app/models/article.rb | 2 +- spec/models/article_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index 29dff2879..bf5355371 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -24,7 +24,7 @@ class Article < ApplicationRecord has_many :rating_votes has_many :page_views - validates :slug, presence: { if: :published? }, format: /\A[0-9a-z-_]*\z/, + validates :slug, presence: { if: :published? }, format: /\A[0-9a-z\-_]*\z/, uniqueness: { scope: :user_id } validates :title, presence: true, length: { maximum: 128 } diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 1e03a0769..2e13b9dca 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -111,7 +111,7 @@ RSpec.describe Article, type: :model do let(:article1) { build(:article, title: title, published: false) } before do - article0.validate + article0.validate! end context "when unpublished" do