Skip to content

Commit

Permalink
Merge pull request #4 from Jameskcoleman/week_2_part_2
Browse files Browse the repository at this point in the history
Week 2 part 2
  • Loading branch information
Jameskcoleman committed Jul 7, 2014
2 parents c578c38 + 394f9c7 commit 7b8e61d
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 7 deletions.
33 changes: 33 additions & 0 deletions app/controllers/queue_items_controller.rb
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
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Category < ActiveRecord::Base
has_many :videos, -> { order "created_at DESC" }
validates_presence_of :name

def recent_videos
videos.first(6)
Expand Down
17 changes: 17 additions & 0 deletions app/models/queue_item.rb
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
7 changes: 2 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ class User < ActiveRecord::Base
validates_uniqueness_of :email
has_many :reviews

# validates :email, presence: true, uniqueness: true
# validates :password, presence: true, :length => ( :minimum => 4), on: :create
# validates :password, allow_blank: true, :length => ( :minimum => 4), on: :update
# validates :full_name, presence: true

has_secure_password validations: false

has_many :queue_items
end
30 changes: 30 additions & 0 deletions app/views/queue_items/index.html.haml
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"
3 changes: 2 additions & 1 deletion app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
%h3= @video.title
%span Rating: 4.5/5.0
%p= @video.description

.actions
= link_to "+ My Queue", queue_items_path(video_id: @video.id), method: :post, class: 'btn'
%section.reviews.container
.row
.col-sm-10.col-sm-offset-1
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
resources :reviews, only: [:create]
end
resources :categories
resources :queue_items, only: [:create, :destroy]

get 'my_queue', to: 'queue_items#index'

get 'ui(/:action)', controller: 'ui'
get 'register', to: "users#new"
get 'sign_in', to: 'sessions#new'
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20140707101635_create_queue_items.rb
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
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140705143740) do
ActiveRecord::Schema.define(version: 20140707101635) do

create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "queue_items", force: true do |t|
t.integer "video_id"
t.integer "user_id"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "reviews", force: true do |t|
t.integer "user_id"
t.integer "video_id"
Expand Down
Empty file.
97 changes: 97 additions & 0 deletions spec/controllers/queue_items_controller_spec.rb
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
3 changes: 3 additions & 0 deletions spec/fabricators/category_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fabricator(:category) do
name { Faker::Lorem.words(1) }
end
3 changes: 3 additions & 0 deletions spec/fabricators/queue_item_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fabricator(:queue_item) do

end
1 change: 1 addition & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe Category do
it { should have_many(:videos) }
it { should validate_presence_of(:name) }

describe "#recent_videos" do
it "returns the videos in reverse chronological order by created_at" do
Expand Down
48 changes: 48 additions & 0 deletions spec/models/queue_item_spec.rb
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

0 comments on commit 7b8e61d

Please sign in to comment.