Skip to content

Commit

Permalink
Fix #279, unify all dialog views under dialog route path
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed May 29, 2023
1 parent f9ca450 commit 8e9d146
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 115 deletions.
7 changes: 1 addition & 6 deletions app/controllers/albums_controller.rb
Expand Up @@ -3,9 +3,7 @@
class AlbumsController < ApplicationController
include Playable

layout proc { "dialog" unless turbo_native? }, only: :edit

before_action :require_admin, only: [:edit, :update]
before_action :require_admin, only: [:update]
before_action :find_album, except: [:index]
before_action :get_sort_options, only: [:index]

Expand All @@ -22,9 +20,6 @@ def show
@album.attach_image_from_discogs
end

def edit
end

def update
if @album.update(album_params)
flash[:success] = t("success.update")
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/artists_controller.rb
@@ -1,9 +1,7 @@
# frozen_string_literal: true

class ArtistsController < ApplicationController
layout proc { "dialog" unless turbo_native? }, only: :edit

before_action :require_admin, only: [:edit, :update]
before_action :require_admin, only: [:update]
before_action :find_artist, except: [:index]
before_action :get_sort_options, only: [:index]

Expand All @@ -19,9 +17,6 @@ def show
@artist.attach_image_from_discogs
end

def edit
end

def update
if @artist.update(artist_params)
flash[:success] = t("success.update")
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/dialog/albums_controller.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Dialog::AlbumsController < DialogController
before_action :require_admin

def edit
@album = Album.find(params[:id])
end
end
9 changes: 9 additions & 0 deletions app/controllers/dialog/artists_controller.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Dialog::ArtistsController < DialogController
before_action :require_admin

def edit
@artist = Artist.find(params[:id])
end
end
12 changes: 9 additions & 3 deletions app/controllers/dialog/playlists_controller.rb
@@ -1,9 +1,15 @@
# frozen_string_literal: true

class Dialog::PlaylistsController < ApplicationController
layout proc { "dialog" unless turbo_native? }

class Dialog::PlaylistsController < DialogController
def index
@pagy, @playlists = pagy(Current.user.all_playlists.order(created_at: :desc))
end

def new
@playlist = Playlist.new
end

def edit
@playlist = Current.user.playlists.find(params[:id])
end
end
5 changes: 5 additions & 0 deletions app/controllers/dialog_controller.rb
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class DialogController < ApplicationController
layout proc { "dialog" unless turbo_native? }
end
11 changes: 1 addition & 10 deletions app/controllers/playlists_controller.rb
@@ -1,22 +1,13 @@
# frozen_string_literal: true

class PlaylistsController < ApplicationController
layout proc { "dialog" unless turbo_native? }, only: [:new, :edit]

before_action :find_playlist, only: [:edit, :destroy, :update]
before_action :find_playlist, only: [:destroy, :update]
before_action :get_sort_options, only: [:index]

def index
@pagy, @playlists = pagy(Current.user.all_playlists.sort_records(*sort_params))
end

def new
@playlist = Playlist.new
end

def edit
end

def create
@playlist = Current.user.playlists.new playlist_params

Expand Down
2 changes: 1 addition & 1 deletion app/views/albums/show.html.erb
Expand Up @@ -14,7 +14,7 @@
<div class='u-mt-large'>
<%= button_to t("label.play_all"), play_album_path(@album), form_class: "u-display-inline-block", class: "c-button c-button--primary", form: {"data-controller" => "native-bridge", "data-turbo-frame" => "turbo-playlist", "data-action" => "turbo:submit-end->native-bridge#playAll"} %>
<% if is_admin? %>
<%= link_to t("label.edit"), edit_album_path(@album), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary u-ml-tiny" %>
<%= link_to t("label.edit"), edit_dialog_album_path(@album), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary u-ml-tiny" %>
<% end %>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/artists/show.html.erb
Expand Up @@ -12,7 +12,7 @@
</div>
<% if is_admin? %>
<div class='u-mt-large'>
<%= link_to t("label.edit"), edit_artist_path(@artist), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary u-mt-large" %>
<%= link_to t("label.edit"), edit_dialog_artist_path(@artist), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary u-mt-large" %>
</div>
<% end %>
</div>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/playlists/index.html.erb
Expand Up @@ -6,7 +6,7 @@
<% unless turbo_native? %>
<h1 class='u-mb-0 u-mr-narrow'><%= t("label.playlists") %></h1>
<% end %>
<%= link_to t("label.add"), new_playlist_path, data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--primary" %>
<%= link_to t("label.add"), new_dialog_playlist_path, data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--primary" %>
</div>

<div class='<%= "u-ml-auto" if turbo_native? %>'>
Expand Down
2 changes: 1 addition & 1 deletion app/views/playlists/songs/show.html.erb
Expand Up @@ -15,7 +15,7 @@
<% unless @songs.blank? %>
<%= button_to t("label.play_all"), play_playlist_songs_path(@playlist), class: "c-button c-button--primary", form_class: "u-display-inline-block", form: {"data-controller" => "native-bridge", "data-turbo-frame" => "turbo-playlist", "data-action" => "turbo:submit-end->native-bridge#playAll"} %>
<% end %>
<%= link_to t("label.edit"), edit_playlist_path(@playlist), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary" %>
<%= link_to t("label.edit"), edit_dialog_playlist_path(@playlist), data: {turbo_frame: ("turbo-dialog" unless turbo_native?)}, class: "c-button c-button--secondary" %>

