From 334586c46174daed6970b5fe1327ff7ed255a85f Mon Sep 17 00:00:00 2001 From: Tristan Date: Fri, 19 Sep 2025 14:27:42 -0400 Subject: [PATCH 1/5] Track Artist title and if else none samples used or by --- app/views/artists/show.html.erb | 2 +- app/views/tracks/show.html.erb | 34 ++++++++++++++++++++++----------- config/routes.rb | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/views/artists/show.html.erb b/app/views/artists/show.html.erb index 35d09fc..d35816a 100644 --- a/app/views/artists/show.html.erb +++ b/app/views/artists/show.html.erb @@ -1,4 +1,4 @@ -

<%= @artist.name %>

+

Artist: <%= @artist.name %>

Tracks

+ +

+ <%= link_to "Add New Track", new_track_path %> +

\ No newline at end of file diff --git a/app/views/tracks/_form.html.erb b/app/views/tracks/_form.html.erb new file mode 100644 index 0000000..29ac095 --- /dev/null +++ b/app/views/tracks/_form.html.erb @@ -0,0 +1,48 @@ +<%= form_with model: @track do |form| %> + <% if @track.errors.any? %> +
+

<%= pluralize(@track.errors.count, "error") %> prohibited this track from being saved:

+ +
+ <% end %> + +
+ <%= form.label :title %>
+ <%= form.text_field :title %> +
+ +
+ <%= form.label :artist_id, "Artist" %>
+ <%= form.collection_select :artist_id, Artist.all, :id, :name, prompt: "Choose an artist" %> +
+

+ <%= link_to "Add New Artist", new_artist_path %> +

+
+ <%= form.label :year %>
+ <%= form.number_field :year %> +
+ +
+ <%= form.label :bpm %>
+ <%= form.number_field :bpm %> +
+ +
+ <%= form.label :key %>
+ <%= form.text_field :key %> +
+ +
+ <%= form.label :notes %>
+ <%= form.text_area :notes %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/tracks/edit.html.erb b/app/views/tracks/edit.html.erb new file mode 100644 index 0000000..ba907f3 --- /dev/null +++ b/app/views/tracks/edit.html.erb @@ -0,0 +1,2 @@ +

Edit Track

+<%= render "form" %> \ No newline at end of file diff --git a/app/views/tracks/new.html.erb b/app/views/tracks/new.html.erb new file mode 100644 index 0000000..6852bd1 --- /dev/null +++ b/app/views/tracks/new.html.erb @@ -0,0 +1,2 @@ +

New Track

