diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index a53ac74..95fee5c 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -1,4 +1,8 @@ class TracksController < ApplicationController + def index + @tracks = Track.all + end + def show @track = Track.find(params[:id]) end @@ -7,6 +11,10 @@ class TracksController < ApplicationController @track = Track.new end + def edit + @track = Track.find(params[:id]) + end + def create @track = Track.new(track_params) @@ -17,6 +25,17 @@ class TracksController < ApplicationController 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 def track_params diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 7741308..5cab5ae 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,6 +12,8 @@ %ul %li = link_to 'Playlists', playlists_path + %li + = link_to 'Tracks', tracks_path %li = link_to 'Sign out', signout_path diff --git a/app/views/tracks/_form.html.haml b/app/views/tracks/_form.html.haml new file mode 100644 index 0000000..0f0a015 --- /dev/null +++ b/app/views/tracks/_form.html.haml @@ -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 diff --git a/app/views/tracks/edit.html.haml b/app/views/tracks/edit.html.haml new file mode 100644 index 0000000..54b326f --- /dev/null +++ b/app/views/tracks/edit.html.haml @@ -0,0 +1 @@ += render 'form', submit_text: 'Save' diff --git a/app/views/tracks/index.html.haml b/app/views/tracks/index.html.haml new file mode 100644 index 0000000..234e39f --- /dev/null +++ b/app/views/tracks/index.html.haml @@ -0,0 +1,6 @@ += link_to 'Create track', new_track_path + +%ul + - @tracks.each do |p| + %li + = link_to p.name, track_path(p) diff --git a/app/views/tracks/new.html.haml b/app/views/tracks/new.html.haml index e008986..f7aa010 100644 --- a/app/views/tracks/new.html.haml +++ b/app/views/tracks/new.html.haml @@ -1,12 +1 @@ -= form_for @track, html: { multipart: true } do |t| - %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' += render 'form', submit_text: 'Upload' diff --git a/app/views/tracks/show.html.haml b/app/views/tracks/show.html.haml index 6060fca..f82f26a 100644 --- a/app/views/tracks/show.html.haml +++ b/app/views/tracks/show.html.haml @@ -1,5 +1,9 @@ %h1= @track.name - if @track.sound? - %audio{src: sound_path(@track.sound), controls: true, autoplay: true} - Your browser does not support the audio element + %p + %audio{src: sound_path(@track.sound), controls: true, autoplay: true} + Your browser does not support the audio element + +%p + = link_to 'Edit', edit_track_path(@track) diff --git a/spec/features/tracks/crud_spec.rb b/spec/features/tracks/crud_spec.rb index 88a54e2..dd5e082 100644 --- a/spec/features/tracks/crud_spec.rb +++ b/spec/features/tracks/crud_spec.rb @@ -4,22 +4,24 @@ feature 'Tracks CRUD' do background { sign_in } 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 - scenario 'creates track' do - visit root_path + scenario 'edits track' do + track = create_track + visit tracks_path - click_link 'Add a track' - fill_in 'Name', with: 'Mega song' - attach_file 'File', File.expand_path('spec/fixtures/test.mp3') - click_button 'Upload' + click_link track[:name] + click_link 'Edit' + fill_in 'Name', with: 'new track name' + click_button 'Save' - expect(current_path).to eq track_path Track.first - expect(page).to have_content 'Mega song' + expect(current_path).to eq tracks_path + expect(page).to have_content 'new track name' end end diff --git a/spec/support/acceptance_helpers.rb b/spec/support/acceptance_helpers.rb index 66fef94..87407ea 100644 --- a/spec/support/acceptance_helpers.rb +++ b/spec/support/acceptance_helpers.rb @@ -24,4 +24,13 @@ module AcceptanceHelpers click_button 'Create' playlist 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