diff --git a/app/models/album.rb b/app/models/album.rb index 7fefcfca..58238ca5 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -33,6 +33,13 @@ class Album < ApplicationRecord scope :in_release_order, -> { order(Arel.sql('COALESCE(released_on, first_published_on) DESC NULLS LAST')) } scope :best_selling, -> { left_joins(:purchases).group(:id).order('COUNT(purchases.id) DESC') } + scope :recently_released, -> { + joins(:artist) + .where.not(released_on: nil) + .select('DISTINCT ON (artists.id) albums.*') + .order('artists.id, albums.released_on DESC') + } + after_commit :transcode_tracks, on: :update, if: :metadata_or_cover_changed? def preview diff --git a/app/views/interests/new.html.erb b/app/views/interests/new.html.erb index 5bb22c48..1fbc652d 100644 --- a/app/views/interests/new.html.erb +++ b/app/views/interests/new.html.erb @@ -10,21 +10,36 @@

We're a co-operatively owned music store, 100% run by and for the benefit of musicians, fans and the people who work here. <%= text_link_to "Find out more", about_path %>.

-<%= render( - SectionComponent.new( - title: 'Recently purchased', - link_text: "View All", - link_path: artists_path) +
+ <%= render( + SectionComponent.new(title: 'Recently released') ) do %> - <%= render(CardGridComponent.new) do %> - <% Album.best_selling.limit(8).each do |album| %> - <%= link_to(artist_album_path(album.artist, album)) do %> - <%= render(CardComponent.new( + <%= render(CardGridComponent.new) do %> + <% Album.recently_released.limit(4).each do |album| %> + <%= link_to(artist_album_path(album.artist, album)) do %> + <%= render(CardComponent.new( title: album.title, subtitle: album.artist.name, image: ( cdn_url(album.cover.representation(resize_to_limit: [1000, 1000])) if album.cover.representable? ) )) %> + <% end %> <% end %> <% end %> <% end %> -<% end %> + + <%= render( + SectionComponent.new(title: 'Best selling') +) do %> + <%= render(CardGridComponent.new) do %> + <% Album.best_selling.limit(4).each do |album| %> + <%= link_to(artist_album_path(album.artist, album)) do %> + <%= render(CardComponent.new( + title: album.title, + subtitle: album.artist.name, + image: ( cdn_url(album.cover.representation(resize_to_limit: [1000, 1000])) if album.cover.representable? ) + )) %> + <% end %> + <% end %> + <% end %> + <% end %> +
diff --git a/test/models/album_test.rb b/test/models/album_test.rb index 7d2b8ee1..14544a92 100644 --- a/test/models/album_test.rb +++ b/test/models/album_test.rb @@ -207,6 +207,29 @@ class AlbumTest < ActiveSupport::TestCase end end + test '.recently_released returns most recent released album per artist' do + artist1 = create(:artist) + artist2 = create(:artist) + + older_album_artist1 = create(:album, artist: artist1, released_on: 2.months.ago) + newer_album_artist1 = create(:album, artist: artist1, released_on: 1.month.ago) + unreleased_album_artist1 = create(:album, artist: artist1, released_on: nil) + + older_album_artist2 = create(:album, artist: artist2, released_on: 2.months.ago) + newer_album_artist2 = create(:album, artist: artist2, released_on: 1.month.ago) + unreleased_album_artist2 = create(:album, artist: artist2, released_on: nil) + + recently_released = Album.recently_released.map(&:id) + + assert_equal 2, recently_released.size + assert_includes recently_released, newer_album_artist1.id + assert_includes recently_released, newer_album_artist2.id + assert_not_includes recently_released, older_album_artist1.id + assert_not_includes recently_released, older_album_artist2.id + assert_not_includes recently_released, unreleased_album_artist1.id + assert_not_includes recently_released, unreleased_album_artist2.id + end + test 'is invalid with a non-numeric price' do album = build(:album, price: 'foo') assert_not album.valid?