Skip to content

Commit

Permalink
refactor song form
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazingRockStorm committed Aug 19, 2024
1 parent 543e670 commit ade6880
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 137 deletions.
58 changes: 58 additions & 0 deletions app/views/admin/songs/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
= form_with model: [:admin, @song], local: true do |form|
- if @song.errors.any?
.alert.alert-danger
h2 = "#{pluralize(@song.errors.count, "error")} prohibited this song from being saved:"
ul
- @song.errors.each do |error|
li = error.full_message

.mb-3
= form.label :name, class: 'form-label'
= form.text_field :name, class: 'form-control'

.mb-3
= form.label :link, class: 'form-label'
= form.text_field :link, class: 'form-control'

.mb-3
= form.label :tempo, class: 'form-label'
= form.number_field :tempo, class: 'form-control'

= form.fields_for :artists_songs_attributes do |artists_song|
/ Main Artist.
.mb-3
= artists_song.label 'Main Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name'), class: 'form-control'
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'main'

div[data-controller='artists-songs']
= button_tag 'Add Featuring Artist', type: 'button', data: { action: 'artists-songs#addFeaturingArtist' }, class: 'btn btn-link mb-3'
div[data-artists-songs-target='featuringArtistsList']
.featuring-field-template[style="display: none;"]
/ Feature Artist.
.mb-3[data-artists-songs-target='featuringArtist']
.row
.col-md-10.offset-md-1
= artists_song.label 'Featuring Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name'), class: 'form-control', disabled: true
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'featuring', disabled: true
= hidden_field_tag 'song[artists_songs_attributes][][_destroy]', '0', disabled: true

.col-md-1
= button_tag '', type: 'button', data: { action: 'artists-songs#removeFeaturingArtist' }, class: 'btn btn-danger' do
span.bi.bi-x-square-fill

.mb-3
= form.label :genre_id, class: 'form-label'
= form.select :genre_id, Genre.all.collect {|genre| [ genre.name, genre.id ] }, {}, class: 'form-control'

.mb-3
= form.label :album_id, class: 'form-label'
= form.select :album_id, Album.all.collect {|album| [ album.name, album.id ] }, {}, class: 'form-control'

.mb-3
= form.label :lyric, class: 'form-label'
= form.text_area :lyric, class: 'form-control', rows: 20

div
= form.submit class: 'btn btn-primary'
79 changes: 1 addition & 78 deletions app/views/admin/songs/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,7 @@
main.col-md-9.col-lg-10.mt-2.mb-2
h1 Editing song

= form_with scope: :song, url: admin_song_path(@song), method: :patch do |form|
- if @song.errors.any?
.alert.alert-danger
h2 = "#{pluralize(@song.errors.count, "error")} prohibited this song from being saved:"
ul
- @song.errors.each do |error|
li = error.full_message

.mb-3
= form.label :name, class: 'form-label'
= form.text_field :name, class: 'form-control'

.mb-3
= form.label :link, class: 'form-label'
= form.text_field :link, class: 'form-control'

.mb-3
= form.label :tempo, class: 'form-label'
= form.number_field :tempo, class: 'form-control'

= form.fields_for @song.artists_songs[0] do |artists_song|
/ Main Artist.
.mb-3
= artists_song.label 'Main Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name', @song.artists[0].id), class: 'form-control'
= hidden_field_tag 'song[artists_songs_attributes][][id]', @song.artists_songs[0].id
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'main'

div[data-controller='artists-songs']
= button_tag 'Add Featuring Artist', type: 'button', data: { action: 'artists-songs#addFeaturingArtist' }, class: 'btn btn-link mb-3'
div[data-artists-songs-target='featuringArtistsList']
.featuring-field-template[style="display: none;"]
/ Feature Artist.
.mb-3[data-artists-songs-target='featuringArtist']
.row
.col-md-10.offset-md-1
= artists_song.label 'Featuring Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name'), class: 'form-control', disabled: true

= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'featuring', disabled: true
= hidden_field_tag 'song[artists_songs_attributes][][_destroy]', '0', disabled: true

