From d918ccca6ee70b51325b8dbc8012ae053305658d Mon Sep 17 00:00:00 2001 From: James Mead Date: Mon, 8 Jan 2024 23:16:08 +0000 Subject: [PATCH] Don't allow Album#released_on to be in the future --- app/models/album.rb | 1 + test/models/album_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/app/models/album.rb b/app/models/album.rb index 54c1021b..5710034d 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -8,6 +8,7 @@ class Album < ApplicationRecord validates :title, presence: true validates :price, presence: true, numericality: true + validates :released_on, comparison: { less_than_or_equal_to: Time.zone.today, allow_blank: true } belongs_to :artist has_many :tracks, -> { order(position: :asc) }, dependent: :destroy, inverse_of: :album diff --git a/test/models/album_test.rb b/test/models/album_test.rb index 4aee40ae..93fae550 100644 --- a/test/models/album_test.rb +++ b/test/models/album_test.rb @@ -213,4 +213,12 @@ class AlbumTest < ActiveSupport::TestCase assert_not album.valid? assert_includes album.errors[:cover], 'must be an image file (jpeg, png)' end + + test 'is not valid if released_on is a date in the future' do + freeze_time do + album = build(:album, released_on: 1.day.from_now) + assert_not album.valid? + assert_includes album.errors[:released_on], "must be less than or equal to #{Time.zone.today}" + end + end end