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