Skip to content

Commit

Permalink
Merge pull request #320 from blackcandy-org/playlist-songs-controller…
Browse files Browse the repository at this point in the history
…s-refactoring

Refactor playlist songs controllers
  • Loading branch information
aidewoode committed Nov 27, 2023
2 parents 9db0a8d + 4c88050 commit 5a17b4a
Show file tree
Hide file tree
Showing 46 changed files with 358 additions and 231 deletions.
10 changes: 0 additions & 10 deletions app/controllers/albums_controller.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class AlbumsController < ApplicationController
include PlayableConcern

before_action :require_admin, only: [:update]
before_action :find_album, except: [:index]
before_action :get_sort_option, only: [:index]
Expand Down Expand Up @@ -40,14 +38,6 @@ def find_album
@album = Album.find(params[:id])
end

def find_all_song_ids
@song_ids = Album.find(params[:id]).song_ids
end

def after_played
Current.user.add_album_to_recently_played(@album)
end

def filter_params
params[:filter]&.slice(*Album::VALID_FILTERS)
end
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/api/v1/current_playlist/songs/albums_controller.rb
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Api
module V1
class CurrentPlaylist::Songs::AlbumsController < ApiController
before_action :find_current_playlist
before_action :find_album
after_action :add_to_recently_played, only: [:update]

def update
@current_playlist.replace(@album.song_ids)
end

private

def find_current_playlist
@current_playlist = Current.user.current_playlist
end

def find_album
@album = Album.find(params[:id])
end

def add_to_recently_played
Current.user.add_album_to_recently_played(@album)
end
end
end
end
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Api
module V1
class CurrentPlaylist::Songs::PlaylistsController < ApiController
before_action :find_current_playlist
before_action :find_playlist

def update
@current_playlist.replace(@playlist.song_ids)
end

private

def find_current_playlist
@current_playlist = Current.user.current_playlist
end

def find_playlist
@playlist = Current.user.playlists_with_favorite.find(params[:id])
end
end
end
end
22 changes: 13 additions & 9 deletions app/controllers/api/v1/current_playlist/songs_controller.rb
Expand Up @@ -4,22 +4,18 @@ module Api
module V1
class CurrentPlaylist::SongsController < ApiController
before_action :find_playlist
before_action :find_song, only: [:destroy, :move]

def show
def index
@songs = @playlist.songs_with_favorite
end

def destroy
if params[:clear_all]
@playlist.songs.clear
else
songs = Song.find(params[:song_ids])
@playlist.songs.destroy(songs)
end
@playlist.songs.destroy(@song)
end

def update
moving_song = @playlist.playlists_songs.find_by!(song_id: params[:song_id])
def move
moving_song = @playlist.playlists_songs.find_by!(song_id: @song.id)
destination_song = @playlist.playlists_songs.find_by!(song_id: params[:destination_song_id])

moving_song.update(position: destination_song.position)
Expand All @@ -38,11 +34,19 @@ def create
raise BlackCandy::DuplicatePlaylistSong
end

def destroy_all
@playlist.songs.clear
end

private

def find_playlist
@playlist = Current.user.current_playlist
end

def find_song
@song = @playlist.songs.find(params[:id])
end
end
end
end
5 changes: 3 additions & 2 deletions app/controllers/api/v1/favorite_playlist/songs_controller.rb
Expand Up @@ -4,9 +4,10 @@ module Api
module V1
class FavoritePlaylist::SongsController < ApiController
before_action :find_playlist
before_action :find_song
before_action :find_song, only: [:destroy]

def create
@song = Song.find(params[:song_id])
@playlist.playlists_songs.create(song_id: @song.id, position: 1)
rescue ActiveRecord::RecordNotUnique
raise BlackCandy::DuplicatePlaylistSong
Expand All @@ -19,7 +20,7 @@ def destroy
private

def find_song
@song = Song.find(params[:song_id])
@song = @playlist.songs.find(params[:id])
end

def find_playlist
Expand Down
31 changes: 0 additions & 31 deletions app/controllers/concerns/playable_concern.rb

This file was deleted.

26 changes: 26 additions & 0 deletions app/controllers/current_playlist/songs/albums_controller.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class CurrentPlaylist::Songs::AlbumsController < ApplicationController
before_action :find_current_playlist
before_action :find_album
after_action :add_to_recently_played, only: [:update]

def update
@current_playlist.replace(@album.song_ids)
redirect_to current_playlist_songs_path(should_play_all: true)
end

private

def find_current_playlist
@current_playlist = Current.user.current_playlist
end

def find_album
@album = Album.find(params[:id])
end

def add_to_recently_played
Current.user.add_album_to_recently_played(@album)
end
end
21 changes: 21 additions & 0 deletions app/controllers/current_playlist/songs/playlists_controller.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class CurrentPlaylist::Songs::PlaylistsController < ApplicationController
before_action :find_current_playlist
before_action :find_playlist

