Associate a user to his playlists
This commit is contained in:
parent
4f66db2682
commit
7fad9e0402
@ -8,7 +8,7 @@ class PlaylistsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@playlist = Playlist.new(:name => params[:playlist][:name])
|
@playlist = current_user.playlists.build(params[:playlist])
|
||||||
if @playlist.save
|
if @playlist.save
|
||||||
redirect_to :action => 'index'
|
redirect_to :action => 'index'
|
||||||
else
|
else
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
class Playlist < ActiveRecord::Base
|
class Playlist < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
|
||||||
|
validates_presence_of :user_id
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
end
|
end
|
||||||
|
@ -6,6 +6,8 @@ class User < ActiveRecord::Base
|
|||||||
attr_reader :password
|
attr_reader :password
|
||||||
attr_accessible :email, :password, :password_confirmation
|
attr_accessible :email, :password, :password_confirmation
|
||||||
|
|
||||||
|
has_many :playlists
|
||||||
|
|
||||||
validates_presence_of :email, :password_hash
|
validates_presence_of :email, :password_hash
|
||||||
validates_confirmation_of :password
|
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.
|
# 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|
|
create_table "playlists", :force => true do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
|
t.integer "user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "sessions", :force => true do |t|
|
create_table "sessions", :force => true do |t|
|
||||||
@ -40,6 +41,7 @@ ActiveRecord::Schema.define(:version => 20110809130610) do
|
|||||||
t.string "email"
|
t.string "email"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
|
t.string "bcrypt_password"
|
||||||
t.string "password_hash"
|
t.string "password_hash"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,15 +11,14 @@ Feature: Playlists
|
|||||||
Given a playlist named "Electro"
|
Given a playlist named "Electro"
|
||||||
And a playlist named "Reggae"
|
And a playlist named "Reggae"
|
||||||
When I am on the playlists page
|
When I am on the playlists page
|
||||||
Then I should see "Electro" within "ul>li:first-child"
|
Then I should see "Electro" and "Reggae" in a list
|
||||||
And I should see "Reggae" within "ul>li:first-child+li"
|
|
||||||
|
|
||||||
Scenario: Create playlist
|
Scenario: Create playlist
|
||||||
Given I am on the playlists page
|
Given I am on the playlists page
|
||||||
When I follow "Create playlist"
|
When I follow "Create playlist"
|
||||||
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:first-child"
|
Then I should see "Electro" in the list
|
||||||
|
|
||||||
Scenario: Edit playlist
|
Scenario: Edit playlist
|
||||||
Given a playlist named "Electro"
|
Given a playlist named "Electro"
|
||||||
@ -27,4 +26,4 @@ Feature: Playlists
|
|||||||
When I follow "Electro"
|
When I follow "Electro"
|
||||||
And I fill in "Name" with "Rock"
|
And I fill in "Name" with "Rock"
|
||||||
And I press "Save"
|
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|
|
Given /^a playlist named "([^"]*)"$/ do |name|
|
||||||
Factory.create(:playlist, :name => name)
|
Factory.create(:playlist, :name => name)
|
||||||
end
|
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
|
FactoryGirl.define do
|
||||||
factory :playlist do
|
factory :playlist do
|
||||||
name 'Electro'
|
name 'Electro'
|
||||||
|
association :user
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :track do
|
factory :track do
|
||||||
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe Playlist do
|
describe Playlist do
|
||||||
subject { playlist }
|
subject { playlist }
|
||||||
let(:playlist) { Factory.build(:playlist) }
|
let(:playlist) { Factory.build(:playlist) }
|
||||||
|
|
||||||
context 'with valid attributes' do
|
context 'with valid attributes' do
|
||||||
it { should be_valid }
|
it { should be_valid }
|
||||||
@ -15,4 +15,20 @@ describe Playlist do
|
|||||||
|
|
||||||
it { should_not be_valid }
|
it { should_not be_valid }
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user