Skip to content

Commit

Permalink
Don't allow Album#released_on to be in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Jan 9, 2024
1 parent 4e31a4a commit d918ccc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d918ccc

Please sign in to comment.