From 9d08ef6f18ca56e571dc36d1e68055d12ad622e5 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 1 Dec 2024 18:17:07 +0100 Subject: [PATCH] Update wedding slug rules to accept numbers and other chars --- app/models/wedding.rb | 4 +++- config/routes.rb | 2 +- spec/models/wedding_spec.rb | 17 ++++++++++++++++- spec/requests/schemas.rb | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/models/wedding.rb b/app/models/wedding.rb index abc0999..cee2fb7 100644 --- a/app/models/wedding.rb +++ b/app/models/wedding.rb @@ -15,6 +15,8 @@ # index_weddings_on_slug (slug) UNIQUE # class Wedding < ApplicationRecord + SLUG_REGEX = /[a-z\d-]+/ + validates :date, presence: true - validates :slug, presence: true, uniqueness: true, format: { with: /\A[a-z]+\z/ } + validates :slug, presence: true, uniqueness: true, format: { with: /\A#{SLUG_REGEX}\z/ } end diff --git a/config/routes.rb b/config/routes.rb index 9659d05..10810fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ Rails.application.routes.draw do mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development? - scope ":slug", constraints: { slug: /[a-z]+/ } do + scope ":slug", constraints: { slug: Wedding::SLUG_REGEX } do devise_for :users, skip: [:registration, :session, :confirmation] devise_scope :user do post 'users', to: 'users/registrations#create' diff --git a/spec/models/wedding_spec.rb b/spec/models/wedding_spec.rb index 702769f..6ec8a3e 100644 --- a/spec/models/wedding_spec.rb +++ b/spec/models/wedding_spec.rb @@ -3,5 +3,20 @@ require 'rails_helper' RSpec.describe Wedding, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe 'validations' do + subject { build(:wedding) } + describe 'slug' do + it { should allow_value('foo').for(:slug) } + it { should allow_value('foo-bar').for(:slug) } + it { should allow_value('foo-123').for(:slug) } + it { should allow_value('foo-123-').for(:slug) } + it { should allow_value('foo--123').for(:slug) } + + it { should_not allow_value('Foo').for(:slug) } + it { should_not allow_value('/foo').for(:slug) } + it { should_not allow_value('foo/123').for(:slug) } + it { should_not allow_value('foo_123').for(:slug) } + it { should_not allow_value('foo/').for(:slug) } + end + end end diff --git a/spec/requests/schemas.rb b/spec/requests/schemas.rb index ce35ad7..6602db8 100644 --- a/spec/requests/schemas.rb +++ b/spec/requests/schemas.rb @@ -14,6 +14,7 @@ module Swagger name: 'slug', in: :path, type: :string, + pattern: Wedding::SLUG_REGEX, example: :default, description: 'Wedding slug' }