+<%= render "form" %> \ No newline at end of file diff --git a/app/views/tracks/show.html.erb b/app/views/tracks/show.html.erb index 5bc4be6..534c482 100644 --- a/app/views/tracks/show.html.erb +++ b/app/views/tracks/show.html.erb @@ -36,4 +36,10 @@ <% @track.comments.each do |comment| %>
  • <%= comment.user.username %>: <%= comment.body %>
  • <% end %> - \ No newline at end of file + + +

    + <%= link_to "Edit Track", edit_track_path(@track) %> | + <%= link_to "Delete Track", track_path(@track), + data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete this track?" } %> +

    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cbd3087..e51d96e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,9 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - resources :artists, only: [ :index, :show ] - resources :tracks, only: [ :index, :show ] + resources :artists, only: [:index, :show, :new, :create] + resources :tracks, only: [ :index, :show, :new, :create, :edit, :update, :destroy ] - get "/dash", to: "dashboard#index" + get "/dashboard", to: "dashboard#index" # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. diff --git a/spec/helpers/tracks_helper_spec.rb b/spec/helpers/tracks_helper_spec.rb new file mode 100644 index 0000000..917c9c8 --- /dev/null +++ b/spec/helpers/tracks_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the TracksHelper. For example: +# +# describe TracksHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe TracksHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/tracks_spec.rb b/spec/requests/tracks_spec.rb new file mode 100644 index 0000000..0d7566b --- /dev/null +++ b/spec/requests/tracks_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +RSpec.describe "Tracks", type: :request do +let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } + let!(:artist) { Artist.create!(name: "Test Artist") } + + describe "POST /tracks" do + it "createsa a new track" do + expect{ + post tracks_path, params: { + track: { title: "New Song", year: 2024, artist_id: artist.id} + } + }.to change(Track, :count).by(1) + + new_track = Track.last + expect(new_track.title).to eq("New Song") + expect(new_track.artist).to eq(artist) + expect(response).to redirect_to(track_path(new_track)) + end +end + + describe "PATCH /tracks/:id" do + let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist)} + + it "updates an exsisting track" do + patch track_path(track), params: { + track: {key: "C minor"} + } + expect(response).to redirect_to(track_path(track)) + + track.reload + expect(track.key).to eq("C minor") + end + end +end \ No newline at end of file From c01d88c4b162275a0be13e2b8979ee3292b3a5d7 Mon Sep 17 00:00:00 2001 From: Tristan Date: Tue, 23 Sep 2025 21:56:31 -0400 Subject: [PATCH 3/5] format --- app/controllers/artists_controller.rb | 19 +++++----- app/controllers/tracks_controller.rb | 52 +++++++++++++-------------- app/models/track.rb | 1 - config/routes.rb | 2 +- spec/helpers/tracks_helper_spec.rb | 2 +- spec/requests/tracks_spec.rb | 36 +++++++++---------- 6 files changed, 54 insertions(+), 58 deletions(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 540ba73..b1e4610 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -13,17 +13,16 @@ def new def create @artist = Artist.new(artist_params) - if @artist.save - redirect_to @artist, notice: "Artist Created" - else - render :new, status: :unprocessable_entity - end + if @artist.save + redirect_to @artist, notice: "Artist Created" + else + render :new, status: :unprocessable_entity + end end - - private - def artist_params - params.require(:artist).permit(:name) - end + private + def artist_params + params.require(:artist).permit(:name) + end end diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index aaf3f77..77d3e67 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -7,43 +7,41 @@ def show @track = Track.find(params[:id]) end - -def new + def new @track = Track.new -end + end -def create - @track = Track.new(track_params) - @track.user = User.find_by(username: "Twhite") - if @track.save - redirect_to @track, notice: "Track Created!" - else - render :new, status: :unprocessable_entity + def create + @track = Track.new(track_params) + @track.user = User.find_by(username: "Twhite") + if @track.save + redirect_to @track, notice: "Track Created!" + else + render :new, status: :unprocessable_entity + end end -end -def edit + def edit @track = Track.find(params[:id]) -end + end -def update - @track = Track.find(params[:id]) - @track.user = User.find_by(username: "Twhite") - if @track.update(track_params) - redirect_to @track, notice: "Track Updated" - else - render :edit, status: :unprocessable_entity + def update + @track = Track.find(params[:id]) + @track.user = User.find_by(username: "Twhite") + if @track.update(track_params) + redirect_to @track, notice: "Track Updated" + else + render :edit, status: :unprocessable_entity + end end -end -def destroy - @track = Track.find(params[:id]) - @track.destroy - redirect_to dashboard_path, notice: "Track Deleted" -end + def destroy + @track = Track.find(params[:id]) + @track.destroy + redirect_to dashboard_path, notice: "Track Deleted" + end def track_params params.require(:track).permit(:title, :artist_id, :year, :bpm, :key, :notes) end - end diff --git a/app/models/track.rb b/app/models/track.rb index 8f9d645..a617471 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -29,5 +29,4 @@ class Track < ApplicationRecord validates :title, presence: true validates :year, presence: true validates :bpm, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true - end diff --git a/config/routes.rb b/config/routes.rb index e51d96e..b9c90f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - resources :artists, only: [:index, :show, :new, :create] + resources :artists, only: [ :index, :show, :new, :create ] resources :tracks, only: [ :index, :show, :new, :create, :edit, :update, :destroy ] get "/dashboard", to: "dashboard#index" diff --git a/spec/helpers/tracks_helper_spec.rb b/spec/helpers/tracks_helper_spec.rb index 917c9c8..02eafde 100644 --- a/spec/helpers/tracks_helper_spec.rb +++ b/spec/helpers/tracks_helper_spec.rb @@ -10,6 +10,6 @@ # end # end # end -RSpec.describe TracksHelper, type: :helper do +RSpec.describe TracksHelper do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/requests/tracks_spec.rb b/spec/requests/tracks_spec.rb index 0d7566b..096f080 100644 --- a/spec/requests/tracks_spec.rb +++ b/spec/requests/tracks_spec.rb @@ -1,30 +1,30 @@ require 'rails_helper' -RSpec.describe "Tracks", type: :request do -let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } - let!(:artist) { Artist.create!(name: "Test Artist") } +RSpec.describe "Tracks" do + let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } + let!(:artist) { Artist.create!(name: "Test Artist") } describe "POST /tracks" do it "createsa a new track" do - expect{ - post tracks_path, params: { - track: { title: "New Song", year: 2024, artist_id: artist.id} - } - }.to change(Track, :count).by(1) + expect { + post tracks_path, params: { + track: { title: "New Song", year: 2024, artist_id: artist.id } + } + }.to change(Track, :count).by(1) - new_track = Track.last - expect(new_track.title).to eq("New Song") - expect(new_track.artist).to eq(artist) - expect(response).to redirect_to(track_path(new_track)) - end -end + new_track = Track.last + expect(new_track.title).to eq("New Song") + expect(new_track.artist).to eq(artist) + expect(response).to redirect_to(track_path(new_track)) + end + end describe "PATCH /tracks/:id" do - let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist)} + let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist) } - it "updates an exsisting track" do + it "updates an exsisting track" do patch track_path(track), params: { - track: {key: "C minor"} + track: { key: "C minor" } } expect(response).to redirect_to(track_path(track)) @@ -32,4 +32,4 @@ expect(track.key).to eq("C minor") end end -end \ No newline at end of file +end From 9cbb220fefc0110e0d11bce245e0bb1fe285a50e Mon Sep 17 00:00:00 2001 From: Tristan Date: Tue, 23 Sep 2025 22:05:16 -0400 Subject: [PATCH 4/5] format spec test --- spec/requests/tracks_spec.rb | 51 ++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/spec/requests/tracks_spec.rb b/spec/requests/tracks_spec.rb index 096f080..011443a 100644 --- a/spec/requests/tracks_spec.rb +++ b/spec/requests/tracks_spec.rb @@ -1,33 +1,44 @@ require 'rails_helper' -RSpec.describe "Tracks" do - let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } +RSpec.describe "Tracks", type: :request do + let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } let!(:artist) { Artist.create!(name: "Test Artist") } - describe "POST /tracks" do - it "createsa a new track" do - expect { - post tracks_path, params: { - track: { title: "New Song", year: 2024, artist_id: artist.id } - } - }.to change(Track, :count).by(1) - - new_track = Track.last - expect(new_track.title).to eq("New Song") - expect(new_track.artist).to eq(artist) - expect(response).to redirect_to(track_path(new_track)) - end + describe "POST /tracks" do + + it "creates a new track" do + expect { + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + }.to change(Track, :count).by(1) + end + + it "redirects to the new track page" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(response).to redirect_to(track_path(Track.last)) + end + + it "saves the correct title" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.title).to eq("New Song") + end + + it "saves the correct artist" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.artist).to eq(artist) + end + end - describe "PATCH /tracks/:id" do + describe "PATCH /tracks/:id" do let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist) } - it "updates an exsisting track" do - patch track_path(track), params: { - track: { key: "C minor" } - } + it "redirects after update" do + patch track_path(track), params: { track: { key: "C minor" } } expect(response).to redirect_to(track_path(track)) + end + it "updates the key" do + patch track_path(track), params: { track: { key: "C minor" } } track.reload expect(track.key).to eq("C minor") end From e51c29261a1aae1180baddff3881ed69c1364176 Mon Sep 17 00:00:00 2001 From: Tristan Date: Tue, 23 Sep 2025 22:06:23 -0400 Subject: [PATCH 5/5] another format --- spec/requests/tracks_spec.rb | 78 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/spec/requests/tracks_spec.rb b/spec/requests/tracks_spec.rb index 011443a..8ebc971 100644 --- a/spec/requests/tracks_spec.rb +++ b/spec/requests/tracks_spec.rb @@ -1,46 +1,44 @@ require 'rails_helper' -RSpec.describe "Tracks", type: :request do +RSpec.describe "Tracks" do let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } let!(:artist) { Artist.create!(name: "Test Artist") } - describe "POST /tracks" do - - it "creates a new track" do - expect { - post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } - }.to change(Track, :count).by(1) - end - - it "redirects to the new track page" do - post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } - expect(response).to redirect_to(track_path(Track.last)) - end - - it "saves the correct title" do - post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } - expect(Track.last.title).to eq("New Song") - end - - it "saves the correct artist" do - post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } - expect(Track.last.artist).to eq(artist) - end - - end - - describe "PATCH /tracks/:id" do - let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist) } - - it "redirects after update" do - patch track_path(track), params: { track: { key: "C minor" } } - expect(response).to redirect_to(track_path(track)) - end - - it "updates the key" do - patch track_path(track), params: { track: { key: "C minor" } } - track.reload - expect(track.key).to eq("C minor") - end - end + describe "POST /tracks" do + it "creates a new track" do + expect { + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + }.to change(Track, :count).by(1) + end + + it "redirects to the new track page" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(response).to redirect_to(track_path(Track.last)) + end + + it "saves the correct title" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.title).to eq("New Song") + end + + it "saves the correct artist" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.artist).to eq(artist) + end + end + + describe "PATCH /tracks/:id" do + let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist) } + + it "redirects after update" do + patch track_path(track), params: { track: { key: "C minor" } } + expect(response).to redirect_to(track_path(track)) + end + + it "updates the key" do + patch track_path(track), params: { track: { key: "C minor" } } + track.reload + expect(track.key).to eq("C minor") + end + end end