forked from gotealeaf/myflix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Jameskcoleman/week_2_part_2
Week 2 part 2
- Loading branch information
Showing
15 changed files
with
259 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class QueueItemsController < ApplicationController | ||
before_filter :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 | ||
|
||
def destroy | ||
queue_item = QueueItem.find(params[:id]) | ||
queue_item.destroy if current_user.queue_items.include?(queue_item) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class QueueItem < ActiveRecord::Base | ||
belongs_to :user | ||
belongs_to :video | ||
|
||
delegate :category, to: :video | ||
delegate :title, to: :video, prefix: :video | ||
|
||
def rating | ||
review = Review.where(user_id: user.id, video_id: video.id).first | ||
review.rating if review | ||
end | ||
|
||
def category_name | ||
category.name | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
%section.my_queue.container | ||
.row | ||
.col-sm-10.col-sm-offset-1 | ||
%article | ||
%header | ||
%h2 My Queue | ||
%table.table | ||
%thead | ||
%tr | ||
%th(width="10%") List Order | ||
%th(width="30%") Video Title | ||
%th(width="10%") Play | ||
%th(width="20%") Rating | ||
%th(width="15%") Genre | ||
%th(width="15%") Remove | ||
%tbody | ||
- @queue_items.each do |queue_item| | ||
%tr | ||
%td= queue_item.position | ||
%td | ||
= link_to queue_item.video_title, queue_item.video | ||
%td | ||
= button_to "Play", nil, class: "btn btn-default" | ||
%td= queue_item.rating | ||
%td | ||
= link_to queue_item.category_name, queue_item.category | ||
%td | ||
= link_to queue_item, method: :delete do | ||
%i.glyphicon.glyphicon-remove | ||
= button_to "Update Instant Queue", nil, class: "btn btn-default" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateQueueItems < ActiveRecord::Migration | ||
def change | ||
create_table :queue_items do |t| | ||
t.integer :video_id, :user_id | ||
t.integer :position | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require 'spec_helper' | ||
|
||
describe QueueItemsController do | ||
describe "GET index" do | ||
it "sets @queue_items to the queue items of the logged in user" do | ||
alice = Fabricate(:user) | ||
session[:user_id] = alice.id | ||
queue_item1 = Fabricate(:queue_item, user: alice) | ||
queue_item2 = Fabricate(:queue_item, user: alice) | ||
get :index | ||
expect(assigns(:queue_items)).to match_array([queue_item1, queue_item2]) | ||
end | ||
|
||
it "redirects to the sign in page for unauthenticated users" do | ||
get :index | ||
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 | ||
monk = Fabricate(:video) | ||
Fabricate(:queue_item, video: monk, user: alice) | ||
south_park = Fabricate(:video) | ||
post :create, video_id: south_park.id | ||
south_park_queue_item = QueueItem.where(video_id: south_park.id, user_id: alice.id).first | ||
expect(south_park_queue_item.position).to eq(2) | ||
end | ||
it "does not add the video to the queue if the video is already in the queue" do | ||
alice = Fabricate(:user) | ||
session[:user_id] = alice.id | ||
monk = Fabricate(:video) | ||
Fabricate(:queue_item, video: monk, user: alice) | ||
post :create, video_id: monk.id | ||
expect(alice.queue_items.count).to eq(1) | ||
end | ||
it "redirects to the sign in page for unauthenticated users" do | ||
post :create, video_id: 3 | ||
expect(response).to redirect_to sign_in_path | ||
end | ||
end | ||
|
||
describe "DELETE destroy" do | ||
it "redirects to the my queue page" do | ||
session[:user_id] = Fabricate(:user).id | ||
queue_item = Fabricate(:queue_item) | ||
delete :destroy, id: queue_item.id | ||
expect(response).to redirect_to my_queue_path | ||
end | ||
it "deletes the queue item" do | ||
alice = Fabricate(:user) | ||
session[:user_id] = alice.id | ||
queue_item = Fabricate(:queue_item, user: alice) | ||
delete :destroy, id: queue_item.id | ||
expect(QueueItem.count).to eq(0) | ||
end | ||
it "does not delete the queue item if the queue item is not in the current user's queue" do | ||
alice = Fabricate(:user) | ||
bob = Fabricate(:user) | ||
session[:user_id] = alice.id | ||
queue_item = Fabricate(:queue_item, user: bob) | ||
delete :destroy, id: queue_item.id | ||
expect(QueueItem.count).to eq(1) | ||
end | ||
it "redirects to the sign in page for unauthenticated users" do | ||
delete :destroy, id: 3 | ||
expect(response).to redirect_to sign_in_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fabricator(:category) do | ||
name { Faker::Lorem.words(1) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fabricator(:queue_item) do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'spec_helper' | ||
|
||
describe QueueItem do | ||
it { should belong_to(:user) } | ||
it { should belong_to(:video) } | ||
|
||
describe "#video_title" do | ||
it "returns the title of the associated video" do | ||
video = Fabricate(:video, title: 'Monk') | ||
queue_item = Fabricate(:queue_item, video: video) | ||
expect(queue_item.video_title).to eq('Monk') | ||
end | ||
|
||
describe "#rating" do | ||
it "returns the rating from the review when the review is present" do | ||
video = Fabricate(:video) | ||
user = Fabricate(:user) | ||
review = Fabricate(:review, user: user, video: video, rating: 4) | ||
queue_item = Fabricate(:queue_item, user: user, video: video) | ||
expect(queue_item.rating).to eq(4) | ||
end | ||
it "returns nil when the review is not present" do | ||
video = Fabricate(:video) | ||
user = Fabricate(:user) | ||
queue_item = Fabricate(:queue_item, user: user, video: video) | ||
expect(queue_item.rating).to eq(nil) | ||
end | ||
end | ||
|
||
describe "#category_name" do | ||
it "returns the category name of the video" do | ||
category = Fabricate(:category, name: "comedies") | ||
video = Fabricate(:video, category: category) | ||
queue_item = Fabricate(:queue_item, video: video) | ||
expect(queue_item.category_name).to eq("comedies") | ||
end | ||
end | ||
|
||
describe "#category" do | ||
it "returns the category of the video" do | ||
category = Fabricate(:category, name: "comedies") | ||
video = Fabricate(:video, category: category) | ||
queue_item = Fabricate(:queue_item, video: video) | ||
expect(queue_item.category).to eq(category) | ||
end | ||
end | ||
end | ||
end |