Improve tracks CRUD

* Add index, edit and update action;
* Add _form partial;
* Add link to tracks/index in nav;
* Refactor specs.
This commit is contained in:
Thibault Jouan 2015-05-01 12:09:29 +00:00
parent e1c8a6038d
commit 5e1757aff9
9 changed files with 70 additions and 25 deletions

View File

@ -1,4 +1,8 @@
class TracksController < ApplicationController class TracksController < ApplicationController
def index
@tracks = Track.all
end
def show def show
@track = Track.find(params[:id]) @track = Track.find(params[:id])
end end
@ -7,6 +11,10 @@ class TracksController < ApplicationController
@track = Track.new @track = Track.new
end end
def edit
@track = Track.find(params[:id])
end
def create def create
@track = Track.new(track_params) @track = Track.new(track_params)
@ -17,6 +25,17 @@ class TracksController < ApplicationController
end end
end end
def update
@track = Track.find(params[:id])
if @track.update_attributes track_params
redirect_to action: :index
else
render :edit
end
end
private private
def track_params def track_params

View File

@ -12,6 +12,8 @@
%ul %ul
%li %li
= link_to 'Playlists', playlists_path = link_to 'Playlists', playlists_path
%li
= link_to 'Tracks', tracks_path
%li %li
= link_to 'Sign out', signout_path = link_to 'Sign out', signout_path

View File

@ -0,0 +1,13 @@
= form_for @track, html: { multipart: true } do |t|
%table
%tbody
%tr
%th= t.label :name
%td= t.text_field :name
- if @track.new_record?
%tr
%th= t.label :file
%td= t.file_field :file
%tfoot
%tr
%td= t.submit submit_text

View File

@ -0,0 +1 @@
= render 'form', submit_text: 'Save'

View File

@ -0,0 +1,6 @@
= link_to 'Create track', new_track_path
%ul
- @tracks.each do |p|
%li
= link_to p.name, track_path(p)

View File

@ -1,12 +1 @@
= form_for @track, html: { multipart: true } do |t| = render 'form', submit_text: 'Upload'
%table
%tbody
%tr
%th= t.label :name
%td= t.text_field :name
%tr
%th= t.label :file
%td= t.file_field :file
%tfoot
%tr
%td= t.submit 'Upload'

View File

@ -1,5 +1,9 @@
%h1= @track.name %h1= @track.name
- if @track.sound? - if @track.sound?
%p
%audio{src: sound_path(@track.sound), controls: true, autoplay: true} %audio{src: sound_path(@track.sound), controls: true, autoplay: true}
Your browser does not support the audio element Your browser does not support the audio element
%p
= link_to 'Edit', edit_track_path(@track)

View File

@ -4,22 +4,24 @@ feature 'Tracks CRUD' do
background { sign_in } background { sign_in }
scenario 'shows track' do scenario 'shows track' do
track = FactoryGirl.create(:track, name: 'Mega song') track = create_track
visit tracks_path
visit track_path(track) click_link track[:name]
expect(page).to have_content 'Mega song' expect(page).to have_content track[:name]
end end
scenario 'creates track' do scenario 'edits track' do
visit root_path track = create_track
visit tracks_path
click_link 'Add a track' click_link track[:name]
fill_in 'Name', with: 'Mega song' click_link 'Edit'
attach_file 'File', File.expand_path('spec/fixtures/test.mp3') fill_in 'Name', with: 'new track name'
click_button 'Upload' click_button 'Save'
expect(current_path).to eq track_path Track.first expect(current_path).to eq tracks_path
expect(page).to have_content 'Mega song' expect(page).to have_content 'new track name'
end end
end end

View File

@ -24,4 +24,13 @@ module AcceptanceHelpers
click_button 'Create' click_button 'Create'
playlist playlist
end end
def create_track
track = attributes_for :track
visit tracks_path
click_link 'Create track'
fill_in 'Name', with: track[:name]
click_button 'Upload'
track
end
end end