Skip to content
Draft
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
21 changes: 19 additions & 2 deletions app/controllers/playlists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ def index
end

def create
@playlist = Current.user.playlists.create!(playlist_params)
Playlist.transaction do
@playlist = Current.user.playlists.create!(playlist_params)
@playlist.songs.push(Song.find(params[:song_id])) if add_song_to_created_playlist?
end

respond_to do |format|
format.html { redirect_to action: "index", notice: t("notice.created") }
format.html { redirect_to after_create_path, notice: create_notice }
format.json { render partial: "playlists/playlist", locals: { playlist: @playlist }, status: :created }
end
rescue ActiveRecord::RecordNotUnique
raise BlackCandy::DuplicatePlaylistSong
end

def update
Expand Down Expand Up @@ -45,6 +50,18 @@ def playlist_params
params.require(:playlist).permit(:name)
end

def after_create_path
add_song_to_created_playlist? ? playlist_songs_path(@playlist) : { action: "index" }
end

def create_notice
add_song_to_created_playlist? ? t("notice.added_to_playlist") : t("notice.created")
end

def add_song_to_created_playlist?
params[:song_id].present?
end

def sort_params
[ params[:sort], params[:sort_direction] ]
end
Expand Down
15 changes: 14 additions & 1 deletion app/views/dialog/playlists/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
<% page_title_tag t("label.add_to_playlist") %>

<%= form_with model: Playlist.new, class: "c-form", data: { "turbo-frame" => "_top", "turbo-action" => ("replace" if native_app?) } do |form| %>
<%= hidden_field_tag :song_id, params[:song_id] if params[:song_id].present? %>

<div class='c-form__field'>
<%= form.label :name, t("label.create_playlist") %>
<%= form.text_field :name, class: "c-input" %>
</div>

<div class='c-form__field c-form__field--submit'>
<%= form.submit t(params[:song_id].present? ? "button.create_and_add" : "button.save"), class: "c-button c-button--primary c-button--full-width" %>
</div>
<% end %>

<% if @playlists.present? %>
<%= form_tag nil, "data-turbo-frame": "_top", "data-turbo-action": ("replace" if native_app?) do %>
<%= hidden_field_tag :song_id, params[:song_id] %>
<%= hidden_field_tag :song_id, params[:song_id] if params[:song_id].present? %>

<%= turbo_frame_tag "turbo-dialog-playlists-page-#{@pagy.page}", class: "c-list", target: "_top" do %>
<%= render partial: "dialog/playlists/playlist", collection: @playlists %>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ en:
play_now: 'Play Now'
play_next: 'Play Next'
play_last: 'Play Last'
create_and_add: 'Create and Add'

field:
name: 'Name'
Expand Down Expand Up @@ -122,4 +123,4 @@ en:
not_exist: 'does not exist'
unreadable: 'is unreadable'
invalid_content_type: "has an invalid content type"
not_supported_with_sqlite: "is not supported with SQLite"
not_supported_with_sqlite: "is not supported with SQLite"
4 changes: 3 additions & 1 deletion test/controllers/dialog/playlists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
class Dialog::PlaylistsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
login
get dialog_playlists_url
get dialog_playlists_url(song_id: songs(:mp3_sample).id)

assert_response :success
assert_select "form[action='#{playlists_path}']"
assert_select "input[name='song_id'][value='#{songs(:mp3_sample).id}']"
end

test "should get new playlist" do
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/playlists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ class PlaylistsControllerTest < ActionDispatch::IntegrationTest
assert_equal playlists_count + 1, Playlist.count
end

test "should create playlist and add song" do
user = users(:admin)
song = songs(:mp3_sample)

login user

assert_difference -> { user.playlists.count }, 1 do
post playlists_url, params: { playlist: { name: "test" }, song_id: song.id }, xhr: true
end

playlist = user.playlists.order(created_at: :desc).first

assert_equal [ song.id ], playlist.song_ids
assert_redirected_to playlist_songs_path(playlist)
end

test "should has error flash when failed to create playlist" do
login
post playlists_url, params: { playlist: { name: "" } }, xhr: true
Expand Down
Loading