Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atom feeds for ArtistsController#index & ArtistsController#show #136

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem 'rails_autolink', '~> 1.1'
gem 'redcarpet', '~> 3.6'
gem 'redis', '~> 5.0'
gem 'rollbar'
gem 'rss'
gem 'rubyzip', '~> 2.3'
gem 'sidekiq'
gem 'sprockets-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ GEM
io-console (~> 0.5)
rexml (3.2.6)
rollbar (3.4.2)
rss (0.3.0)
rexml
rubocop (1.59.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down Expand Up @@ -391,6 +393,7 @@ DEPENDENCIES
redcarpet (~> 3.6)
redis (~> 5.0)
rollbar
rss
rubocop-capybara
rubocop-factory_bot
rubocop-rails
Expand Down
17 changes: 17 additions & 0 deletions app/views/artists/index.atom.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

artists = @artists.sort_by(&:first_listed_on).reverse

atom_feed(root_url: artists_url, language: 'en-GB', schema_date: 2024) do |f|
f.title 'Artists on jam.coop'
f.updated artists.first.updated_at
artists.each do |artist|
id = "tag:#{request.host},2024:#{artist_path(artist)}"
published = artist.first_listed_on
f.entry(artist, id:, published:) do |e|
e.title artist.name
e.author { |a| a.name 'jam.coop' }
e.content artist.description, type: 'html'
end
end
end
4 changes: 4 additions & 0 deletions app/views/artists/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
)
%>

<% content_for(:auto_discovery) do %>
<%= auto_discovery_link_tag(:atom, { format: :atom }, { title: 'Artists on jam.coop' }) %>
<% end %>

<%= render('shared/page_header', text: 'Artists') %>

<div class="pt-4 grid grid-cols-3 gap-4">
Expand Down
18 changes: 18 additions & 0 deletions app/views/artists/show.atom.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

albums = @albums.in_release_order

atom_feed(language: 'en-GB', schema_date: 2024) do |f|
f.title "#{@artist.name} albums on jam.coop"
f.updated albums.first.released_on
albums.each do |album|
id = "tag:#{request.host},2024:#{artist_album_path(@artist, album)}"
url = artist_album_url(@artist, album)
published = album.released_on
f.entry(album, id:, url:, published:) do |e|
e.title album.title
e.author { |a| a.name @artist.name }
e.content format_metadata(album.about), type: 'html'
end
end
end
4 changes: 4 additions & 0 deletions app/views/artists/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
)
%>

<% content_for(:auto_discovery) do %>
<%= auto_discovery_link_tag(:atom, { format: :atom }, { title: "#{@artist.name} albums on jam.coop" }) %>
<% end %>

<div class="flex flex-row border-b-2 border-b-slate-100 pb-6">
<div class="basis-1/4 mr-4 m:mr-0">
<div class="aspect-w-1 aspect-h-1">
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<%= hotwire_livereload_tags if Rails.env.development? %>
<%= content_for(:auto_discovery) %>
</head>

<body class="bg-slate-100">
Expand Down
48 changes: 48 additions & 0 deletions test/controllers/artists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ class ArtistsControllerTestSignedOut < ActionDispatch::IntegrationTest
assert_select 'p', { text: @artist.name }
end

test '#index includes auto-discovery link for atom feed' do
get artists_url

url = artists_url(format: :atom)
assert_select "head link[rel='alternate'][type='application/atom+xml'][href='#{url}']"
end

test '#index with atom format should render atom feed' do
@artist.update!(name: 'Older Artist')
@artist.albums << create(:album, publication_status: :published)

another_artist = create(:artist, name: 'Newer Artist')
another_artist.albums << create(:album, publication_status: :published)

create(:artist, name: 'Unlisted Artist')

get artists_url(format: :atom)

feed = RSS::Parser.parse(response.body)
assert_equal 'Artists on jam.coop', feed.title.content
assert_equal 'Newer Artist', feed.entries.first.title.content
assert_equal 'Older Artist', feed.entries.last.title.content
assert_not_includes feed.entries.map(&:title).map(&:content), 'Unlisted Artist'
end

test '#show should include published albums' do
@artist.albums << create(:album, title: 'Album Title', publication_status: :published)

Expand All @@ -166,6 +191,29 @@ class ArtistsControllerTestSignedOut < ActionDispatch::IntegrationTest
assert_select 'p', { text: 'Album Title (pending)', count: 0 }
end

test '#show includes auto-discovery link for atom feed' do
get artist_url(@artist)

url = artist_url(@artist, format: :atom)
assert_select "head link[rel='alternate'][type='application/atom+xml'][href='#{url}']"
end

test '#show with atom format should render atom feed' do
@artist.albums << create(:album, title: 'Older', publication_status: :published, released_on: 2.days.ago)
@artist.albums << create(:album, title: 'Newer', publication_status: :published, released_on: 1.day.ago)
@artist.albums << create(:album, title: 'Pending', publication_status: :pending, released_on: 0.days.ago)
@artist.albums << create(:album, title: 'Unpublished', publication_status: :unpublished, released_on: 0.days.ago)

get artist_url(@artist, format: :atom)

feed = RSS::Parser.parse(response.body)
assert_equal "#{@artist.name} albums on jam.coop", feed.title.content
assert_equal 'Newer', feed.entries.first.title.content
assert_equal 'Older', feed.entries.last.title.content
assert_not_includes feed.entries.map(&:title).map(&:content), 'Pending'
assert_not_includes feed.entries.map(&:title).map(&:content), 'Unpublished'
end

test '#new' do
get new_artist_url
assert_redirected_to log_in_path
Expand Down