-
Notifications
You must be signed in to change notification settings - Fork 0
Week10 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week10 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def artist_params | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this right to use the name instead of ID
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module TracksHelper | ||
| end |
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would this be optional?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| 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 %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1> New Artist </h1> | ||
| <%= render "form" %> |
| 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> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,4 +20,8 @@ | |
| "<%= comment.body %>" | ||
| </li> | ||
| <% end %> | ||
| </ul> | ||
| </ul> | ||
|
|
||
| <p> | ||
| <%= link_to "Add New Track", new_track_path %> | ||
| </p> | ||
| 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 %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1> Edit Track </h1> | ||
| <%= render "form" %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1> New Track </h1> | ||
| <%= render "form" %> |
| 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> |
| 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 |
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh I understand.