Skip to content

Commit

Permalink
Add Artist#first_listed_on
Browse files Browse the repository at this point in the history
Since an artist is considered to be listed when at least one of their
albums is published, I think it makes sense to say
`Artist#first_listed_on` should be the oldest of
`Album#first_published_on` on any of its albums. And that should be
correct even if an album has subsequently been unpublished.

I'm planning to use `Artist#first_listed_on` for the entry.published
property on an Atom feed for `ArtistsController#index`, i.e. for each
artist.
  • Loading branch information
floehopper committed Jan 9, 2024
1 parent 0cbdcc2 commit 2aa44ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def listed?
albums.where(publication_status: :published).any?
end

def first_listed_on
albums.minimum(:first_published_on)
end

def transcode_albums
albums.each(&:transcode_tracks)
end
Expand Down
9 changes: 9 additions & 0 deletions test/models/artist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class ArtistTest < ActiveSupport::TestCase
assert artist.listed?
end

test '#first_listed_on returns oldest Album#first_published_on' do
newer_album = build(:album, publication_status: :published, first_published_on: Date.parse('2023-01-02'))
older_album = build(:album, publication_status: :unpublished, first_published_on: Date.parse('2023-01-01'))
album_without_date = build(:album, publication_status: :pending, first_published_on: nil)
artist = create(:artist, albums: [newer_album, older_album, album_without_date])

assert_equal older_album.first_published_on, artist.first_listed_on
end

test 'uses a friendly id' do
artist = create(:artist, name: 'Rick Astley')

Expand Down

0 comments on commit 2aa44ef

Please sign in to comment.