Skip to content
Merged
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
19 changes: 19 additions & 0 deletions app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@ def index
def show
@artist = Artist.find(params[:id])
end

def new
@artist = Artist.new
end

def create
@artist = Artist.new(artist_params)
if @artist.save
redirect_to @artist, notice: "Artist Created"
else
render :new, status: :unprocessable_entity
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure you are formatting

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was a bit thrown off by this as well , in past if/else blocks (React JS) the else was always 1 indent to the right of the if, but in rails the Ruby style guide suggest the else is aligned with the if, and Rubocop enforces it that way.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh I understand.

end
end

private

def artist_params
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right to use the name instead of ID

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is for creating an artist, It does not get an id until after it is created, What the user types in the params is to be the name of the artist.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thats my fault. miss understood what this was.

params.require(:artist).permit(:name)
end
end
38 changes: 38 additions & 0 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,42 @@ def index
def show
@track = Track.find(params[:id])
end

def new
@track = Track.new
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
end
end

def edit
@track = Track.find(params[:id])
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
end
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
2 changes: 2 additions & 0 deletions app/helpers/tracks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TracksHelper
end
2 changes: 1 addition & 1 deletion app/models/track.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Track < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this be optional?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh Yes that was my first attempt at creating a track without a user, Eventually I went with hardcoding user (username: Twhite) into the controller ( after the user/auth set up , i will change it to current_user )

TLDR - it was a work around for creating CRUD with out users, that i forgot to delete.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh okay. Thats good to understand. Definitely should change it to current user.

belongs_to :artist

has_many :comments, dependent: :destroy
Expand Down
21 changes: 21 additions & 0 deletions app/views/artists/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_with model: @artist do |form| %>
<% if @artist.errors.any? %>
<div class="error-messages">
<ul>
<% @artist.errors.full_messgaes.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :name %><br>
<%= form.text_field :name %>
</div>

<div>
<%= form.submit %>
</div>

<% end %>
2 changes: 2 additions & 0 deletions app/views/artists/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1> New Artist </h1>
<%= render "form" %>
2 changes: 1 addition & 1 deletion app/views/artists/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1><%= @artist.name %></h1>
<h1>Artist: <%= @artist.name %></h1>

<h2>Tracks</h2>
<ul>
Expand Down
6 changes: 5 additions & 1 deletion app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
"<%= comment.body %>"
</li>
<% end %>
</ul>
</ul>

<p>
<%= link_to "Add New Track", new_track_path %>
</p>
48 changes: 48 additions & 0 deletions app/views/tracks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<%= form_with model: @track do |form| %>
<% if @track.errors.any? %>
<div class="error-messages">
<h3><%= pluralize(@track.errors.count, "error") %> prohibited this track from being saved:</h3>
<ul>
<% @track.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :title %><br>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :artist_id, "Artist" %><br>
<%= form.collection_select :artist_id, Artist.all, :id, :name, prompt: "Choose an artist" %>
</div>
<p>
<%= link_to "Add New Artist", new_artist_path %>
</p>
<div>
<%= form.label :year %><br>
<%= form.number_field :year %>
</div>

<div>
<%= form.label :bpm %><br>
<%= form.number_field :bpm %>
</div>

<div>
<%= form.label :key %><br>
<%= form.text_field :key %>
</div>

<div>
<%= form.label :notes %><br>
<%= form.text_area :notes %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/tracks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1> Edit Track </h1>
<%= render "form" %>
2 changes: 2 additions & 0 deletions app/views/tracks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1> New Track </h1>
<%= render "form" %>
42 changes: 30 additions & 12 deletions app/views/tracks/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
<h1><%= @track.title %></h1>
<h1>Track: <%= @track.title %></h1>
<p>
Artist: <%= @track.artist.name %><br>
Year: <%= @track.year %><br>
BPM: <%= @track.bpm %>
</p>

<h2>Samples Used</h2>
<ul>
<% @track.sampled_tracks.each do |source| %>
<li><%= link_to source.title, track_path(source) %> - <%= link_to source.artist.name, artist_path(source) %></li>
<% end %>
</ul>

<% if @track.sampled_tracks.any? %>
<ul>
<% @track.sampled_tracks.each do |source| %>
<li><%= link_to source.title, track_path(source) %> -
<%= link_to source.artist.name, artist_path(source) %></li>
<% end %>
</ul>
<% else %>
<p>None</p>
<% end %>

<h2>Sampled By</h2>
<ul>
<% @track.sampled_by_tracks.each do |derived| %>
<li><%= link_to derived.title, track_path(derived) %> - <%= link_to derived.artist.name, artist_path(derived) %></li>
<% end %>
</ul>

<% if @track.sampled_by_tracks.any? %>
<ul>
<% @track.sampled_by_tracks.each do |derived| %>
<li><%= link_to derived.title, track_path(derived) %> -
<%= link_to derived.artist.name, artist_path(derived) %></li>
<% end %>
</ul>
<% else %>
<p>None</p>
<% end %>

<h2>Comments</h2>
<ul>
<% @track.comments.each do |comment| %>
<li><%= comment.user.username %>: <%= comment.body %></li>
<% end %>
</ul>
</ul>

<p>
<%= 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?" } %>
</p>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 "/dashboard", to: "dashboard#index"

Expand Down
15 changes: 15 additions & 0 deletions spec/helpers/tracks_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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 do
pending "add some examples to (or delete) #{__FILE__}"
end
44 changes: 44 additions & 0 deletions spec/requests/tracks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rails_helper'

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
end