.col-md-1
= button_tag '', type: 'button', data: { action: 'artists-songs#removeFeaturingArtist' }, class: 'btn btn-danger' do
span.bi.bi-x-square-fill

- for i in 1...@song.artists.length
.featuring-field-template[id="song_artists_#{i}"]
/ Feature Artist.
.mb-3[data-artists-songs-target='featuringArtist']
.row
.col-md-10.offset-md-1
= artists_song.label 'Featuring Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name', @song.artists[i].id), class: 'form-control'
= hidden_field_tag 'song[artists_songs_attributes][][id]', @song.artists_songs[i].id
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'featuring'
= hidden_field_tag 'song[artists_songs_attributes][][_destroy]', '0'

.col-md-1
= button_tag '', type: 'button', data: { action: 'artists-songs#removeFeaturingArtist', 'artists-songs-template-param': "song_artists_#{i}" }, class: 'btn btn-danger' do
span.bi.bi-x-square-fill

.mb-3
= form.label :genre_id, class: 'form-label'
= form.select :genre_id, Genre.all.collect {|genre| [ genre.name, genre.id ] }, {}, class: 'form-control'

.mb-3
= form.label :album_id, class: 'form-label'
= form.select :album_id, Album.all.collect {|album| [ album.name, album.id ] }, {}, class: 'form-control'

.mb-3
= form.label :lyric, class: 'form-label'
= form.text_area :lyric, class: 'form-control', rows: 20


div
= form.submit 'Save song', class: 'btn btn-primary'

== render 'form', song: @song

br

Expand Down
60 changes: 1 addition & 59 deletions app/views/admin/songs/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,7 @@
main.col-md-9.col-lg-10.mt-2.mb-2
h1 New song

= form_with scope: :song, url: admin_songs_path do |form|
- if @song.errors.any?
.alert.alert-danger
h2 = "#{pluralize(@song.errors.count, "error")} prohibited this song from being saved:"
ul
- @song.errors.each do |error|
li = error.full_message

.mb-3
= form.label :name, class: 'form-label'
= form.text_field :name, class: 'form-control'

.mb-3
= form.label :link, class: 'form-label'
= form.text_field :link, class: 'form-control'

.mb-3
= form.label :tempo, class: 'form-label'
= form.number_field :tempo, class: 'form-control'

= form.fields_for :artists_songs_attributes do |artists_song|
/ Main Artist.
.mb-3
= artists_song.label 'Main Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name'), class: 'form-control'
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'main'

div[data-controller='artists-songs']
= button_tag 'Add Featuring Artist', type: 'button', data: { action: 'artists-songs#addFeaturingArtist' }, class: 'btn btn-link mb-3'
div[data-artists-songs-target='featuringArtistsList']
.featuring-field-template[style="display: none;"]
/ Feature Artist.
.mb-3[data-artists-songs-target='featuringArtist']
.row
.col-md-10.offset-md-1
= artists_song.label 'Featuring Artist', class: 'form-label'
= select_tag 'song[artists_songs_attributes][][artist_id]', options_from_collection_for_select(Artist.all, 'id', 'name'), class: 'form-control', disabled: true
= hidden_field_tag 'song[artists_songs_attributes][][artist_type]', 'featuring', disabled: true
= hidden_field_tag 'song[artists_songs_attributes][][_destroy]', '0', disabled: true

.col-md-1
= button_tag '', type: 'button', data: { action: 'artists-songs#removeFeaturingArtist' }, class: 'btn btn-danger' do
span.bi.bi-x-square-fill

.mb-3
= form.label :genre_id, class: 'form-label'
= form.select :genre_id, Genre.all.collect {|genre| [ genre.name, genre.id ] }, {}, class: 'form-control'

.mb-3
= form.label :album_id, class: 'form-label'
= form.select :album_id, Album.all.collect {|album| [ album.name, album.id ] }, {}, class: 'form-control'

.mb-3
= form.label :lyric, class: 'form-label'
= form.text_area :lyric, class: 'form-control', rows: 20

div
= form.submit 'Create Song', class: 'btn btn-primary'

== render 'form', song: @song

br

Expand Down

0 comments on commit ade6880

Please sign in to comment.