Skip to content

Commit

Permalink
User adds a video to my queue
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-huang committed Feb 10, 2014
1 parent ea384b1 commit f68387f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
22 changes: 21 additions & 1 deletion app/controllers/queue_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
class QueueItemsController < ApplicationController
before_action :require_user

def index
@queue_items = current_user.queue_items
end

def create
video = Video.find(params[:video_id])
queue_video(video)
redirect_to my_queue_path
end

private

def queue_video(video)
QueueItem.create(video: video, user: current_user, position: new_queue_item_position) unless current_user_queued_video?(video)
end

def new_queue_item_position
current_user.queue_items.count + 1
end

def current_user_queued_video?(video)
current_user.queue_items.map(&:video).include?(video)
end
end
3 changes: 1 addition & 2 deletions app/views/queue_items/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
%tbody
- @queue_items.each do |queue_item|
%tr
%td
%input.form-control(type="text" value="1")
%td= queue_item.position
%td
= link_to queue_item.video_title, queue_item.video
%td
Expand Down
4 changes: 2 additions & 2 deletions app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%p #{@video.description}
.actions
%a.btn.btn-primary(href="") Watch Now
%a.btn.btn-default(href="") + My Queue
= link_to "+ My Queue", queue_items_path(video_id: @video.id), method: :post, class: 'btn btn-default'
%section.reviews.container
.row
.col-sm-10.col-sm-offset-1
Expand All @@ -27,7 +27,7 @@
.col-sm-3
= f.text_area :content, rows: 6, cols: 100, class: "span6"
%fieldset.form-group.actions.clearfix
%input(type="submit" value="Submit" class="btn")
%input(type="submit" value="Submit" class="btn btn-default")
= link_to "Cancel", @video
%header
%h3 User Reviews (#{@reviews.count})
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
resources :categories
resources :users, only: [:create]
resources :sessions, only: [:create]
resources :queue_items, only: [:create]
end
56 changes: 56 additions & 0 deletions spec/controllers/queue_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,60 @@
expect(response).to redirect_to sign_in_path
end
end

describe "POST create" do
it "redirects to the my queue page" do
session[:user_id] = Fabricate(:user).id
video = Fabricate(:video)
post :create, video_id: video.id
expect(response).to redirect_to my_queue_path
end

it "creates a queue item" do
session[:user_id] = Fabricate(:user).id
video = Fabricate(:video)
post :create, video_id: video.id
expect(QueueItem.count).to eq(1)
end

it "creates the queue item that is associated with the video" do
session[:user_id] = Fabricate(:user).id
video = Fabricate(:video)
post :create, video_id: video.id
expect(QueueItem.first.video).to eq(video)
end

it "creates the queue item that is associated with the signed in user" do
alice = Fabricate(:user)
session[:user_id] = alice.id
video = Fabricate(:video)
post :create, video_id: video.id
expect(QueueItem.first.user).to eq(alice)
end

it "puts the video as the last one in the queue" do
alice = Fabricate(:user)
session[:user_id] = alice.id
inception = Fabricate(:video)
Fabricate(:queue_item, video: inception, user: alice)
gravity = Fabricate(:video)
post :create, video_id: gravity.id
gravity_queue_item = QueueItem.where(user_id: alice.id, video_id: gravity.id).first
expect(gravity_queue_item.position).to eq(2)
end

it "doesn't add the video in the queue if the video is already in the queue" do
alice = Fabricate(:user)
session[:user_id] = alice.id
inception = Fabricate(:video)
Fabricate(:queue_item, video: inception, user: alice)
post :create, video_id: inception.id
expect(alice.queue_items.count).to eq(1)
end

it "redirects to the sign in page for the unauthenticated users" do
post :create, video_id: Fabricate(:video).id
expect(response).to redirect_to sign_in_path
end
end
end

0 comments on commit f68387f

Please sign in to comment.