Skip to content

Commit

Permalink
Pending dashboard
Browse files Browse the repository at this point in the history
This commit adds a simple admin-only "pending" dashboard to make it
easier and less error-prone to review albums that have been submitted
for publication.

I've decided to namespace the new controller under `admin` rather than
reuse the existing album controller for this.

Fixes #105
  • Loading branch information
chrislo committed Jan 16, 2024
1 parent a66ef07 commit 629512b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/admin/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Admin
class AlbumsController < ApplicationController
def index
authorize([:admin, Album])
end
end
end
1 change: 1 addition & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Album < ApplicationRecord

scope :published, -> { where(publication_status: :published) }
scope :unpublished, -> { where(publication_status: :unpublished) }
scope :pending, -> { where(publication_status: :pending) }
scope :in_release_order, -> { order(Arel.sql('COALESCE(released_on, first_published_on) DESC NULLS LAST')) }

after_commit :transcode_tracks, on: :update, if: :metadata_or_cover_changed?
Expand Down
9 changes: 9 additions & 0 deletions app/policies/admin/album_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Admin
class AlbumPolicy < ApplicationPolicy
def index?
user.admin?
end
end
end
8 changes: 8 additions & 0 deletions app/views/admin/albums/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= render('shared/page_header', text: 'Albums') %>
<%= render('shared/page_sub_header', text: 'Pending') %>

<ul>
<% Album.pending.each do |album| %>
<li><%= text_link_to "#{album.title} by #{album.artist.name}", artist_album_path(album.artist, album) %></li>
<% end %>
</ul>
1 change: 1 addition & 0 deletions app/views/shared/_page_sub_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2 class="text-2xl tracking-tight leading-10 font-extrabold mb-3"><%= text %></h2>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
end
end

namespace :admin do
resources 'albums', only: %i[index]
end

resources :purchases, only: %i[show]

resources :artists do
Expand Down
30 changes: 30 additions & 0 deletions test/controllers/admin/albums_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'test_helper'

module Admin
class AlbumsControllerTestSignedInAsAdmin < ActionDispatch::IntegrationTest
def setup
@album = create(:album, publication_status: :pending)
@user = create(:user, admin: true)
log_in_as(@user)
end

test '#index is authorized' do
get admin_albums_path
assert_response :success
end

test '#index lists pending albums' do
get admin_albums_path
assert_select 'li', text: "#{@album.title} by #{@album.artist.name}"
end
end

class AlbumsControllerTestSignedOut < ActionDispatch::IntegrationTest
test '#index is unauthorized' do
get admin_albums_path
assert_redirected_to log_in_url
end
end
end
7 changes: 7 additions & 0 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class AlbumTest < ActiveSupport::TestCase
assert_equal [unpublished_album], Album.unpublished
end

test '.pending' do
create(:album)
create(:album, publication_status: :pending)

assert_equal 1, Album.pending.count
end

test '.in_release_order' do
create(:album, title: 'Unknown', released_on: nil)
create(:album, title: 'Older Published', released_on: nil, first_published_on: Date.parse('2023-01-01'))
Expand Down
15 changes: 15 additions & 0 deletions test/policies/admin/album_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'test_helper'

module Admin
class AlbumPolicyTest < ActiveSupport::TestCase
test 'an admin' do
user = build(:user, admin: true)
album = build(:album)
policy = Admin::AlbumPolicy.new(user, album)

assert policy.index?
end
end
end

0 comments on commit 629512b

Please sign in to comment.