Skip to content

Commit

Permalink
Add recently released albums to homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislo committed Dec 18, 2024
1 parent 86cfcd4 commit 0683ce0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
7 changes: 7 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 25 additions & 10 deletions app/views/interests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,36 @@
<p class="mb-3 text-slate-600">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 %>.</p>
</div>

<%= render(
SectionComponent.new(
title: 'Recently purchased',
link_text: "View All",
link_path: artists_path)
<div class="grid grid-col gap-6">
<%= 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 %>
</div>
23 changes: 23 additions & 0 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 0683ce0

Please sign in to comment.