From 301205d3f0c9298f4e7e813183feac65fa4bbc5f Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 12 Jul 2011 11:34:40 +0000 Subject: [PATCH] Implement playlist edit feature * Display playlists as links in index * Move playlist form in a partial --- app/controllers/playlists_controller.rb | 10 +++++ app/views/playlists/_form.html.haml | 4 ++ app/views/playlists/edit.html.haml | 1 + app/views/playlists/index.html.haml | 3 +- app/views/playlists/new.html.haml | 5 +-- features/playlists.feature | 8 ++++ features/step_definitions/playlists_step.rb | 4 ++ spec/controllers/playlists_controller_spec.rb | 37 +++++++++++++++++-- spec/views/playlists/edit.html.haml_spec.rb | 27 ++++++++++++++ spec/views/playlists/index.html.haml_spec.rb | 12 +++--- 10 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 app/views/playlists/_form.html.haml create mode 100644 app/views/playlists/edit.html.haml create mode 100644 spec/views/playlists/edit.html.haml_spec.rb diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index f4a9bd0..03a56a3 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -15,4 +15,14 @@ class PlaylistsController < ApplicationController render :action => 'new' end end + + def edit + @playlist = Playlist.find(params[:id]) + end + + def update + @playlist = Playlist.find(params[:id]) + @playlist.update_attributes params[:playlist] + redirect_to :action => 'index' + end end diff --git a/app/views/playlists/_form.html.haml b/app/views/playlists/_form.html.haml new file mode 100644 index 0000000..106ab72 --- /dev/null +++ b/app/views/playlists/_form.html.haml @@ -0,0 +1,4 @@ += form_for @playlist do |f| + = f.label :name + = f.text_field :name + = f.submit submit_text diff --git a/app/views/playlists/edit.html.haml b/app/views/playlists/edit.html.haml new file mode 100644 index 0000000..ed80003 --- /dev/null +++ b/app/views/playlists/edit.html.haml @@ -0,0 +1 @@ += render 'form', :submit_text => 'Save' diff --git a/app/views/playlists/index.html.haml b/app/views/playlists/index.html.haml index dd883ca..86cc038 100644 --- a/app/views/playlists/index.html.haml +++ b/app/views/playlists/index.html.haml @@ -1,4 +1,5 @@ = link_to 'Create playlist', new_playlist_path %ul - @playlists.each do |p| - %li= p.name + %li + = link_to p.name, edit_playlist_path(p) diff --git a/app/views/playlists/new.html.haml b/app/views/playlists/new.html.haml index 52e732d..7b4174f 100644 --- a/app/views/playlists/new.html.haml +++ b/app/views/playlists/new.html.haml @@ -1,4 +1 @@ -= form_for @playlist do |f| - = f.label :name - = f.text_field :name - = f.submit 'Create' += render 'form', :submit_text => 'Create' diff --git a/features/playlists.feature b/features/playlists.feature index 9dbcda1..41586a8 100644 --- a/features/playlists.feature +++ b/features/playlists.feature @@ -16,3 +16,11 @@ Feature: Playlists And I fill in "Name" with "Electro" And I press "Create" Then I should see "Electro" within "ul>li" + + Scenario: Edit playlist + Given a playlist named Electro + And I am on the playlists page + When I follow "Electro" + And I fill in "Name" with "Rock" + And I press "Save" + Then I should see "Rock" diff --git a/features/step_definitions/playlists_step.rb b/features/step_definitions/playlists_step.rb index 96ace99..5b4513b 100644 --- a/features/step_definitions/playlists_step.rb +++ b/features/step_definitions/playlists_step.rb @@ -2,3 +2,7 @@ Given /^Foo and Bar playlists$/ do @foo = Playlist.create!(:name => 'Foo') @bar = Playlist.create!(:name => 'Bar') end + +Given /^a playlist named Electro$/ do + Playlist.create!(:name => 'Electro') +end diff --git a/spec/controllers/playlists_controller_spec.rb b/spec/controllers/playlists_controller_spec.rb index 0ab28fa..0149f42 100644 --- a/spec/controllers/playlists_controller_spec.rb +++ b/spec/controllers/playlists_controller_spec.rb @@ -7,7 +7,7 @@ describe PlaylistsController do } end - describe "GET index" do + describe 'GET index' do it 'assigns all playlists as @playlists' do playlist = Playlist.create! valid_attributes get :index @@ -15,14 +15,22 @@ describe PlaylistsController do end end - describe "GET new" do + describe 'GET new' do it 'assigns a new playlist as @playlist' do get :new assigns[:playlist].should be_a_new(Playlist) end end - describe "POST create" do + describe 'GET edit' do + it 'assigns the requested playlist as @playlist' do + playlist = Playlist.create! valid_attributes + get :edit, :id => playlist.id.to_s + assigns[:playlist].should == playlist + end + end + + describe 'POST create' do let(:playlist) { mock_model(Playlist).as_null_object } before do @@ -64,4 +72,27 @@ describe PlaylistsController do end end end + + describe 'PUT update' do + it 'updates the playlist' do + playlist = Playlist.create! valid_attributes + Playlist.any_instance.should_receive(:update_attributes). + with({'name' => 'Rock'}) + put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} + end + + it 'assigns the requested playlist as @playlist' do + playlist = Playlist.create! valid_attributes + put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} + assigns[:playlist].should == playlist + end + + context 'when the playlist updates successfully' do + it 'redirects to the playlists index' do + playlist = Playlist.create! valid_attributes + put :update, :id => playlist.id.to_s, :playlist => valid_attributes + response.should redirect_to(:action => 'index') + end + end + end end diff --git a/spec/views/playlists/edit.html.haml_spec.rb b/spec/views/playlists/edit.html.haml_spec.rb new file mode 100644 index 0000000..050d4c8 --- /dev/null +++ b/spec/views/playlists/edit.html.haml_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe 'playlists/edit.html.haml' do + let(:playlist) do + mock_model('Playlist').as_new_record.as_null_object + end + + before do + assign :playlist, playlist + end + + it 'renders a form to edit a playlist' do + render + rendered.should \ + have_selector("form[method=post][action='#{playlists_path}']") + rendered.should have_selector('input[type=submit]') + end + + it 'renders a text field with a label for the playlists name' do + playlist.stub(:name => 'Electro') + render + rendered.should \ + have_selector("input[type=text][name='playlist[name]'][value=Electro]") + rendered.should \ + have_selector("label[for=playlist_name]", :text => 'Name') + end +end diff --git a/spec/views/playlists/index.html.haml_spec.rb b/spec/views/playlists/index.html.haml_spec.rb index 554607c..af2db4c 100644 --- a/spec/views/playlists/index.html.haml_spec.rb +++ b/spec/views/playlists/index.html.haml_spec.rb @@ -3,20 +3,22 @@ require 'spec_helper' describe 'playlists/index.html.haml' do before do assign :playlists, [ - double('Playlist', :name => 'Foo'), - double('Playlist', :name => 'Bar') + mock_model('Playlist', :name => 'Electro') ] end it 'displays a list of playlists' do render - rendered.should have_selector('ul>li', :count => 2) - rendered.should have_selector('ul>li', :text => 'Foo') - rendered.should have_selector('ul>li+li', :text => 'Bar') + rendered.should have_selector('ul>li', :text => 'Electro') end it 'displays a link to create a new playlist' do render rendered.should have_selector('a', :text => 'Create playlist') end + + it 'displays playlists as links' do + render + rendered.should have_selector('a', :text => 'Electro') + end end