Associate a user to his playlists
This commit is contained in:
parent
4f66db2682
commit
7fad9e0402
@ -8,7 +8,7 @@ class PlaylistsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
@playlist = Playlist.new(:name => params[:playlist][:name])
|
||||
@playlist = current_user.playlists.build(params[:playlist])
|
||||
if @playlist.save
|
||||
redirect_to :action => 'index'
|
||||
else
|
||||
|
@ -1,3 +1,6 @@
|
||||
class Playlist < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
validates_presence_of :user_id
|
||||
validates_presence_of :name
|
||||
end
|
||||
|
@ -6,6 +6,8 @@ class User < ActiveRecord::Base
|
||||
attr_reader :password
|
||||
attr_accessible :email, :password, :password_confirmation
|
||||
|
||||
has_many :playlists
|
||||
|
||||
validates_presence_of :email, :password_hash
|
||||
validates_confirmation_of :password
|
||||
|
||||
|
9
db/migrate/20110830110346_add_user_id_to_playlists.rb
Normal file
9
db/migrate/20110830110346_add_user_id_to_playlists.rb
Normal file
@ -0,0 +1,9 @@
|
||||
class AddUserIdToPlaylists < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column :playlists, :user_id, :integer
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column :playlists, :user_id
|
||||
end
|
||||
end
|
@ -10,12 +10,13 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20110809130610) do
|
||||
ActiveRecord::Schema.define(:version => 20110830110346) do
|
||||
|
||||
create_table "playlists", :force => true do |t|
|
||||
t.string "name"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.integer "user_id"
|
||||
end
|
||||
|
||||
create_table "sessions", :force => true do |t|
|
||||
@ -40,6 +41,7 @@ ActiveRecord::Schema.define(:version => 20110809130610) do
|
||||
t.string "email"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "bcrypt_password"
|
||||
t.string "password_hash"
|
||||
end
|
||||
|
||||
|
@ -11,15 +11,14 @@ Feature: Playlists
|
||||
Given a playlist named "Electro"
|
||||
And a playlist named "Reggae"
|
||||
When I am on the playlists page
|
||||
Then I should see "Electro" within "ul>li:first-child"
|
||||
And I should see "Reggae" within "ul>li:first-child+li"
|
||||
Then I should see "Electro" and "Reggae" in a list
|
||||
|
||||
Scenario: Create playlist
|
||||
Given I am on the playlists page
|
||||
When I follow "Create playlist"
|
||||
And I fill in "Name" with "Electro"
|
||||
And I press "Create"
|
||||
Then I should see "Electro" within "ul>li:first-child"
|
||||
Then I should see "Electro" in the list
|
||||
|
||||
Scenario: Edit playlist
|
||||
Given a playlist named "Electro"
|
||||
@ -27,4 +26,4 @@ Feature: Playlists
|
||||
When I follow "Electro"
|
||||
And I fill in "Name" with "Rock"
|
||||
And I press "Save"
|
||||
Then I should see "Rock"
|
||||
Then I should see "Rock" in the list
|
||||
|
@ -1,3 +1,12 @@
|
||||
Given /^a playlist named "([^"]*)"$/ do |name|
|
||||
Factory.create(:playlist, :name => name)
|
||||
end
|
||||
|
||||
Then /^I should see "([^"]*)" and "([^"]*)" in a list$/ do |pl1, pl2|
|
||||
page.should have_selector('ul>li:first-child', :text => pl1)
|
||||
page.should have_selector('ul>li:first-child+li', :text => pl2)
|
||||
end
|
||||
|
||||
Then /^I should see "([^"]*)" in the list$/ do |playlist_name|
|
||||
page.should have_selector('ul>li', :text => playlist_name)
|
||||
end
|
||||
|
@ -1,6 +1,7 @@
|
||||
FactoryGirl.define do
|
||||
factory :playlist do
|
||||
name 'Electro'
|
||||
association :user
|
||||
end
|
||||
|
||||
factory :track do
|
||||
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
||||
|
||||
describe Playlist do
|
||||
subject { playlist }
|
||||
let(:playlist) { Factory.build(:playlist) }
|
||||
let(:playlist) { Factory.build(:playlist) }
|
||||
|
||||
context 'with valid attributes' do
|
||||
it { should be_valid }
|
||||
@ -15,4 +15,20 @@ describe Playlist do
|
||||
|
||||
it { should_not be_valid }
|
||||
end
|
||||
|
||||
context 'without user_id' do
|
||||
before do
|
||||
playlist.user = nil
|
||||
end
|
||||
|
||||
it { should_not be_valid }
|
||||
end
|
||||
|
||||
describe '#user' do
|
||||
it 'returns the user who created the playlist' do
|
||||
user = Factory.create(:user)
|
||||
playlist = user.playlists.build(Factory.attributes_for(:playlist))
|
||||
playlist.user.should == user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user