Implement playlist edit feature

* Display playlists as links in index
* Move playlist form in a partial
This commit is contained in:
Thibault Jouan 2011-07-12 11:34:40 +00:00
parent 33f979b573
commit 301205d3f0
10 changed files with 98 additions and 13 deletions

View File

@ -15,4 +15,14 @@ class PlaylistsController < ApplicationController
render :action => 'new' render :action => 'new'
end end
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 end

View File

@ -0,0 +1,4 @@
= form_for @playlist do |f|
= f.label :name
= f.text_field :name
= f.submit submit_text

View File

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

View File

@ -1,4 +1,5 @@
= link_to 'Create playlist', new_playlist_path = link_to 'Create playlist', new_playlist_path
%ul %ul
- @playlists.each do |p| - @playlists.each do |p|
%li= p.name %li
= link_to p.name, edit_playlist_path(p)

View File

@ -1,4 +1 @@
= form_for @playlist do |f| = render 'form', :submit_text => 'Create'
= f.label :name
= f.text_field :name
= f.submit 'Create'

View File

@ -16,3 +16,11 @@ Feature: Playlists
And I fill in "Name" with "Electro" And I fill in "Name" with "Electro"
And I press "Create" And I press "Create"
Then I should see "Electro" within "ul>li" 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"

View File

@ -2,3 +2,7 @@ Given /^Foo and Bar playlists$/ do
@foo = Playlist.create!(:name => 'Foo') @foo = Playlist.create!(:name => 'Foo')
@bar = Playlist.create!(:name => 'Bar') @bar = Playlist.create!(:name => 'Bar')
end end
Given /^a playlist named Electro$/ do
Playlist.create!(:name => 'Electro')
end

View File

@ -7,7 +7,7 @@ describe PlaylistsController do
} }
end end
describe "GET index" do describe 'GET index' do
it 'assigns all playlists as @playlists' do it 'assigns all playlists as @playlists' do
playlist = Playlist.create! valid_attributes playlist = Playlist.create! valid_attributes
get :index get :index
@ -15,14 +15,22 @@ describe PlaylistsController do
end end
end end
describe "GET new" do describe 'GET new' do
it 'assigns a new playlist as @playlist' do it 'assigns a new playlist as @playlist' do
get :new get :new
assigns[:playlist].should be_a_new(Playlist) assigns[:playlist].should be_a_new(Playlist)
end end
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 } let(:playlist) { mock_model(Playlist).as_null_object }
before do before do
@ -64,4 +72,27 @@ describe PlaylistsController do
end end
end 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 end

View File

@ -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

View File

@ -3,20 +3,22 @@ require 'spec_helper'
describe 'playlists/index.html.haml' do describe 'playlists/index.html.haml' do
before do before do
assign :playlists, [ assign :playlists, [
double('Playlist', :name => 'Foo'), mock_model('Playlist', :name => 'Electro')
double('Playlist', :name => 'Bar')
] ]
end end
it 'displays a list of playlists' do it 'displays a list of playlists' do
render render
rendered.should have_selector('ul>li', :count => 2) rendered.should have_selector('ul>li', :text => 'Electro')
rendered.should have_selector('ul>li', :text => 'Foo')
rendered.should have_selector('ul>li+li', :text => 'Bar')
end end
it 'displays a link to create a new playlist' do it 'displays a link to create a new playlist' do
render render
rendered.should have_selector('a', :text => 'Create playlist') rendered.should have_selector('a', :text => 'Create playlist')
end end
it 'displays playlists as links' do
render
rendered.should have_selector('a', :text => 'Electro')
end
end end