def update
@current_playlist.replace(@playlist.song_ids)
redirect_to current_playlist_songs_path(should_play_all: true)
end

private

def find_current_playlist
@current_playlist = Current.user.current_playlist
end

def find_playlist
@playlist = Current.user.playlists_with_favorite.find(params[:id])
end
end
8 changes: 6 additions & 2 deletions app/controllers/current_playlist/songs_controller.rb
Expand Up @@ -3,11 +3,15 @@
class CurrentPlaylist::SongsController < Playlists::SongsController
layout "playlist"

def show
skip_before_action :redirect_to_built_in_playlist

def index
@songs = @playlist.songs_with_favorite
end

def create
@song = Song.find(params[:song_id])

if params[:location] == "last"
@playlist.songs.push(@song)
else
Expand All @@ -17,7 +21,7 @@ def create

flash.now[:success] = t("success.add_to_playlist")

redirect_to action: "show", playable: params[:song_playable] if @playlist.songs.count == 1
redirect_to action: "index", should_play_all: params[:should_play] if @playlist.songs.count == 1
rescue ActiveRecord::RecordNotUnique
flash.now[:error] = t("error.already_in_playlist")
render turbo_stream: render_flash
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dialog/playlists_controller.rb
Expand Up @@ -3,7 +3,7 @@
module Dialog
class PlaylistsController < DialogController
def index
@pagy, @playlists = pagy(Current.user.all_playlists.order(created_at: :desc))
@pagy, @playlists = pagy(Current.user.playlists_with_favorite.order(created_at: :desc))
end

def new
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/favorite_playlist/songs_controller.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class FavoritePlaylist::SongsController < Playlists::SongsController
skip_before_action :redirect_to_built_in_playlist

private

def find_playlist
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/libraries_controller.rb
Expand Up @@ -5,6 +5,6 @@ def show
@albums_count = Album.count
@artists_count = Artist.count
@songs_count = Song.count
@playlists_count = Current.user.all_playlists.count
@playlists_count = Current.user.playlists_with_favorite.count
end
end
39 changes: 21 additions & 18 deletions app/controllers/playlists/songs_controller.rb
Expand Up @@ -2,53 +2,56 @@

class Playlists::SongsController < ApplicationController
before_action :find_playlist
before_action :find_song, only: [:create, :destroy]
before_action :find_song, only: [:move, :destroy]
before_action :redirect_to_built_in_playlist, only: [:index]

include PlayableConcern

def show
def index
@pagy, @songs = pagy(@playlist.songs.includes(:artist))
end

def create
@song = Song.find(params[:song_id])
@playlist.songs.push(@song)

flash[:success] = t("success.add_to_playlist")
rescue ActiveRecord::RecordNotUnique
flash[:error] = t("error.already_in_playlist")
ensure
redirect_back_with_referer_params(fallback_location: {action: "show"})
redirect_back_with_referer_params(fallback_location: {action: "index"})
end

def destroy
if params[:clear_all]
@playlist.songs.clear
else
@playlist.songs.destroy(@song)
flash.now[:success] = t("success.delete_from_playlist")
end
@playlist.songs.destroy(@song)
flash.now[:success] = t("success.delete_from_playlist")

# for refresh playlist content, when remove last song from playlist
redirect_to action: "show" if @playlist.songs.empty?
redirect_to action: "index" if @playlist.songs.empty?
end

def update
moving_song = @playlist.playlists_songs.find_by!(song_id: params[:song_id])
def move
moving_song = @playlist.playlists_songs.find_by!(song_id: @song.id)
destination_song = @playlist.playlists_songs.find_by!(song_id: params[:destination_song_id])

moving_song.update(position: destination_song.position)
end

def destroy_all
@playlist.songs.clear
redirect_to action: "index"
end

private

def find_playlist
@playlist = Current.user.playlists.find(params[:playlist_id])
@playlist = Current.user.all_playlists.find(params[:playlist_id])
end

def find_song
@song = Song.find(params[:song_id]) unless params[:clear_all]
@song = @playlist.songs.find(params[:id])
end

def find_all_song_ids
@song_ids = @playlist.song_ids
def redirect_to_built_in_playlist
redirect_to current_playlist_songs_path if @playlist.current?
redirect_to favorite_playlist_songs_path if @playlist.favorite?
end
end
2 changes: 1 addition & 1 deletion app/controllers/playlists_controller.rb
Expand Up @@ -5,7 +5,7 @@ class PlaylistsController < ApplicationController
before_action :get_sort_option, only: [:index]

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

def create
Expand Down
10 changes: 0 additions & 10 deletions app/helpers/playlist_helper.rb

This file was deleted.

0 comments on commit 5a17b4a

Please sign in to comment.