From 7fad9e04026a6cd0bfb687023b090d7efeafb2bc Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 30 Aug 2011 10:50:05 +0000 Subject: [PATCH] Associate a user to his playlists --- app/controllers/playlists_controller.rb | 2 +- app/models/playlist.rb | 3 +++ app/models/user.rb | 2 ++ .../20110830110346_add_user_id_to_playlists.rb | 9 +++++++++ db/schema.rb | 4 +++- features/playlists.feature | 7 +++---- features/step_definitions/playlists_step.rb | 9 +++++++++ spec/factories.rb | 1 + spec/models/playlist_spec.rb | 18 +++++++++++++++++- 9 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20110830110346_add_user_id_to_playlists.rb diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index 0bba16c..e3113c2 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -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 diff --git a/app/models/playlist.rb b/app/models/playlist.rb index e8cf8d0..fe81427 100644 --- a/app/models/playlist.rb +++ b/app/models/playlist.rb @@ -1,3 +1,6 @@ class Playlist < ActiveRecord::Base + belongs_to :user + + validates_presence_of :user_id validates_presence_of :name end diff --git a/app/models/user.rb b/app/models/user.rb index 1e6d889..414b5bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20110830110346_add_user_id_to_playlists.rb b/db/migrate/20110830110346_add_user_id_to_playlists.rb new file mode 100644 index 0000000..a487f08 --- /dev/null +++ b/db/migrate/20110830110346_add_user_id_to_playlists.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b04519e..334810e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/features/playlists.feature b/features/playlists.feature index 92dedcb..daaba2e 100644 --- a/features/playlists.feature +++ b/features/playlists.feature @@ -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 diff --git a/features/step_definitions/playlists_step.rb b/features/step_definitions/playlists_step.rb index cb02b48..2adcffd 100644 --- a/features/step_definitions/playlists_step.rb +++ b/features/step_definitions/playlists_step.rb @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index c736d0c..05bb659 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,6 +1,7 @@ FactoryGirl.define do factory :playlist do name 'Electro' + association :user end factory :track do diff --git a/spec/models/playlist_spec.rb b/spec/models/playlist_spec.rb index b53b428..cb7a7ec 100644 --- a/spec/models/playlist_spec.rb +++ b/spec/models/playlist_spec.rb @@ -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