<details class='c-dropdown' data-test-id='playlist_menu'>
<summary>
Expand Down
10 changes: 6 additions & 4 deletions config/routes.rb
Expand Up @@ -11,15 +11,15 @@
resource :setting, only: [:show, :update]
resource :library, only: [:show]

resources :artists, only: [:index, :show, :edit, :update]
resources :artists, only: [:index, :show, :update]
resources :songs, only: [:index]
resources :albums, only: [:index, :show, :edit, :update], concerns: :playable
resources :albums, only: [:index, :show, :update], concerns: :playable

resources :users, except: [:show] do
resource :setting, only: [:update], module: "users"
end

resources :playlists, except: [:show] do
resources :playlists, only: [:index, :create, :update, :destroy] do
resource :songs, only: [:show, :create, :destroy, :update], module: "playlists", concerns: :playable
end

Expand All @@ -32,7 +32,9 @@
end

namespace :dialog do
resources :playlists, only: [:index]
resources :playlists, only: [:index, :new, :edit]
resources :artists, only: [:edit]
resources :albums, only: [:edit]
end

get "/search", to: "search#index", as: "search"
Expand Down
36 changes: 0 additions & 36 deletions test/controllers/albums_controller_test.rb
Expand Up @@ -12,20 +12,6 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "should show album" do
login
get album_url(albums(:album1))

assert_response :success
end

test "should edit album" do
login users(:admin)
get edit_album_url(albums(:album1))

assert_response :success
end

test "should update image for album" do
album = albums(:album1)
album_original_image_url = album.image.url
Expand Down Expand Up @@ -69,28 +55,6 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest
assert_equal albums(:album1).song_ids, user.current_playlist.reload.song_ids
end

test "should only admin can edit album" do
login

get edit_album_url(albums(:album1))
assert_response :forbidden

patch album_url(albums(:album1)), params: {album: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end

test "should not edit album when is on demo mode" do
with_env("DEMO_MODE" => "true") do
login users(:admin)

get edit_album_url(albums(:album1))
assert_response :forbidden

patch album_url(albums(:album1)), params: {album: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end
end

test "should add album to recently played after playing" do
user = users(:visitor1)

Expand Down
29 changes: 0 additions & 29 deletions test/controllers/artists_controller_test.rb
Expand Up @@ -19,13 +19,6 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "should edit album" do
login users(:admin)
get edit_artist_url(artists(:artist1))

assert_response :success
end

test "should update image for artist" do
artist = artists(:artist1)
artist_original_image_url = artist.image.url
Expand Down Expand Up @@ -57,26 +50,4 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
end

test "should only admin can edit artist" do
login

get edit_artist_url(artists(:artist1))
assert_response :forbidden

patch artist_url(artists(:artist1)), params: {artist: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end

test "should not edit artist when is on demo mode" do
with_env("DEMO_MODE" => "true") do
login users(:admin)

get edit_artist_url(artists(:artist1))
assert_response :forbidden

patch artist_url(artists(:artist1)), params: {artist: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end
end
end
34 changes: 34 additions & 0 deletions test/controllers/dialog/albums_controller_test.rb
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "test_helper"

class Dialog::AlbumsControllerTest < ActionDispatch::IntegrationTest
test "should edit album" do
login users(:admin)
get edit_dialog_album_url(albums(:album1))

assert_response :success
end

test "should only admin can edit album" do
login

get edit_dialog_album_url(albums(:album1))
assert_response :forbidden

patch album_url(albums(:album1)), params: {album: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end

test "should not edit album when is on demo mode" do
with_env("DEMO_MODE" => "true") do
login users(:admin)

get edit_dialog_album_url(albums(:album1))
assert_response :forbidden

patch album_url(albums(:album1)), params: {album: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end
end
end
34 changes: 34 additions & 0 deletions test/controllers/dialog/artists_controller_test.rb
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "test_helper"

class Dialog::ArtistsControllerTest < ActionDispatch::IntegrationTest
test "should edit album" do
login users(:admin)
get edit_dialog_artist_url(artists(:artist1))

assert_response :success
end

test "should only admin can edit artist" do
login

get edit_dialog_artist_url(artists(:artist1))
assert_response :forbidden

patch artist_url(artists(:artist1)), params: {artist: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end

test "should not edit artist when is on demo mode" do
with_env("DEMO_MODE" => "true") do
login users(:admin)

get edit_dialog_artist_url(artists(:artist1))
assert_response :forbidden

patch artist_url(artists(:artist1)), params: {artist: {image: fixture_file_upload("cover_image.jpg", "image/jpeg")}}
assert_response :forbidden
end
end
end
17 changes: 17 additions & 0 deletions test/controllers/dialog/playlists_controller_test.rb
Expand Up @@ -9,4 +9,21 @@ class Dialog::PlaylistsControllerTest < ActionDispatch::IntegrationTest

assert_response :success
end

test "should get new playlist" do
login
get new_dialog_playlist_path

assert_response :success
end

test "should edit playlist" do
playlist = playlists(:playlist1)
user = playlist.user

login user
get edit_dialog_playlist_path(playlist)

assert_response :success
end
end
17 changes: 0 additions & 17 deletions test/controllers/playlists_controller_test.rb
Expand Up @@ -56,21 +56,4 @@ class PlaylistsControllerTest < ActionDispatch::IntegrationTest

assert_equal playlists_count - 1, Playlist.count
end

test "should get new playlist" do
login
get new_playlist_path

assert_response :success
end

test "should edit playlist" do
playlist = playlists(:playlist1)
user = playlist.user

login user
get edit_playlist_path(playlist)

assert_response :success
end
end

0 comments on commit 8e9d146

Please sign in to comment.