Skip to content

Commit

Permalink
Allow artist to edit their own artist page
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislo committed Nov 14, 2023
1 parent 7f71444 commit b39c2d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/policies/artist_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def create?
end

def edit?
user.admin?
user.admin? || user.artists.include?(record)
end

def update?
user.admin
user.admin || user.artists.include?(record)
end

def new?
Expand Down
4 changes: 4 additions & 0 deletions app/views/artists/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<% end %>

<% content_for :navbar do %>
<% if policy(@artist).edit? %>
<%= link_to "Edit artist", edit_artist_path(@artist), class: 'px-2 py-1 bg-amber-600 hover:bg-amber-500 text-white cursor-pointer text-sm' %>
<% end %>

<% if policy(@artist.albums.new).new? %>
<%= link_to "Add album", new_artist_album_path(@artist), class: 'px-2 py-1 bg-amber-600 hover:bg-amber-500 text-white cursor-pointer text-sm' %>
<% end %>
Expand Down
13 changes: 13 additions & 0 deletions test/policies/artist_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@ class ArtistPolicyTest < ActiveSupport::TestCase

assert_equal [listed_artist], scope.resolve
end

test 'a user with an artist' do
artist = create(:artist)
user = create(:user, artists: [artist])

policy = ArtistPolicy.new(user, artist)

assert_not policy.destroy?
assert_not policy.create?
assert policy.update?
assert policy.edit?
assert_not policy.new?
end
end
17 changes: 16 additions & 1 deletion test/system/artists_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

class ArtistsTest < ApplicationSystemTestCase
setup do
sign_in_as(create(:user, admin: true))
@artist = build(:artist)
end

test 'adding a new artist' do
admin = create(:user, admin: true)
sign_in_as(admin)

visit artists_url
click_link 'New artist'
fill_in 'Name', with: @artist.name
Expand All @@ -18,4 +20,17 @@ class ArtistsTest < ApplicationSystemTestCase
click_button 'Save'
assert_selector 'h2', text: @artist.name
end

test 'editing a new artist' do
@artist.save
artist_user = create(:user, artists: [@artist])
sign_in_as(artist_user)

visit artist_url(@artist)
assert_text @artist.name
click_link 'Edit artist'
fill_in 'Name', with: 'New Artist Name'
click_button 'Save'
assert_text 'New Artist Name'
end
end

0 comments on commit b39c2d7

Please sign in to comment.