Skip to content

Commit

Permalink
Add Album resources (#28)
Browse files Browse the repository at this point in the history
* add album model

* run migration

* add 404 test

* rewrite tests

* controllers and views

* add tests

* edit artists spec

* add release year

* fix config for bullet gem

* fix bullet

* song's link to album
  • Loading branch information
BlazingRockStorm authored Oct 4, 2023
1 parent 1aed872 commit c5e3b6b
Show file tree
Hide file tree
Showing 41 changed files with 640 additions and 38 deletions.
76 changes: 76 additions & 0 deletions app/controllers/admin/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

module Admin
class AlbumsController < ApplicationController
before_action :set_album, only: %i[show edit update destroy]
before_action :authenticate_user!
PAGE_LIMIT = 10

# GET /albums or /albums.json
def index
@albums = Album.page(params[:page]).
per(PAGE_LIMIT).order('id ASC')
end

# GET /albums/1 or /albums/1.json
def show; end

# GET /albums/new
def new
@album = Album.new
end

# GET /albums/1/edit
def edit; end

# POST /albums or /albums.json
def create
@album = Album.new(album_params)

respond_to do |format|
if @album.save
format.html { redirect_to admin_albums_url, notice: 'Album was successfully created.' }
format.json { render :show, status: :created, location: @album }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /albums/1 or /albums/1.json
def update
respond_to do |format|
if @album.update(album_params)
format.html { redirect_to admin_album_url(@album), notice: 'Album was successfully updated.' }
format.json { render :show, status: :ok, location: @album }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

# DELETE /albums/1 or /albums/1.json
def destroy
@album.destroy

respond_to do |format|
format.html { redirect_to admin_albums_url, notice: 'Album was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_album
@album = Album.find(params[:id])
end

# Only allow a list of trusted parameters through.
def album_params
params.require(:album).permit(:name, :artist_id, :release_year)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/admin/songs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def set_song

# Only allow a list of trusted parameters through.
def song_params
params.require(:song).permit(:name, :lyric, :genre_id, :link, :tempo,
params.require(:song).permit(:name, :lyric, :genre_id, :link, :tempo, :album_id,
artists_songs_attributes: [:id, :artist_id, :artist_type, :_destroy])
end
end
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class AlbumsController < ApplicationController
before_action :set_album, only: :show
PAGE_LIMIT = 10

def index
@albums = Album.includes([:artist]).page(params[:page]).
per(PAGE_LIMIT)
end

def show
@songs = @album.songs.includes([:artists, :genre])
end

private

# Use callbacks to share common setup or constraints between actions.
def set_album
@album = begin
Album.find(params[:id])
rescue StandardError
not_found
end
end
end
5 changes: 4 additions & 1 deletion app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ def index
per(PAGE_LIMIT)
end

def show; end
def show
@albums = @artist.albums
@songs = @artist.songs.includes([:artists, :genre, :album])
end

private

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/genres_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def index
per(PAGE_LIMIT)
end

def show; end
def show
@songs = @genre.songs.includes([:artists, :album])
end

private

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/songs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SongsController < ApplicationController
PAGE_LIMIT = 10

def index
@songs = Song.includes(%i[genre artists_songs artists]).
@songs = Song.includes(%i[genre album artists_songs artists]).
page(params[:page]).
per(PAGE_LIMIT)
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Album < ApplicationRecord
belongs_to :artist
has_many :songs, dependent: :nullify
end
1 change: 1 addition & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class Artist < ApplicationRecord
has_many :albums, dependent: :destroy
has_many :artists_songs, dependent: :destroy
has_many :songs, through: :artists_songs
validates :name, presence: true, uniqueness: true
Expand Down
1 change: 1 addition & 0 deletions app/models/song.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Song < ApplicationRecord
belongs_to :genre
belongs_to :album
has_many :artists_songs, dependent: :destroy
has_many :artists, through: :artists_songs
validates :name, presence: true
Expand Down
12 changes: 12 additions & 0 deletions app/views/admin/albums/_album.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
div id="#{dom_id album}"
p
strong Name:
=< album.name

div
strong Artist:
=< album.artist.name

p
strong Release Year:
=< album.release_year
4 changes: 4 additions & 0 deletions app/views/admin/albums/_album.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

json.extract! album, :id, :name, :artist_id, :release_year, :created_at, :updated_at
json.url album_url(album, format: :json)
38 changes: 38 additions & 0 deletions app/views/admin/albums/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
== render "layouts/navbar"

.container-fluid
.row.lyrics-site-content
== render "layouts/sidebar"

main.col-md-9.col-lg-10.mt-2.mb-2
h1 Editing album

= form_with scope: :album, url: admin_album_path(@album), method: :patch do |form|
- if @album.errors.any?
div.alert.alert-danger
h2 = "#{pluralize(@album.errors.count, "error")} prohibited this album from being saved:"
ul
- @album.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 :artist_id, class: 'form-label'
= form.select :artist_id, Artist.all.collect {|artist| [ artist.name, artist.id ] }, {}, class: 'form-control'

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

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

br

div
= link_to "Back to albums", admin_albums_path, class: 'btn btn-secondary'

== render 'layouts/footer'
39 changes: 39 additions & 0 deletions app/views/admin/albums/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
== render "layouts/navbar"

.container-fluid
.row.lyrics-site-content
== render "layouts/sidebar"

main.col-md-9.col-lg-10.mt-2.mb-2
- if notice
p.alert.alert-success = notice

h1 Albums

table.table.table-bordered#albums
thead.table-dark
tr
th[scope='col']
| #
th[scope='col']
| Name
th[scope='col', colspan=3]
| Action
tbody.table-group-divider
- @albums.each do |album|
tr
td
=< album.id
td
=< album.name
td
= link_to "Show this album", admin_album_path(album), class: 'btn btn-success'
td
= link_to "Edit this album", edit_admin_album_path(album), class: 'btn btn-warning'
td
= button_to "Delete this album", admin_album_path(album), method: :delete, class: 'btn btn-danger'

= paginate @albums, theme: 'bootstrap-5'
= link_to "New album", new_admin_album_path, class: 'btn btn-primary'

== render 'layouts/footer'
3 changes: 3 additions & 0 deletions app/views/admin/albums/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.array! @albums, partial: 'albums/album', as: :album
38 changes: 38 additions & 0 deletions app/views/admin/albums/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
== render "layouts/navbar"

.container-fluid
.row.lyrics-site-content
== render "layouts/sidebar"

main.col-md-9.col-lg-10.mt-2.mb-2
h1 New album

= form_with scope: :album, url: admin_albums_path do |form|
- if @album.errors.any?
.alert.alert-danger
h2 = "#{pluralize(@album.errors.count, "error")} prohibited this album from being saved:"
ul
- @album.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 :artist_id, class: 'form-label'
= form.select :artist_id, Artist.all.collect {|artist| [ artist.name, artist.id ] }, {}, class: 'form-control'

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

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

br

div
= link_to "Back to albums", admin_albums_path, class: 'btn btn-secondary'

== render 'layouts/footer'
20 changes: 20 additions & 0 deletions app/views/admin/albums/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
== render "layouts/navbar"

.container-fluid
.row.lyrics-site-content
== render "layouts/sidebar"

main.col-md-9.col-lg-10.mt-2.mb-2
- if notice
p.alert.alert-success = notice

== render @album

.mb-2
=> link_to "Edit this album", edit_admin_album_path(@album), class: 'btn btn-warning'
'|
=< link_to "Back to albums", admin_albums_path, class: 'btn btn-secondary'

= button_to "Delete this album", admin_album_path(@album), method: :delete, class: 'btn btn-danger'

== render 'layouts/footer'
3 changes: 3 additions & 0 deletions app/views/admin/albums/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.partial! 'albums/album', album: @album
4 changes: 4 additions & 0 deletions app/views/admin/songs/_song.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ div id="#{dom_id song}"
strong Genre:
=< song.genre.name

p
strong Album:
=< song.album.name

p
strong Link:
=< song.link
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/songs/_song.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

json.extract! song, :id, :name, :lyric, :genre_id, :created_at, :updated_at
json.extract! song, :id, :name, :lyric, :genre_id, :album_id, :created_at, :updated_at
json.url song_url(song, format: :json)
4 changes: 4 additions & 0 deletions app/views/admin/songs/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
= 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
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/songs/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
= 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
Expand Down
22 changes: 22 additions & 0 deletions app/views/albums/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- content_for :title, 'Japanese lyrics site'

== render 'layouts/navbar_for_viewer'

h1 All Albums

.container-fluid
.row.mt-2.lyrics-site-content
main.col-md-9.col-lg-10
- @albums.each do |album|
.card.mb-2
.card-body
p
= link_to album.name, album_path(album)
p
= album.artist.name
p
= album.release_year

= paginate @albums, theme: 'bootstrap-5'

== render 'layouts/footer'
Loading

0 comments on commit c5e3b6b

Please sign in to comment.