From 663c04ffae8a9f8cb0533015b8f71c57e45dcf04 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Mon, 19 May 2014 13:49:00 -0500 Subject: [PATCH 01/37] video and categories --- .DS_Store | Bin 0 -> 6148 bytes app/controllers/categories_controller.rb | 10 ++++++ app/controllers/videos_controller.rb | 8 +++++ app/models/category.rb | 4 +++ app/models/video.rb | 4 +++ app/views/categories/show.html.haml | 0 app/views/videos/index.html.haml | 18 ++++++++++ config/routes.rb | 5 +++ db/migrate/20140519141833_create_videos.rb | 11 +++++++ .../20140519181338_create_categories.rb | 8 +++++ db/schema.rb | 31 ++++++++++++++++++ db/seeds.rb | 9 +++++ 12 files changed, 108 insertions(+) create mode 100644 .DS_Store create mode 100644 app/controllers/categories_controller.rb create mode 100644 app/controllers/videos_controller.rb create mode 100644 app/models/category.rb create mode 100644 app/models/video.rb create mode 100644 app/views/categories/show.html.haml create mode 100644 app/views/videos/index.html.haml create mode 100644 db/migrate/20140519141833_create_videos.rb create mode 100644 db/migrate/20140519181338_create_categories.rb create mode 100644 db/schema.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..15b0d7461a3a0b04c43a9e9ca9aba1fc61f0b5d1 GIT binary patch literal 6148 zcmeH~Jr2S!425mzfW*>~F$)La1`&c2Z~+7zkvbsu9G&MM1`0E((6eNJu^X%H8=6`~ zbpIH3BE5*L;6~Y6n3y78$RL;De!X4B@n(gzMbb*}UMAOH`$#H41*iZOpaN82J__Vf zzFN=ck@zH3fC|h+0sB4_xUnW%&_5jrJ_3Lp%5GTuECCiPfHm2IsK7K@!Dv+^4`w>BmCR^KWSk~1*pKEDWKiq zblBsi@@)NhJ*%IyYU>6EeLKS2PXH1-iZ^gKTrai&YqAAVf$>McWniEJKULrfWLgj- literal 0 HcmV?d00001 diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb new file mode 100644 index 000000000..d23ac0167 --- /dev/null +++ b/app/controllers/categories_controller.rb @@ -0,0 +1,10 @@ +class CategoriesController < ApplicationController + + def index + @categories = Category.all + end + + def show + @category = Category.find(params[:id]) + + end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb new file mode 100644 index 000000000..f77e875bb --- /dev/null +++ b/app/controllers/videos_controller.rb @@ -0,0 +1,8 @@ +class VideosController < ApplicationController + + def index + @videos = Video.all + + end + +end \ No newline at end of file diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 000000000..ee5d1d612 --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,4 @@ +class Category < ActiveRecord::Base + has_many :videos + +end diff --git a/app/models/video.rb b/app/models/video.rb new file mode 100644 index 000000000..f824c566d --- /dev/null +++ b/app/models/video.rb @@ -0,0 +1,4 @@ +class Video < ActiveRecord::Base + belongs_to :category + +end \ No newline at end of file diff --git a/app/views/categories/show.html.haml b/app/views/categories/show.html.haml new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml new file mode 100644 index 000000000..ac61bf16e --- /dev/null +++ b/app/views/videos/index.html.haml @@ -0,0 +1,18 @@ +- @categories.each do |category| + %header + %h3= link_to "TV Commedies" + +.videos.row + - @videos.each do |video| + .@videos.col-sm-2 + %img(src="#{video.small_cover_url}") + + +%article.video_category + %header + %h3= link_to "TV Commedies" + .videos.row + - 6.times do + .video.col-sm-2 + = link_to "", nil do + %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 95cc741d1..76af32aca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,8 @@ Myflix::Application.routes.draw do get 'ui(/:action)', controller: 'ui' + + get '/home', to: 'videos#index' + + resources :videos, only: [:index, :show] + resources :categories, only: [:show] end diff --git a/db/migrate/20140519141833_create_videos.rb b/db/migrate/20140519141833_create_videos.rb new file mode 100644 index 000000000..dc2e76394 --- /dev/null +++ b/db/migrate/20140519141833_create_videos.rb @@ -0,0 +1,11 @@ +class CreateVideos < ActiveRecord::Migration + def change + create_table :videos do |t| + t.string :title + t.text :description + t.string :small_cover_url + t.string :large_cover_url + t.timestamps + end + end +end diff --git a/db/migrate/20140519181338_create_categories.rb b/db/migrate/20140519181338_create_categories.rb new file mode 100644 index 000000000..f4b307a06 --- /dev/null +++ b/db/migrate/20140519181338_create_categories.rb @@ -0,0 +1,8 @@ +class CreateCategories < ActiveRecord::Migration + def change + create_table :categories do |t| + t.string :name + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..995f11178 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,31 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20140519181338) do + + create_table "categories", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "videos", force: true do |t| + t.string "title" + t.text "description" + t.string "small_cover_url" + t.string "large_cover_url" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e857..04ec5b02f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,12 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +Video.create(title: "Futurama", description: "Pizza boy Philip J. Fry awakens in the 31st century after 1,000 years of cryogenic preservation in this animated series.", small_cover_url: "/tmp/futurama.jpg", large_cover_url: "/tmp/futurama.jpg") +Video.create(title: "Family Guy", description: "Story about a family", small_cover_url: "/tmp/family_guy.jpg", large_cover_url: "/tmp/family_guy") +Video.create(title: "South Park", description: "Kids in a park", small_cover_url: "/tmp/south_park.jpg", large_cover_url: "/tmp/south_park.jpg") +Video.create(title: "Monk", description: "Story about a monk detetctive", small_cover_url: "/tmp/monk.jpg", large_cover_url: "/tmp/monk_large") + +Category.create(name: "Comedy") +Category.create(name: "Drama") +Category.create(name: "Reality") \ No newline at end of file From c1b90578261fb4c8baf4608feaed0a7cdeb56ddb Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Mon, 19 May 2014 14:59:02 -0500 Subject: [PATCH 02/37] categoryid in video --- db/migrate/20140519190153_add_categoryid_to_videos.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20140519190153_add_categoryid_to_videos.rb diff --git a/db/migrate/20140519190153_add_categoryid_to_videos.rb b/db/migrate/20140519190153_add_categoryid_to_videos.rb new file mode 100644 index 000000000..ffa226819 --- /dev/null +++ b/db/migrate/20140519190153_add_categoryid_to_videos.rb @@ -0,0 +1,5 @@ +class AddCategoryidToVideos < ActiveRecord::Migration + def change + add_column :videos, :category_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 995f11178..807eb90b2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140519181338) do +ActiveRecord::Schema.define(version: 20140519190153) do create_table "categories", force: true do |t| t.string "name" @@ -26,6 +26,7 @@ t.string "large_cover_url" t.datetime "created_at" t.datetime "updated_at" + t.integer "category_id" end end From 7b4e04c72b5a9f062515ef927ed1a14126927e6b Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Mon, 19 May 2014 15:11:30 -0500 Subject: [PATCH 03/37] updated index --- app/views/videos/index.html.haml | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index ac61bf16e..74d3cfad9 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,18 +1,9 @@ - @categories.each do |category| - %header - %h3= link_to "TV Commedies" - -.videos.row - - @videos.each do |video| - .@videos.col-sm-2 - %img(src="#{video.small_cover_url}") - - -%article.video_category - %header - %h3= link_to "TV Commedies" - .videos.row - - 6.times do - .video.col-sm-2 - = link_to "", nil do - %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file + %article.video_category + %header + %h3= link_to "TV #{category}" + .videos.row + - @videos.each do |video| + .video.col-sm-2 + =link_to "", nil do + %img(src="#{video.small_cover_url}") From bb86fb5f555e69075b33e5535c7bce5e7b95c0b8 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Mon, 19 May 2014 15:14:22 -0500 Subject: [PATCH 04/37] updated index --- app/views/videos/index.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index 74d3cfad9..770a47a5e 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,7 +1,7 @@ - @categories.each do |category| %article.video_category %header - %h3= link_to "TV #{category}" + %h3= link_to "TV #{category.name}" .videos.row - @videos.each do |video| .video.col-sm-2 From e3a2416e2f942f1d120bf7986740c307a7fefff2 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 10:02:46 -0500 Subject: [PATCH 05/37] fixed end --- app/controllers/categories_controller.rb | 7 ++++--- app/controllers/videos_controller.rb | 3 +++ app/views/categories/show.html.haml | 14 ++++++++++++++ app/views/videos/index.html.haml | 10 ++-------- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index d23ac0167..8681f67e2 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -5,6 +5,7 @@ def index end def show - @category = Category.find(params[:id]) - - end \ No newline at end of file + #@category = Category.find(params[:id]) + @categories = Category.all + end +end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index f77e875bb..76c5e4b76 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -5,4 +5,7 @@ def index end + def show + @video = Video.find(params[:id]) + end end \ No newline at end of file diff --git a/app/views/categories/show.html.haml b/app/views/categories/show.html.haml index e69de29bb..bc959bff7 100644 --- a/app/views/categories/show.html.haml +++ b/app/views/categories/show.html.haml @@ -0,0 +1,14 @@ +%section.genre + %header.clearfix + %h1 Comedies + .genre_sorting + Sort by: + %select(name="") + %option(value="a-z") Title: A - Z + %option(value="z-a") Title: Z - A + %option(value="rating") Rating + %article.row + - 30.times do + .video.col-md-2 + = link_to "", nil do + %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index 770a47a5e..db73d8ac8 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,9 +1,3 @@ - @categories.each do |category| - %article.video_category - %header - %h3= link_to "TV #{category.name}" - .videos.row - - @videos.each do |video| - .video.col-sm-2 - =link_to "", nil do - %img(src="#{video.small_cover_url}") + %tr + %td = category.title \ No newline at end of file From 7156438cd5bb657c329d8f988da4592381f9e129 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 10:12:54 -0500 Subject: [PATCH 06/37] added categories to videos --- app/controllers/categories_controller.rb | 4 ++-- app/controllers/videos_controller.rb | 2 +- app/views/videos/index.html.haml | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 8681f67e2..6f01f7e95 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -5,7 +5,7 @@ def index end def show - #@category = Category.find(params[:id]) - @categories = Category.all + @category = Category.find(params[:id]) + #@categories = Category.all end end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index 76c5e4b76..bf11fe9a8 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -2,7 +2,7 @@ class VideosController < ApplicationController def index @videos = Video.all - + @categories = Category.all end def show diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index db73d8ac8..86f6e75ee 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,3 +1,8 @@ -- @categories.each do |category| - %tr - %td = category.title \ No newline at end of file +%article.video_category + %header + %h3= link_to "TV Commedies" + .videos.row + - 6.times do + .video.col-sm-2 + = link_to "", nil do + %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file From 6fc5daccff926e40d2a53b088b7b19ef983fc4f7 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 10:32:58 -0500 Subject: [PATCH 07/37] stuff spitting out --- app/views/videos/index.html.haml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index 86f6e75ee..b060e68aa 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,8 +1,9 @@ -%article.video_category - %header - %h3= link_to "TV Commedies" +- @categories.each do |category| + %article.video_category + %header + %h3= link_to "TV #{category.name}" .videos.row - - 6.times do + - @videos.each do |video| .video.col-sm-2 = link_to "", nil do - %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file + %img(src="#{video.small_cover_url}") \ No newline at end of file From 4f2067f46b18e82a559a7860f6dbafe9bba9ea51 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 15:29:30 -0500 Subject: [PATCH 08/37] iterating --- app/controllers/videos_controller.rb | 1 + app/views/videos/index.html.haml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index bf11fe9a8..34cc11203 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -3,6 +3,7 @@ class VideosController < ApplicationController def index @videos = Video.all @categories = Category.all + @video = Video.find(params[:id]) end def show diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index b060e68aa..191880fb5 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -3,7 +3,7 @@ %header %h3= link_to "TV #{category.name}" .videos.row - - @videos.each do |video| + - @videos.category_id(category.id).each do |video| .video.col-sm-2 = link_to "", nil do %img(src="#{video.small_cover_url}") \ No newline at end of file From a3feeb5af0ab460df09978991e88cdc21d8f7ad8 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 20:58:41 -0500 Subject: [PATCH 09/37] working --- Gemfile | 1 + Gemfile.lock | 14 ++++++++++++++ app/controllers/categories_controller.rb | 2 +- app/controllers/videos_controller.rb | 2 +- app/views/categories/show.html.haml | 8 ++++---- app/views/videos/index.html.haml | 6 +++--- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 6d829a5c8..23ebf7c8e 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ group :development do gem 'thin' gem "better_errors" gem "binding_of_caller" + gem 'rspec-rails' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 219b62049..9f2c17fdf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,6 +45,7 @@ GEM coffee-script-source (1.6.3) daemons (1.1.9) debug_inspector (0.0.2) + diff-lcs (1.2.5) erubis (2.7.0) eventmachine (1.0.0) execjs (2.0.2) @@ -98,6 +99,18 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (10.1.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + rspec-rails (2.14.2) + actionpack (>= 3.0) + activemodel (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) sass (3.2.12) sass-rails (4.0.1) railties (>= 4.0.0, < 5.0) @@ -145,6 +158,7 @@ DEPENDENCIES pry-nav rails rails_12factor + rspec-rails sass-rails sqlite3 thin diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 6f01f7e95..775691f55 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -6,6 +6,6 @@ def index def show @category = Category.find(params[:id]) - #@categories = Category.all + end end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index 34cc11203..263e21214 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -3,7 +3,7 @@ class VideosController < ApplicationController def index @videos = Video.all @categories = Category.all - @video = Video.find(params[:id]) + end def show diff --git a/app/views/categories/show.html.haml b/app/views/categories/show.html.haml index bc959bff7..588e7b20d 100644 --- a/app/views/categories/show.html.haml +++ b/app/views/categories/show.html.haml @@ -1,6 +1,6 @@ %section.genre %header.clearfix - %h1 Comedies + %h1= @category.name .genre_sorting Sort by: %select(name="") @@ -8,7 +8,7 @@ %option(value="z-a") Title: Z - A %option(value="rating") Rating %article.row - - 30.times do + - @category.videos.each do |video| .video.col-md-2 - = link_to "", nil do - %img(src="/tmp/#{['futurama', 'south_park', 'family_guy'].sample}.jpg") \ No newline at end of file + = link_to video_path(video) do + %img(src="#{video.small_cover_url}") \ No newline at end of file diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index 191880fb5..6527362c1 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,9 +1,9 @@ - @categories.each do |category| %article.video_category %header - %h3= link_to "TV #{category.name}" + %h3= link_to "TV #{category.name}", category_path(category) .videos.row - - @videos.category_id(category.id).each do |video| + - category.videos.each do |video| .video.col-sm-2 - = link_to "", nil do + = link_to video_path(video) do %img(src="#{video.small_cover_url}") \ No newline at end of file From 2cf7d7ce574c9e2cef503dff08d0abdfdda5ddde Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 20 May 2014 20:59:01 -0500 Subject: [PATCH 10/37] working --- .rspec | 1 + app/views/videos/show.html.haml | 13 ++++++++++ spec/spec_helper.rb | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 .rspec create mode 100644 app/views/videos/show.html.haml create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 000000000..4e1e0d2f7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color diff --git a/app/views/videos/show.html.haml b/app/views/videos/show.html.haml new file mode 100644 index 000000000..2dd817038 --- /dev/null +++ b/app/views/videos/show.html.haml @@ -0,0 +1,13 @@ +%article.video + .container + .row + .video_large_cover.col-sm-7.col-sm-offset-1 + %img(src="#{@video.large_cover_url}") + .video_info.col-sm-3 + %header + %h3= @video.title + %span Rating: 4.5/5.0 + %p= @video.description + .actions + %a.btn.btn-primary(href="") Watch Now + %a.btn.btn-default(href="") + My Queue \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000..943bc1919 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,42 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV["RAILS_ENV"] ||= 'test' +require File.expand_path("../../config/environment", __FILE__) +require 'rspec/rails' +require 'rspec/autorun' + +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } + +# Checks for pending migrations before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) + +RSpec.configure do |config| + # ## Mock Framework + # + # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: + # + # config.mock_with :mocha + # config.mock_with :flexmock + # config.mock_with :rr + + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # If true, the base class of anonymous controllers will be inferred + # automatically. This will be the default behavior in future versions of + # rspec-rails. + config.infer_base_class_for_anonymous_controllers = false + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = "random" +end From 0ecfe433b30d81b5eb8697f0319623dd64f5cf75 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 21 May 2014 16:51:15 -0500 Subject: [PATCH 11/37] test --- app/models/video.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/video.rb b/app/models/video.rb index f824c566d..c1c3f12ba 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,4 +1,5 @@ class Video < ActiveRecord::Base belongs_to :category + #test end \ No newline at end of file From 66a0dfcc10d4993793577ab9f995d647db33e729 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sat, 24 May 2014 12:08:48 -0500 Subject: [PATCH 12/37] tested video model for save --- Gemfile | 4 ++++ app/models/video.rb | 3 +-- app/views/videos/show.html.haml | 3 ++- db/seeds.rb | 3 ++- spec/models/video_spec.rb | 10 ++++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 spec/models/video_spec.rb diff --git a/Gemfile b/Gemfile index 23ebf7c8e..77e5b0437 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,10 @@ group :development do gem 'thin' gem "better_errors" gem "binding_of_caller" + +end + +group :test, :development do gem 'rspec-rails' end diff --git a/app/models/video.rb b/app/models/video.rb index c1c3f12ba..0d5035ba0 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,5 +1,4 @@ class Video < ActiveRecord::Base belongs_to :category - #test - + end \ No newline at end of file diff --git a/app/views/videos/show.html.haml b/app/views/videos/show.html.haml index 2dd817038..431bbb611 100644 --- a/app/views/videos/show.html.haml +++ b/app/views/videos/show.html.haml @@ -10,4 +10,5 @@ %p= @video.description .actions %a.btn.btn-primary(href="") Watch Now - %a.btn.btn-default(href="") + My Queue \ No newline at end of file + %a.btn.btn-default(href="") + My Queue + \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index 04ec5b02f..dcf89fd9b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,4 +13,5 @@ Category.create(name: "Comedy") Category.create(name: "Drama") -Category.create(name: "Reality") \ No newline at end of file +Category.create(name: "Reality") + diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb new file mode 100644 index 000000000..d46bb74da --- /dev/null +++ b/spec/models/video_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +#tests saving to the test database +describe Video do + it "saves itself" do + video = Video.new(title: "monk", description: "a great video!") + video.save + expect(Video.first).to eq(video) + end +end \ No newline at end of file From 9d750222aca838dfae71f0495139f202a2a7f157 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sat, 24 May 2014 12:45:50 -0500 Subject: [PATCH 13/37] tested validations --- app/models/category.rb | 3 +-- app/models/video.rb | 4 +++- spec/models/category_spec.rb | 16 ++++++++++++++++ spec/models/video_spec.rb | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 spec/models/category_spec.rb diff --git a/app/models/category.rb b/app/models/category.rb index ee5d1d612..8adc0bcb3 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,4 +1,3 @@ class Category < ActiveRecord::Base - has_many :videos - + has_many :videos, order: :title end diff --git a/app/models/video.rb b/app/models/video.rb index 0d5035ba0..43f28a5e1 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,4 +1,6 @@ class Video < ActiveRecord::Base belongs_to :category - + #validates :title, presence: true + #validates :description, presence: true + validates_presence_of :title, :description end \ No newline at end of file diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 000000000..ab0f52965 --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Category do + it "saves itself" do + category = Category.new(name: "comedies") + category.save + expect(Category.first).to eq(category) + end + + it "has many videos" do + comedies = Category.create(name: "comedies") + south_park = Video.create(title: "South Park", description: "Funny kids", category: comedies) + futurama = Video.create(title: "Futurama", description: "space travel", category: comedies) + expect(comedies.videos).to eq([futurama, south_park]) + end +end diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb index d46bb74da..118974146 100644 --- a/spec/models/video_spec.rb +++ b/spec/models/video_spec.rb @@ -7,4 +7,19 @@ video.save expect(Video.first).to eq(video) end + + it "belongs to category" do + dramas = Category.create(name: "dramas") + monk = Video.create(title: "monk", description: "a great video", category: dramas) + expect(monk.category).to eq(dramas) + end + + it "does not save a video without a title" do + video = Video.create(description: "a great video!") + expect(Video.count).to eq(0) + end + it "does not save a video without a description" do + video = Video.create(title: "monk") + expect(Video.count).to eq(0) + end end \ No newline at end of file From 40ee716e61590d175c348bb3a64a79a54a5bd07f Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sat, 24 May 2014 13:01:15 -0500 Subject: [PATCH 14/37] upated with shoulda matchers --- Gemfile | 5 +++++ Gemfile.lock | 3 +++ spec/models/category_spec.rb | 10 ++++------ spec/models/video_spec.rb | 13 ++++++++++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 77e5b0437..eea1fe7ea 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,11 @@ group :test, :development do gem 'rspec-rails' end +group :test do + gem 'shoulda-matchers' + +end + group :production do gem 'pg' gem 'rails_12factor' diff --git a/Gemfile.lock b/Gemfile.lock index 9f2c17fdf..82a6637fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,6 +116,8 @@ GEM railties (>= 4.0.0, < 5.0) sass (>= 3.1.10) sprockets-rails (~> 2.0.0) + shoulda-matchers (2.6.1) + activesupport (>= 3.0.0) slop (3.4.7) sprockets (2.10.0) hike (~> 1.2) @@ -160,6 +162,7 @@ DEPENDENCIES rails_12factor rspec-rails sass-rails + shoulda-matchers sqlite3 thin uglifier diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index ab0f52965..b1c2a8a45 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -1,16 +1,14 @@ require 'spec_helper' describe Category do - it "saves itself" do - category = Category.new(name: "comedies") - category.save - expect(Category.first).to eq(category) - end - + it { should have_many(:videos)} +=begin +replaced with shoulda matcher it "has many videos" do comedies = Category.create(name: "comedies") south_park = Video.create(title: "South Park", description: "Funny kids", category: comedies) futurama = Video.create(title: "Futurama", description: "space travel", category: comedies) expect(comedies.videos).to eq([futurama, south_park]) end +=end end diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb index 118974146..f96cc8667 100644 --- a/spec/models/video_spec.rb +++ b/spec/models/video_spec.rb @@ -2,18 +2,28 @@ #tests saving to the test database describe Video do +=begin +#this was just for practice it "saves itself" do video = Video.new(title: "monk", description: "a great video!") video.save expect(Video.first).to eq(video) end +=end + it { should belong_to(:category)} +=begin +replaced with shoulda matchers it "belongs to category" do dramas = Category.create(name: "dramas") monk = Video.create(title: "monk", description: "a great video", category: dramas) expect(monk.category).to eq(dramas) end - +=end + it { should validate_presence_of(:title)} + it { should validate_presence_of(:description)} +=begin +replaced with shoulda matchers it "does not save a video without a title" do video = Video.create(description: "a great video!") expect(Video.count).to eq(0) @@ -22,4 +32,5 @@ video = Video.create(title: "monk") expect(Video.count).to eq(0) end +=end end \ No newline at end of file From f611bf790ebd52b4a84a6f8761d5d3519e2c231d Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sun, 25 May 2014 15:58:20 -0500 Subject: [PATCH 15/37] added search --- app/controllers/videos_controller.rb | 4 ++ app/models/video.rb | 7 +++- app/views/shared/_header.html.haml | 7 ++-- config/routes.rb | 6 ++- spec/models/video_spec.rb | 56 ++++++++++++++-------------- 5 files changed, 46 insertions(+), 34 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index 263e21214..a948b7a3a 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -9,4 +9,8 @@ def index def show @video = Video.find(params[:id]) end + + def search + @results = Video.search_by_title(params[:search_term]) + end end \ No newline at end of file diff --git a/app/models/video.rb b/app/models/video.rb index 43f28a5e1..fb40d94c7 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -2,5 +2,10 @@ class Video < ActiveRecord::Base belongs_to :category #validates :title, presence: true #validates :description, presence: true - validates_presence_of :title, :description + validates_presence_of :title, :description + + def self.search_by_title(search_term) + return [] if search_term.blank? + where("title LIKE ?", "%#{search_term}%").order("created_at DESC") + end end \ No newline at end of file diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 21fbdacfe..98819fc77 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -5,10 +5,9 @@ %li= link_to "Videos" %li= link_to "My Queue" %li= link_to "People" - %form.col-md-5.navbar-form(action="") - .form-group - %input.form-control(type="text" placeholder="Search for videos here") - %button.btn.btn-default(type="submit") Search + = form_tag search_videos_path, class: "span5 for-search" do + %input.search-query(name="search_term" type="text" placeholder="Search for videos here") + %button.btn(type="submit") Search #user_links.pull-right %ul %li.dropdown diff --git a/config/routes.rb b/config/routes.rb index 76af32aca..3757465c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,10 @@ get '/home', to: 'videos#index' - resources :videos, only: [:index, :show] + resources :videos, only: [:index, :show] do + collection do + post :search, to: "videos#search" + end + end resources :categories, only: [:show] end diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb index f96cc8667..cd07f59a8 100644 --- a/spec/models/video_spec.rb +++ b/spec/models/video_spec.rb @@ -1,36 +1,36 @@ require 'spec_helper' -#tests saving to the test database -describe Video do -=begin -#this was just for practice - it "saves itself" do - video = Video.new(title: "monk", description: "a great video!") - video.save - expect(Video.first).to eq(video) - end -=end +describe Video do it { should belong_to(:category)} -=begin -replaced with shoulda matchers - it "belongs to category" do - dramas = Category.create(name: "dramas") - monk = Video.create(title: "monk", description: "a great video", category: dramas) - expect(monk.category).to eq(dramas) - end -=end it { should validate_presence_of(:title)} it { should validate_presence_of(:description)} -=begin -replaced with shoulda matchers - it "does not save a video without a title" do - video = Video.create(description: "a great video!") - expect(Video.count).to eq(0) - end - it "does not save a video without a description" do - video = Video.create(title: "monk") - expect(Video.count).to eq(0) + + describe "search_by_title" do + it "returns an empty array if there is no match" do + futurama = Video.create(title: "Futurama", description: "Space Travel!") + back_to_future = Video.create(title: "Back to Future", description: "Time Travel!") + expect(Video.search_by_title("hello")).to eq([]) + end + it "returns an array of one video for an exact match" do + futurama = Video.create(title: "Futurama", description: "Space Travel!") + back_to_future = Video.create(title: "Back to Future", description: "Time Travel!") + expect(Video.search_by_title("Futurama")).to eq([futurama]) + end + it "returns an array of one video for a partial match" do + futurama = Video.create(title: "Futurama", description: "Space Travel!") + back_to_future = Video.create(title: "Back to Future", description: "Time Travel!") + expect(Video.search_by_title("urama")).to eq([futurama]) + end + it "returns an array of all matches ordered by created_at" do + futurama = Video.create(title: "Futurama", description: "Space Travel!", created_at: 1.day.ago) + back_to_future = Video.create(title: "Back to Future", description: "Time Travel!") + expect(Video.search_by_title("Futur")).to eq([back_to_future, futurama]) + end + it "returns an empty array for a search with an empty string" do + futurama = Video.create(title: "Futurama", description: "Space Travel!", created_at: 1.day.ago) + back_to_future = Video.create(title: "Back to Future", description: "Time Travel!") + expect(Video.search_by_title("")).to eq([]) + end end -=end end \ No newline at end of file From e7bd746ba228fedfbf38d05bee467c0c460b3ff4 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sun, 25 May 2014 15:58:40 -0500 Subject: [PATCH 16/37] added search --- app/views/videos/search.html.haml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 app/views/videos/search.html.haml diff --git a/app/views/videos/search.html.haml b/app/views/videos/search.html.haml new file mode 100644 index 000000000..ba2ef2eb1 --- /dev/null +++ b/app/views/videos/search.html.haml @@ -0,0 +1,5 @@ +%article.video_category + - @results.each do |video| + .video.span2 + = link_to video do + %img(src="#{video.small_cover_url}") \ No newline at end of file From ca615b34788f5ca4782db7dcdae8160bd480bb7e Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sun, 25 May 2014 17:43:03 -0500 Subject: [PATCH 17/37] recent videos --- app/models/category.rb | 6 +++++- spec/models/category_spec.rb | 37 ++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 8adc0bcb3..46fdec51d 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,3 +1,7 @@ class Category < ActiveRecord::Base - has_many :videos, order: :title + has_many :videos, order: "created_at DESC" + + def recent_videos + videos.first(6) + end end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index b1c2a8a45..cfc6d707f 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -2,13 +2,34 @@ describe Category do it { should have_many(:videos)} -=begin -replaced with shoulda matcher - it "has many videos" do - comedies = Category.create(name: "comedies") - south_park = Video.create(title: "South Park", description: "Funny kids", category: comedies) - futurama = Video.create(title: "Futurama", description: "space travel", category: comedies) - expect(comedies.videos).to eq([futurama, south_park]) + + describe "#recent_videos" do + it "returns the videos in the reverse chronical order by created at" do + comedies = Category.create(name: "comedies") + futurama = Video.create(title: "Futurama", description: "space travel", category: comedies, created_at: 1.day.ago) + south_park = Video.create(title: "South Park", description: "crazy kids!", category: comedies) + expect(comedies.recent_videos).to eq([south_park, futurama]) + end + it "returns all the videos if there are less than 6 videos" do + comedies = Category.create(name: "comedies") + futurama = Video.create(title: "Futurama", description: "space travel", category: comedies, created_at: 1.day.ago) + south_park = Video.create(title: "South Park", description: "crazy kids!", category: comedies) + expect(comedies.recent_videos.count).to eq(2) + end + it "returns 6 videos if there are more than 6 videos" do + comedies = Category.create(name: "comedies") + 7.times { Video.create(title: "foo", description: "foobar", category: comedies)} + expect(comedies.recent_videos.count).to eq(6) + end + it "returns the most recent 6 videos" do + comedies = Category.create(name: "comedies") + 6.times { Video.create(title: "foo", description: "foobar", category: comedies)} + tonights_show = Video.create(title: "Tonights show", description: "talk show", category: comedies, created_at: 1.day.ago) + expect(comedies.recent_videos).not_to include(tonights_show) + end + it "returns an empty array if the category does not have any videos" do + comedies = Category.create(name: "comedies") + expect(comedies.recent_videos).to eq([]) + end end -=end end From fe6bcbc4fdf35738cc5aa2ed4e9ed3077b25428b Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Sun, 25 May 2014 18:19:12 -0500 Subject: [PATCH 18/37] last update --- app/views/shared/_header.html.haml | 2 +- app/views/videos/index.html.haml | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 98819fc77..0189c171f 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -5,7 +5,7 @@ %li= link_to "Videos" %li= link_to "My Queue" %li= link_to "People" - = form_tag search_videos_path, class: "span5 for-search" do + = form_tag search_videos_path, class: "span5 for-search" do %input.search-query(name="search_term" type="text" placeholder="Search for videos here") %button.btn(type="submit") Search #user_links.pull-right diff --git a/app/views/videos/index.html.haml b/app/views/videos/index.html.haml index 6527362c1..acf849a8e 100644 --- a/app/views/videos/index.html.haml +++ b/app/views/videos/index.html.haml @@ -1,9 +1,9 @@ -- @categories.each do |category| - %article.video_category +%article.video_category + - @categories.each do |category| %header - %h3= link_to "TV #{category.name}", category_path(category) - .videos.row - - category.videos.each do |video| - .video.col-sm-2 - = link_to video_path(video) do - %img(src="#{video.small_cover_url}") \ No newline at end of file + %h3= category.name + .videos.row + - category.recent_videos.each do |video| + .video.col-sm-2 + = link_to video do + %img(src="#{video.small_cover_url}") \ No newline at end of file From 099d6ffaaea9d3ebc096230a3b862a83b36d3ff0 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 27 May 2014 20:45:20 -0500 Subject: [PATCH 19/37] register not working --- Gemfile | 2 ++ Gemfile.lock | 6 ++++++ app/controllers/pages_controller.rb | 2 ++ app/controllers/users_controller.rb | 14 ++++++++++++++ app/models/user.rb | 6 ++++++ app/views/pages/front.html.haml | 8 ++++++++ app/views/users/new.html.haml | 13 +++++++++++++ config/routes.rb | 8 ++++++-- db/migrate/20140528010226_create_users.rb | 10 ++++++++++ db/schema.rb | 10 +++++++++- 10 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 app/controllers/pages_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/models/user.rb create mode 100644 app/views/pages/front.html.haml create mode 100644 app/views/users/new.html.haml create mode 100644 db/migrate/20140528010226_create_users.rb diff --git a/Gemfile b/Gemfile index eea1fe7ea..a5eb89b1e 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,8 @@ gem 'haml-rails' gem 'sass-rails' gem 'uglifier' gem 'jquery-rails' +gem 'bootstrap_form' +gem 'bcrypt-ruby' group :development do gem 'sqlite3' diff --git a/Gemfile.lock b/Gemfile.lock index 82a6637fe..24449d5ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,6 +27,9 @@ GEM tzinfo (~> 0.3.37) arel (4.0.1) atomic (1.1.14) + bcrypt (3.1.7) + bcrypt-ruby (3.1.5) + bcrypt (>= 3.1.3) better_errors (1.0.1) coderay (>= 1.0.0) erubis (>= 2.6.6) @@ -34,6 +37,7 @@ GEM debug_inspector (>= 0.0.1) bootstrap-sass (3.0.2.1) sass (~> 3.2) + bootstrap_form (2.1.1) builder (3.1.4) coderay (1.1.0) coffee-rails (4.0.1) @@ -149,9 +153,11 @@ PLATFORMS ruby DEPENDENCIES + bcrypt-ruby better_errors binding_of_caller bootstrap-sass + bootstrap_form coffee-rails haml-rails jquery-rails diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb new file mode 100644 index 000000000..95217e118 --- /dev/null +++ b/app/controllers/pages_controller.rb @@ -0,0 +1,2 @@ +class PagesController < ApplicationController +end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 000000000..fbd78602e --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,14 @@ +class UsersController < ApplicationController + def new + @user = User.new + end + + def create + @user = User.new(params[:user]) + if @user.save + redirect_to sign_in_path + else + render :new + end + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 000000000..44169d74d --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ActiveRecord::Base + validates_presence_of :email, :password, :full_name + validates_uniqueness_of :email + + has_secure_password +end \ No newline at end of file diff --git a/app/views/pages/front.html.haml b/app/views/pages/front.html.haml new file mode 100644 index 000000000..ad9d5cb01 --- /dev/null +++ b/app/views/pages/front.html.haml @@ -0,0 +1,8 @@ +%section.site_title + %h1 + Unlimited Movies for Only 9.99 a Month. + = link_to "Sign Up Now!", register_path + %p.sign_up + %p.sign_in + Have an account? Please + = link_to "Sign In" \ No newline at end of file diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 index 000000000..ff632266e --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,13 @@ +%section.register.container + .row + .col-sm-10.col-sm-offset-1 + = bootstrap_form_for @user, html: { class: "form-horizontal" } do |f| + %header + %h1 Register + %fieldset + = f.email_field :email, label: "Email Address" + = f.password_field :password + = f.text_field :full_name, label: "Full Name" + %fieldset.actions.control-group.col-sm-offset-2 + .controls + %input(type="submit" value="Sign Up" class="btn btn-default") diff --git a/config/routes.rb b/config/routes.rb index 3757465c9..a2ce020a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,16 @@ Myflix::Application.routes.draw do - get 'ui(/:action)', controller: 'ui' + root to: "pages#front" - get '/home', to: 'videos#index' resources :videos, only: [:index, :show] do collection do post :search, to: "videos#search" end end + resources :users, only: [:create] resources :categories, only: [:show] + get 'ui(/:action)', controller: 'ui' + get '/home', to: 'videos#index' + get 'register', to: "users#new" + get 'sign_in', to: 'sessions#new' end diff --git a/db/migrate/20140528010226_create_users.rb b/db/migrate/20140528010226_create_users.rb new file mode 100644 index 000000000..e0bf4cfe7 --- /dev/null +++ b/db/migrate/20140528010226_create_users.rb @@ -0,0 +1,10 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :email + t.string :password_digest + t.string :full_name + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 807eb90b2..54b833569 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140519190153) do +ActiveRecord::Schema.define(version: 20140528010226) do create_table "categories", force: true do |t| t.string "name" @@ -19,6 +19,14 @@ t.datetime "updated_at" end + create_table "users", force: true do |t| + t.string "email" + t.string "password_digest" + t.string "full_name" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "videos", force: true do |t| t.string "title" t.text "description" From 4f4cfcaddc3814d51f68826e205ffef7f53d54fa Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 27 May 2014 21:33:21 -0500 Subject: [PATCH 20/37] broken --- app/controllers/application_controller.rb | 8 +++++ app/controllers/pages_controller.rb | 3 ++ app/controllers/sessions_controller.rb | 21 +++++++++++++ app/controllers/videos_controller.rb | 1 + app/models/user.rb | 2 +- app/views/pages/front.html.haml | 2 +- app/views/sessions/new.html.haml | 13 ++++++++ app/views/shared/_header.html.haml | 38 ++++++++++++----------- config/routes.rb | 6 +++- 9 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/views/sessions/new.html.haml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e8065d950..61453c4a0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,11 @@ class ApplicationController < ActionController::Base protect_from_forgery + + def require_user + redirect_to sign_in_path unless current_user + end + + def current_user + User.find(session[:user_id]) if session[:user_id] + end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 95217e118..e2948a656 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,2 +1,5 @@ class PagesController < ApplicationController + def front + redirect_to home_path if current_user + end end \ No newline at end of file diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 000000000..78c4d937b --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +class SessionsController < ApplicationController + def new + redirect_to home_path if current_user + end + + def create + user = User.where(email: params[:email]).first + if user && user.authentication(params[:password]) + session[:user_id] = user.id + redirect_to home_path, notice: 'You are signed in, enjoy' + else + flash[:error] = "Invalid email or password." + redirect_to sign_in_path + end + end + + def destroy + session[:user_id] = nil + redirect_to root_path, notice: "You are signed out." + end +end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index a948b7a3a..f1a10004b 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -1,4 +1,5 @@ class VideosController < ApplicationController + before_filter :require_user def index @videos = Video.all diff --git a/app/models/user.rb b/app/models/user.rb index 44169d74d..80939ee38 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,5 +2,5 @@ class User < ActiveRecord::Base validates_presence_of :email, :password, :full_name validates_uniqueness_of :email - has_secure_password + has_secure_password validations: false end \ No newline at end of file diff --git a/app/views/pages/front.html.haml b/app/views/pages/front.html.haml index ad9d5cb01..a472c5682 100644 --- a/app/views/pages/front.html.haml +++ b/app/views/pages/front.html.haml @@ -5,4 +5,4 @@ %p.sign_up %p.sign_in Have an account? Please - = link_to "Sign In" \ No newline at end of file + = link_to "Sign In", sign_in_path \ No newline at end of file diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml new file mode 100644 index 000000000..4e176f007 --- /dev/null +++ b/app/views/sessions/new.html.haml @@ -0,0 +1,13 @@ +%section.sign_in.container + .row + .col-sm-10.col-sm-offset-1 + = form_tag sessions_path, class: 'sign_in' do + %header + %h1 Sign in + %fieldset + = label_tag :email, "Email Address" + = email_field_tag :email + = label_tag :Password + = password_field_tag :password + %fieldset.form-group.action + %input(type="submit" value="Sign in" class="btn btn-default") diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 0189c171f..ed41539c9 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -1,21 +1,23 @@ %section#top-header.row %h1.col-md-2 = link_to "MyFLiX" - %ul.col-md-4.clearfix - %li= link_to "Videos" - %li= link_to "My Queue" - %li= link_to "People" - = form_tag search_videos_path, class: "span5 for-search" do - %input.search-query(name="search_term" type="text" placeholder="Search for videos here") - %button.btn(type="submit") Search - #user_links.pull-right - %ul - %li.dropdown - %a(href="#" id="dlabel" role="button" data-toggle="dropdown" class="dropdown-toggle" data-target="#") - Welcome, Philip J. Fry - %b.caret - %ul.dropdown-menu(role="menu" aria-labelledby="dlabel") - %li - %a(href="#") Account - %a(href="#") Plan and Billing - %a(href="#") Sign Out + - if current_user + %ul.col-md-4.clearfix + %li= link_to "Videos" + %li= link_to "My Queue" + %li= link_to "People" + = form_tag search_videos_path, class: "span5 for-search" do + %input.search-query(name="search_term" type="text" placeholder="Search for videos here") + %button.btn(type="submit") Search + #user_links.pull-right + %ul + %li.dropdown + %a(href="#" id="dlabel" role="button" data-toggle="dropdown" class="dropdown-toggle" data-target="#") + Welcome, @{current_user.full_name} + %b.caret + %ul.dropdown-menu(role="menu" aria-labelledby="dlabel") + %li + %a(href="#") Account + %a(href="#") Plan and Billing + = link_to "Sign Out", sign_out_path + diff --git a/config/routes.rb b/config/routes.rb index a2ce020a9..f99a02bd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Myflix::Application.routes.draw do root to: "pages#front" + get '/home', to: 'videos#index' resources :videos, only: [:index, :show] do @@ -9,8 +10,11 @@ end resources :users, only: [:create] resources :categories, only: [:show] + resources :sessions, only: [:create] get 'ui(/:action)', controller: 'ui' - get '/home', to: 'videos#index' + get 'register', to: "users#new" get 'sign_in', to: 'sessions#new' + get 'sign_out' to: 'sessions#destroy' + end From c1438cacd9b2e20d4e201a4926470b0904dd0126 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 08:24:23 -0500 Subject: [PATCH 21/37] added require for strong params --- app/controllers/users_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fbd78602e..995caa418 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,11 +4,17 @@ def new end def create - @user = User.new(params[:user]) + @user = User.new(category_params) if @user.save redirect_to sign_in_path else render :new end end + + private + + def category_params + params.require(:user).permit(:full_name, :email, :password) + end end \ No newline at end of file From 567019f550944c1fdf20ff202c435f7e37f71d78 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 09:01:09 -0500 Subject: [PATCH 22/37] still not working --- app/controllers/application_controller.rb | 2 ++ app/controllers/categories_controller.rb | 3 +-- app/controllers/videos_controller.rb | 3 +-- config/routes.rb | 10 ++++------ 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 61453c4a0..b6b69c0c2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,4 +8,6 @@ def require_user def current_user User.find(session[:user_id]) if session[:user_id] end + + helper_method :current_user end diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 775691f55..672444cf6 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -5,7 +5,6 @@ def index end def show - @category = Category.find(params[:id]) - + @category = Category.find(params[:id]) end end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index f1a10004b..d999d4d75 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -3,8 +3,7 @@ class VideosController < ApplicationController def index @videos = Video.all - @categories = Category.all - + @categories = Category.all end def show diff --git a/config/routes.rb b/config/routes.rb index f99a02bd8..e38827396 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,19 +2,17 @@ root to: "pages#front" get '/home', to: 'videos#index' - resources :videos, only: [:index, :show] do collection do post :search, to: "videos#search" end end - resources :users, only: [:create] - resources :categories, only: [:show] - resources :sessions, only: [:create] - get 'ui(/:action)', controller: 'ui' + get 'ui(/:action)', controller: 'ui' get 'register', to: "users#new" get 'sign_in', to: 'sessions#new' get 'sign_out' to: 'sessions#destroy' - + resources :users, only: [:create] + resources :sessions, only: [:create] + resources :categories, only: [:show] end From d6bfb4f910a0aa24d922c90f41059c95f55f0c8d Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 09:58:04 -0500 Subject: [PATCH 23/37] fixed routes --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index e38827396..99911171d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Myflix::Application.routes.draw do root to: "pages#front" - get '/home', to: 'videos#index' + get 'home', to: 'videos#index' resources :videos, only: [:index, :show] do collection do From 92afa524c764c0a89a9f130bd2e0201d5e437873 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 10:14:47 -0500 Subject: [PATCH 24/37] updated --- config/routes.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 99911171d..6c557f192 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,9 +9,10 @@ end get 'ui(/:action)', controller: 'ui' - get 'register', to: "users#new" + get 'register', to: 'users#new' get 'sign_in', to: 'sessions#new' get 'sign_out' to: 'sessions#destroy' + resources :users, only: [:create] resources :sessions, only: [:create] resources :categories, only: [:show] From 4b3fc9b306b1ac905dcb7493fc44f2b08a6f4d0c Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 11:58:14 -0500 Subject: [PATCH 25/37] working --- app/controllers/sessions_controller.rb | 10 ++++++---- app/views/shared/_header.html.haml | 2 +- config/routes.rb | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 78c4d937b..919217711 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -5,17 +5,19 @@ def new def create user = User.where(email: params[:email]).first - if user && user.authentication(params[:password]) + if user && user.authenticate(params[:password]) session[:user_id] = user.id - redirect_to home_path, notice: 'You are signed in, enjoy' + flash[:success] = 'You are signed in, enjoy' + redirect_to home_path else - flash[:error] = "Invalid email or password." + flash[:danger] = "Invalid email or password." redirect_to sign_in_path end end def destroy session[:user_id] = nil - redirect_to root_path, notice: "You are signed out." + flash[:danger] = "You are signed out." + redirect_to root_path end end \ No newline at end of file diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index ed41539c9..46a7d7df8 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -13,7 +13,7 @@ %ul %li.dropdown %a(href="#" id="dlabel" role="button" data-toggle="dropdown" class="dropdown-toggle" data-target="#") - Welcome, @{current_user.full_name} + Welcome, #{current_user.full_name} %b.caret %ul.dropdown-menu(role="menu" aria-labelledby="dlabel") %li diff --git a/config/routes.rb b/config/routes.rb index 6c557f192..cea61049e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,7 @@ get 'ui(/:action)', controller: 'ui' get 'register', to: 'users#new' get 'sign_in', to: 'sessions#new' - get 'sign_out' to: 'sessions#destroy' + get 'sign_out', to: 'sessions#destroy' resources :users, only: [:create] resources :sessions, only: [:create] From cd2f61dc000aaf34b78bd0e66908a2a84efcfb44 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 13:01:50 -0500 Subject: [PATCH 26/37] search --- app/views/shared/_header.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 46a7d7df8..534706fd1 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -6,9 +6,9 @@ %li= link_to "Videos" %li= link_to "My Queue" %li= link_to "People" - = form_tag search_videos_path, class: "span5 for-search" do + = form_tag search_videos_path, class: "span5 for-search" do %input.search-query(name="search_term" type="text" placeholder="Search for videos here") - %button.btn(type="submit") Search + %button.btn.btn-default(type="submit") Search #user_links.pull-right %ul %li.dropdown From 2f26e0bd60384c1bd3d5be7f02d4904467cd6b34 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 14:05:56 -0500 Subject: [PATCH 27/37] search aligned --- app/views/shared/_header.html.haml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 534706fd1..34d17330f 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -6,8 +6,9 @@ %li= link_to "Videos" %li= link_to "My Queue" %li= link_to "People" - = form_tag search_videos_path, class: "span5 for-search" do - %input.search-query(name="search_term" type="text" placeholder="Search for videos here") + = form_tag search_videos_path, class: "form.col-md-5 navbar-form" do + .form-group + %input.form-control(name="search_term" type="text" placeholder="Search for videos here") %button.btn.btn-default(type="submit") Search #user_links.pull-right %ul From a6ca94ba9db4d6439c6c5800d55977afe64774dc Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 14:11:16 -0500 Subject: [PATCH 28/37] search aligned --- app/views/shared/_header.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index 34d17330f..b32a2432d 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -1,6 +1,6 @@ %section#top-header.row %h1.col-md-2 - = link_to "MyFLiX" + = link_to "MyFLiX", home_path - if current_user %ul.col-md-4.clearfix %li= link_to "Videos" From 9b9369cbb2e8bd06e1a148a51ecde243afaca5fd Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 28 May 2014 14:25:02 -0500 Subject: [PATCH 29/37] search with git --- app/views/shared/_header.html.haml | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_header.html.haml b/app/views/shared/_header.html.haml index b32a2432d..25425bdd0 100644 --- a/app/views/shared/_header.html.haml +++ b/app/views/shared/_header.html.haml @@ -6,7 +6,7 @@ %li= link_to "Videos" %li= link_to "My Queue" %li= link_to "People" - = form_tag search_videos_path, class: "form.col-md-5 navbar-form" do + = form_tag search_videos_path, method: :get, class: "form.col-md-5 navbar-form" do .form-group %input.form-control(name="search_term" type="text" placeholder="Search for videos here") %button.btn.btn-default(type="submit") Search diff --git a/config/routes.rb b/config/routes.rb index cea61049e..057b79692 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ resources :videos, only: [:index, :show] do collection do - post :search, to: "videos#search" + get :search, to: "videos#search" end end From 0179ee15698cd5279fb9155062e707409bece270 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Mon, 2 Jun 2014 14:12:43 -0500 Subject: [PATCH 30/37] video controller test --- Gemfile | 2 ++ Gemfile.lock | 5 ++++ spec/controllers/videos_controller_spec.rb | 31 ++++++++++++++++++++++ spec/fabricators/user_fabricator.rb | 5 ++++ spec/fabricators/video_fabricator.rb | 4 +++ 5 files changed, 47 insertions(+) create mode 100644 spec/controllers/videos_controller_spec.rb create mode 100644 spec/fabricators/user_fabricator.rb create mode 100644 spec/fabricators/video_fabricator.rb diff --git a/Gemfile b/Gemfile index a5eb89b1e..569cabf53 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,8 @@ gem 'uglifier' gem 'jquery-rails' gem 'bootstrap_form' gem 'bcrypt-ruby' +gem 'fabrication' +gem 'faker' group :development do gem 'sqlite3' diff --git a/Gemfile.lock b/Gemfile.lock index 24449d5ca..0a438767d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,6 +53,9 @@ GEM erubis (2.7.0) eventmachine (1.0.0) execjs (2.0.2) + fabrication (2.11.2) + faker (1.3.0) + i18n (~> 0.5) haml (4.0.4) tilt haml-rails (0.4) @@ -159,6 +162,8 @@ DEPENDENCIES bootstrap-sass bootstrap_form coffee-rails + fabrication + faker haml-rails jquery-rails pg diff --git a/spec/controllers/videos_controller_spec.rb b/spec/controllers/videos_controller_spec.rb new file mode 100644 index 000000000..28e3071d6 --- /dev/null +++ b/spec/controllers/videos_controller_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe VideosController do + describe "GET show" do + it "sets @video for authenticated users" do + session[:user_id] = Fabricate(:user).id + video = Fabricate(:video) + get :show, id: video.id + expect(assigns(:video)).to eq(video) + end + it "redirects the user to the sign in page for unauthenticated users" do + video = Fabricate(:video) + get :show, id: video.id + expect(response).to redirect_to sign_in_path + end + end + + describe "POST search" do + it "sets @results for authenticated users" do + session[:user_id] = Fabricate(:user).id + futurama = Fabricate(:video, title: 'Futurama') + post :search, search_term: 'rama' + expect(assigns(:results)).to eq([futurama]) + end + it "redirects to the sign in page for the unauthenticated users" do + futurama = Fabricate(:video, title: 'Futurama') + post :search, search_term: 'rama' + expect(response).to redirect_to sign_in_path + end + end +end \ No newline at end of file diff --git a/spec/fabricators/user_fabricator.rb b/spec/fabricators/user_fabricator.rb new file mode 100644 index 000000000..b5a9be89e --- /dev/null +++ b/spec/fabricators/user_fabricator.rb @@ -0,0 +1,5 @@ +Fabricator(:user) do + email { Faker::Internet.email } + password 'password' + full_name { Faker::Name.name} +end \ No newline at end of file diff --git a/spec/fabricators/video_fabricator.rb b/spec/fabricators/video_fabricator.rb new file mode 100644 index 000000000..c68966596 --- /dev/null +++ b/spec/fabricators/video_fabricator.rb @@ -0,0 +1,4 @@ +Fabricator(:video) do + title { Faker::Lorem.words(5).join(" ") } + description { Faker::Lorem.paragraph(2) } +end \ No newline at end of file From ed99c378d3ed78c5b70cf96712d42b06d00d7ab2 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 4 Jun 2014 08:50:49 -0500 Subject: [PATCH 31/37] getting fault --- app/controllers/reviews_controller.rb | 20 ++++++ app/controllers/users_controller.rb | 4 +- app/controllers/videos_controller.rb | 1 + app/models/review.rb | 6 ++ app/models/video.rb | 1 + app/views/videos/show.html.haml | 28 ++++++++ config/routes.rb | 1 + db/migrate/20140603153159_create_reviews.rb | 11 +++ db/schema.rb | 11 ++- db/test.sqlite3-journal | Bin 0 -> 2576 bytes spec/controllers/reviews_controller_spec.rb | 67 +++++++++++++++++ spec/controllers/sessions_controller_spec.rb | 71 +++++++++++++++++++ spec/controllers/users_controller_spec.rb | 39 ++++++++++ spec/controllers/videos_controller_spec.rb | 13 +++- spec/fabricators/review_fabricator.rb | 4 ++ spec/models/video_spec.rb | 1 + 16 files changed, 274 insertions(+), 4 deletions(-) create mode 100644 app/controllers/reviews_controller.rb create mode 100644 app/models/review.rb create mode 100644 db/migrate/20140603153159_create_reviews.rb create mode 100644 db/test.sqlite3-journal create mode 100644 spec/controllers/reviews_controller_spec.rb create mode 100644 spec/controllers/sessions_controller_spec.rb create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/fabricators/review_fabricator.rb diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb new file mode 100644 index 000000000..0a021508a --- /dev/null +++ b/app/controllers/reviews_controller.rb @@ -0,0 +1,20 @@ +class ReviewsController < ApplicationController + before_filter :require_user + + def create + @video = Video.find(params[:video_id]) + review = @video.reviews.build(review_params.merge!(user: current_user)) + if review.save + redirect_to @video + else + @reviews = @video.reviews.reload + render 'videos/show' + end + end + + private + + def review_params + params.require(:review).permit(:rating, :content) + end +end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 995caa418..7027a97ed 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,7 +4,7 @@ def new end def create - @user = User.new(category_params) + @user = User.new(user_params) if @user.save redirect_to sign_in_path else @@ -14,7 +14,7 @@ def create private - def category_params + def user_params params.require(:user).permit(:full_name, :email, :password) end end \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index d999d4d75..34e873503 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -8,6 +8,7 @@ def index def show @video = Video.find(params[:id]) + @reviews = @video.reviews end def search diff --git a/app/models/review.rb b/app/models/review.rb new file mode 100644 index 000000000..ef2211a3a --- /dev/null +++ b/app/models/review.rb @@ -0,0 +1,6 @@ +class Review < ActiveRecord::Base + belongs_to :video + belongs_to :user + + validates_presence_of :content, :rating +end \ No newline at end of file diff --git a/app/models/video.rb b/app/models/video.rb index fb40d94c7..95750a330 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,5 +1,6 @@ class Video < ActiveRecord::Base belongs_to :category + has_many :reviews, order: "created_at DESC" #validates :title, presence: true #validates :description, presence: true validates_presence_of :title, :description diff --git a/app/views/videos/show.html.haml b/app/views/videos/show.html.haml index 431bbb611..0138566f7 100644 --- a/app/views/videos/show.html.haml +++ b/app/views/videos/show.html.haml @@ -11,4 +11,32 @@ .actions %a.btn.btn-primary(href="") Watch Now %a.btn.btn-default(href="") + My Queue +%section.reviews.container + .row + .col-sm-10.col-sm-offset-1 + = form_for [@video, Review.new] do |f| + %fieldset + .form-group + %label Rate this video + = f.select :rating, options_for_select([5,4,3,2,1].map {|number| [pluralize(number, "Star")]}) + .form-group + %label Write Review + .row + .col-sm-8 + = f.text_area :content, rows: 6, class: "form-control" + %fieldset.form-group.actions.clearfix + %input(type="submit" value="Submit" class="btn") + = link_to "Cancel", @video + %header + %h3 User Reviews (#{@reviews.count}) + %ul + - @reviews.each do |review| + %article.review + %li.row + .col-sm-2 + %span Rating: #{review.rating} / 5 + %p by #{review.user.full_name} + .col-sm-8 + %p= review.content + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 057b79692..68a30a0d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ collection do get :search, to: "videos#search" end + resources :reviews, only: [:create] end get 'ui(/:action)', controller: 'ui' diff --git a/db/migrate/20140603153159_create_reviews.rb b/db/migrate/20140603153159_create_reviews.rb new file mode 100644 index 000000000..bd2b8c0aa --- /dev/null +++ b/db/migrate/20140603153159_create_reviews.rb @@ -0,0 +1,11 @@ +class CreateReviews < ActiveRecord::Migration + def change + create_table :reviews do |t| + t.integer :user_id + t.integer :video_id + t.text :content + t.integer :rating + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 54b833569..b53c05466 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140528010226) do +ActiveRecord::Schema.define(version: 20140603153159) do create_table "categories", force: true do |t| t.string "name" @@ -19,6 +19,15 @@ t.datetime "updated_at" end + create_table "reviews", force: true do |t| + t.integer "user_id" + t.integer "video_id" + t.text "content" + t.integer "rating" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "users", force: true do |t| t.string "email" t.string "password_digest" diff --git a/db/test.sqlite3-journal b/db/test.sqlite3-journal new file mode 100644 index 0000000000000000000000000000000000000000..0420da48c7d936a4b3ab17410e1ca14df1772889 GIT binary patch literal 2576 zcmZQzKmbX`CKn)&i-Ca&NU_ literal 0 HcmV?d00001 diff --git a/spec/controllers/reviews_controller_spec.rb b/spec/controllers/reviews_controller_spec.rb new file mode 100644 index 000000000..149afeccc --- /dev/null +++ b/spec/controllers/reviews_controller_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe ReviewsController do + describe "POST create" do + let(:video) { Fabricate(video) } + + context "with authenticated users" do + + let(:current_user) { Fabricate(:user) } + before { session[:user_id] = current_user.id } + + context "with valid inputs" do + # before do + # post :create, review: Fabricate.attributes_for(:review), video_id: video.id + # end + it "redirects to the video show page" do + post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(response).to redirect_to video + end + it "creates a review" do + post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.count).to eq(1) + end + it "creates a review associated with the video" do + post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.first.video).to eq(video) + end + it "creates a review associated with the signed in user" do + post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.first.user).to eq(current_user) + end + + end + context "with invalid inputs" do + it "does not create a review" do + + post :create, review: {rating: 4}, video_id: video.id + expect(Review.count).to eq(0) + end + it "renders the videos/show template" do + + post :create, review: {rating: 4}, video_id: video.id + expect(response).to render_template "videos/show" + end + + it "sets @video" do + + post :create, review: {rating: 4}, video_id: video.id + expect(assigns(:video)).to eq(video) + end + it "sets @reviews" do + + review = Fabricate(:review, video: video) + post :create, review: {rating: 4}, video_id: video.id + expect(assigns(:reviews)).to match_array([review]) + end + end + end + context "with unauthenticated users" do + it "redirects to the sign in path" do + + post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(response).to redirect_to sign_in_path + end + end + end +end \ No newline at end of file diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 000000000..f9c714731 --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +describe SessionsController do + describe "GET new" do + it "renders the new template for unauthenticated users" do + get :new + expect(response).to render_template :new + end + it "redirects to the home page for authenticated users" do + session[:user_id] = Fabricate(:user).id + get :new + expect(response).to redirect_to home_path + end + end + + describe "POST create" do + context "with valid credentials" do + before do + alice = Fabricate(:user) + post :create, email: alice.email, password: alice.password + end + + it "puts the signed in user in the session" do + alice = Fabricate(:user) + post :create, email: alice.email, password: alice.password + expect(session[:user_id]).to eq(alice.id) + end + + it "redirects to the home page" do + expect(response).to redirect_to home_path + end + + it "sets the notice" do + expect(flash[:success]).not_to be_blank + end + end + + context "with invalid credentials" do + before do + alice = Fabricate(:user) + post :create, email: alice.email, password: alice.password + 'qwerty' + end + it "does not put the signed in user in the session" do + expect(session[:user_id]).to be_nil + end + it "redirects to the sign in page" do + expect(response).to redirect_to sign_in_path + end + it "sets the error message" do + expect(flash[:danger]).not_to be_blank + end + end + end + + describe "GET destroy" do + before do + session[:user_id] = Fabricate(:user).id + get :destroy + end + + it "clears the session for the user" do + expect(session[:user_id]).to be_nil + end + it "redirects to the root path" do + expect(response).to redirect_to root_path + end + it "sets the notice" do + expect(flash[:danger]).not_to be_blank + end + end +end \ No newline at end of file diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 000000000..3b74115ac --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe UsersController do + describe "GET new" do + it "sets @user" do + get :new + expect(assigns(:user)).to be_instance_of(User) + end + end + describe "POST create" do + context "with valid input" do + before do + post :create, user: Fabricate.attributes_for(:user) + end + it "creates the user" do + expect(User.count).to eq(1) + end + it "redirects to the sign in page" do + expect(response).to redirect_to sign_in_path + end + end + context "with invalid input" do + before do + post :create, user: { password: "password", full_name: "Chuck Norris"} + end + it "does not create the user" do + expect(User.count).to eq(0) + end + + it "render the :new template" do + expect(response).to render_template :new + end + + it "sets @user" do + expect(assigns(:user)).to be_instance_of(User) + end + end + end +end diff --git a/spec/controllers/videos_controller_spec.rb b/spec/controllers/videos_controller_spec.rb index 28e3071d6..e3099c8e8 100644 --- a/spec/controllers/videos_controller_spec.rb +++ b/spec/controllers/videos_controller_spec.rb @@ -7,7 +7,18 @@ video = Fabricate(:video) get :show, id: video.id expect(assigns(:video)).to eq(video) - end + end + + it "sets @reviews for authenticated users" do + session[:user_id] = Fabricate(:user).id + video = Fabricate(:video) + review1 = Fabricate(:review, video: video) + review2 = Fabricate(:review, video: video) + get :show, id: video.id + expect(assigns(:reviews)).to match_array([review1, review2]) + + end + it "redirects the user to the sign in page for unauthenticated users" do video = Fabricate(:video) get :show, id: video.id diff --git a/spec/fabricators/review_fabricator.rb b/spec/fabricators/review_fabricator.rb new file mode 100644 index 000000000..72884b607 --- /dev/null +++ b/spec/fabricators/review_fabricator.rb @@ -0,0 +1,4 @@ +Fabricator(:review) do + rating { (1..5).to_a.sample } + content { Faker::Lorem.paragraph(3) } +end \ No newline at end of file diff --git a/spec/models/video_spec.rb b/spec/models/video_spec.rb index cd07f59a8..a51abbab3 100644 --- a/spec/models/video_spec.rb +++ b/spec/models/video_spec.rb @@ -5,6 +5,7 @@ it { should belong_to(:category)} it { should validate_presence_of(:title)} it { should validate_presence_of(:description)} + it { should have_many(:reviews).order("created_at DESC")} describe "search_by_title" do it "returns an empty array if there is no match" do From 50c58817603633c86dfb7de1f041eb80a613f4e8 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 4 Jun 2014 09:02:08 -0500 Subject: [PATCH 32/37] getting fault --- db/test.sqlite3-journal | Bin 2576 -> 2576 bytes spec/controllers/reviews_controller_spec.rb | 12 +++--------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/db/test.sqlite3-journal b/db/test.sqlite3-journal index 0420da48c7d936a4b3ab17410e1ca14df1772889..ca39f70072a32a3fe9b918ac55b041660a3ed44f 100644 GIT binary patch delta 40 hcmbOrGC@Ru0SU~>KHa`i;0r4Yh_RW2jfDlw0sydh3hw{_ delta 40 gcmbOrGC@Ru0SQPlHo0sR_`=EpVr=GMV_^Za0C{Exga7~l diff --git a/spec/controllers/reviews_controller_spec.rb b/spec/controllers/reviews_controller_spec.rb index 149afeccc..a555aec73 100644 --- a/spec/controllers/reviews_controller_spec.rb +++ b/spec/controllers/reviews_controller_spec.rb @@ -32,25 +32,20 @@ end context "with invalid inputs" do - it "does not create a review" do - + it "does not create a review" do post :create, review: {rating: 4}, video_id: video.id expect(Review.count).to eq(0) end - it "renders the videos/show template" do - + it "renders the videos/show template" do post :create, review: {rating: 4}, video_id: video.id expect(response).to render_template "videos/show" end - it "sets @video" do - + it "sets @video" do post :create, review: {rating: 4}, video_id: video.id expect(assigns(:video)).to eq(video) end it "sets @reviews" do - - review = Fabricate(:review, video: video) post :create, review: {rating: 4}, video_id: video.id expect(assigns(:reviews)).to match_array([review]) end @@ -58,7 +53,6 @@ end context "with unauthenticated users" do it "redirects to the sign in path" do - post :create, review: Fabricate.attributes_for(:review), video_id: video.id expect(response).to redirect_to sign_in_path end From 8cc44e896f35802a3754573b0745ec329d137026 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Wed, 4 Jun 2014 15:57:01 -0500 Subject: [PATCH 33/37] all passed --- app/models/video.rb | 1 + db/test.sqlite3-journal | Bin 2576 -> 0 bytes spec/controllers/reviews_controller_spec.rb | 17 +++++++++-------- 3 files changed, 10 insertions(+), 8 deletions(-) delete mode 100644 db/test.sqlite3-journal diff --git a/app/models/video.rb b/app/models/video.rb index 95750a330..86ee987b8 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,6 +1,7 @@ class Video < ActiveRecord::Base belongs_to :category has_many :reviews, order: "created_at DESC" + #has_many :reviews, -> { order: ("created_at DESC") } #validates :title, presence: true #validates :description, presence: true validates_presence_of :title, :description diff --git a/db/test.sqlite3-journal b/db/test.sqlite3-journal deleted file mode 100644 index ca39f70072a32a3fe9b918ac55b041660a3ed44f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2576 zcmZQzK!7>fr`v%%E(QiBAjN`^7-bFT5Mbp6`EE42hjSc{`fW4>aE1W1+-Dvw2XF?_ OsKjUp4EqoOl>-2;CI?0U diff --git a/spec/controllers/reviews_controller_spec.rb b/spec/controllers/reviews_controller_spec.rb index a555aec73..2474d76af 100644 --- a/spec/controllers/reviews_controller_spec.rb +++ b/spec/controllers/reviews_controller_spec.rb @@ -2,7 +2,7 @@ describe ReviewsController do describe "POST create" do - let(:video) { Fabricate(video) } + let(:video) { Fabricate(:video) } context "with authenticated users" do @@ -10,23 +10,23 @@ before { session[:user_id] = current_user.id } context "with valid inputs" do - # before do - # post :create, review: Fabricate.attributes_for(:review), video_id: video.id - # end - it "redirects to the video show page" do + before do post :create, review: Fabricate.attributes_for(:review), video_id: video.id + end + it "redirects to the video show page" do + expect(response).to redirect_to video end it "creates a review" do - post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.count).to eq(1) end it "creates a review associated with the video" do - post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.first.video).to eq(video) end it "creates a review associated with the signed in user" do - post :create, review: Fabricate.attributes_for(:review), video_id: video.id + expect(Review.first.user).to eq(current_user) end @@ -46,6 +46,7 @@ expect(assigns(:video)).to eq(video) end it "sets @reviews" do + review = Fabricate(:review, video: video) post :create, review: {rating: 4}, video_id: video.id expect(assigns(:reviews)).to match_array([review]) end From d3445a8662ac3c7e166981dcc41dff31857bd73c Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 10 Jun 2014 12:01:53 -0500 Subject: [PATCH 34/37] couple more --- app/models/category.rb | 1 + app/models/user.rb | 2 ++ app/views/videos/show.html.haml | 1 + config/routes.rb | 7 ++++++- db/schema.rb | 10 +++++++++- spec/models/category_spec.rb | 1 + 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 46fdec51d..686a7f4b6 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -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) diff --git a/app/models/user.rb b/app/models/user.rb index 80939ee38..a45d9a238 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,6 @@ class User < ActiveRecord::Base validates_uniqueness_of :email has_secure_password validations: false + + has_many :queue_items end \ No newline at end of file diff --git a/app/views/videos/show.html.haml b/app/views/videos/show.html.haml index 0138566f7..fef25c4e7 100644 --- a/app/views/videos/show.html.haml +++ b/app/views/videos/show.html.haml @@ -11,6 +11,7 @@ .actions %a.btn.btn-primary(href="") Watch Now %a.btn.btn-default(href="") + My Queue + = link_to "+ My Queue", queue_items_path, method: :post, class: "btn" %section.reviews.container .row .col-sm-10.col-sm-offset-1 diff --git a/config/routes.rb b/config/routes.rb index 68a30a0d2..1031e9dc5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,12 @@ end resources :reviews, only: [:create] end + + resources :categories, only: [:show] + resources :queue_items, only: [:create] + get 'my_queue', to: 'queue_items#index' + get 'ui(/:action)', controller: 'ui' get 'register', to: 'users#new' get 'sign_in', to: 'sessions#new' @@ -16,5 +21,5 @@ resources :users, only: [:create] resources :sessions, only: [:create] - resources :categories, only: [:show] + end diff --git a/db/schema.rb b/db/schema.rb index b53c05466..d505afe2b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140603153159) do +ActiveRecord::Schema.define(version: 20140609145631) do create_table "categories", force: true do |t| t.string "name" @@ -19,6 +19,14 @@ 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" diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index cfc6d707f..70f0252f3 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -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 the reverse chronical order by created at" do From fc9e66fece7e28216b76621a8007b3489007751a Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 10 Jun 2014 13:47:17 -0500 Subject: [PATCH 35/37] working --- app/views/videos/show.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/videos/show.html.haml b/app/views/videos/show.html.haml index fef25c4e7..33f0b9254 100644 --- a/app/views/videos/show.html.haml +++ b/app/views/videos/show.html.haml @@ -11,7 +11,7 @@ .actions %a.btn.btn-primary(href="") Watch Now %a.btn.btn-default(href="") + My Queue - = link_to "+ My Queue", queue_items_path, method: :post, class: "btn" + = 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 From 4a5c32c73a0e15e6f6419c7da8fbfd43f95da743 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 10 Jun 2014 13:48:56 -0500 Subject: [PATCH 36/37] working now --- app/controllers/queue_items_controller.rb | 28 ++++++++ app/models/queue_item.rb | 16 +++++ app/views/queue_items/index.html.haml | 38 ++++++++++ .../20140609145631_create_queue_items.rb | 9 +++ .../queue_items_controller_spec.rb | 70 +++++++++++++++++++ spec/fabricators/category_fabricator.rb | 3 + spec/fabricators/queue_item_fabricator.rb | 2 + spec/models/queue_item_spec.rb | 48 +++++++++++++ 8 files changed, 214 insertions(+) create mode 100644 app/controllers/queue_items_controller.rb create mode 100644 app/models/queue_item.rb create mode 100644 app/views/queue_items/index.html.haml create mode 100644 db/migrate/20140609145631_create_queue_items.rb create mode 100644 spec/controllers/queue_items_controller_spec.rb create mode 100644 spec/fabricators/category_fabricator.rb create mode 100644 spec/fabricators/queue_item_fabricator.rb create mode 100644 spec/models/queue_item_spec.rb diff --git a/app/controllers/queue_items_controller.rb b/app/controllers/queue_items_controller.rb new file mode 100644 index 000000000..eb6430758 --- /dev/null +++ b/app/controllers/queue_items_controller.rb @@ -0,0 +1,28 @@ +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 + + private + + def queue_video(video) + QueueItem.create(video: video, user: current_user, position: new_queue_item_position) unless current_user_queued_vide?(video) + end + + def new_queue_item_position + current_user.queue_items.count + 1 + end + + def current_user_queued_vide?(video) + current_user.queue_items.map(&:video).include?(video) + end + +end \ No newline at end of file diff --git a/app/models/queue_item.rb b/app/models/queue_item.rb new file mode 100644 index 000000000..702d9d016 --- /dev/null +++ b/app/models/queue_item.rb @@ -0,0 +1,16 @@ +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 \ No newline at end of file diff --git a/app/views/queue_items/index.html.haml b/app/views/queue_items/index.html.haml new file mode 100644 index 000000000..947cc5d5e --- /dev/null +++ b/app/views/queue_items/index.html.haml @@ -0,0 +1,38 @@ +%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 + %select.form-group(name="") + %option(value="5") 5 Stars + %option(value="4") 4 Stars + %option(value="3") 3 Stars + %option(value="2") 2 Stars + %option(value="1") 1 Star + %td + = link_to queue_item.category_name, queue_item.category + %td + = link_to "", method: :post do + %i.glyphicon.glyphicon-remove + + = button_to "Update Instant Queue", nil, class: "btn btn-default" diff --git a/db/migrate/20140609145631_create_queue_items.rb b/db/migrate/20140609145631_create_queue_items.rb new file mode 100644 index 000000000..4d453d7fb --- /dev/null +++ b/db/migrate/20140609145631_create_queue_items.rb @@ -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 diff --git a/spec/controllers/queue_items_controller_spec.rb b/spec/controllers/queue_items_controller_spec.rb new file mode 100644 index 000000000..2ff1128e9 --- /dev/null +++ b/spec/controllers/queue_items_controller_spec.rb @@ -0,0 +1,70 @@ +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 sign 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 vide 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 in 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 +end \ No newline at end of file diff --git a/spec/fabricators/category_fabricator.rb b/spec/fabricators/category_fabricator.rb new file mode 100644 index 000000000..fb3ef3696 --- /dev/null +++ b/spec/fabricators/category_fabricator.rb @@ -0,0 +1,3 @@ +Fabricator(:category) do + name { Faker::Lorem.words(1) } +end \ No newline at end of file diff --git a/spec/fabricators/queue_item_fabricator.rb b/spec/fabricators/queue_item_fabricator.rb new file mode 100644 index 000000000..be82b7d0a --- /dev/null +++ b/spec/fabricators/queue_item_fabricator.rb @@ -0,0 +1,2 @@ +Fabricator(:queue_item) do +end \ No newline at end of file diff --git a/spec/models/queue_item_spec.rb b/spec/models/queue_item_spec.rb new file mode 100644 index 000000000..da753aea0 --- /dev/null +++ b/spec/models/queue_item_spec.rb @@ -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 + 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's 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 \ No newline at end of file From 640484a6be5125e5ee35e06fb3e6bc0956c08233 Mon Sep 17 00:00:00 2001 From: Quinn Avila Date: Tue, 10 Jun 2014 14:47:16 -0500 Subject: [PATCH 37/37] finish week2 --- app/controllers/queue_items_controller.rb | 6 ++++ app/views/queue_items/index.html.haml | 2 +- config/routes.rb | 2 +- .../queue_items_controller_spec.rb | 29 +++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/controllers/queue_items_controller.rb b/app/controllers/queue_items_controller.rb index eb6430758..6231eda1a 100644 --- a/app/controllers/queue_items_controller.rb +++ b/app/controllers/queue_items_controller.rb @@ -11,6 +11,12 @@ def create 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) diff --git a/app/views/queue_items/index.html.haml b/app/views/queue_items/index.html.haml index 947cc5d5e..43911a187 100644 --- a/app/views/queue_items/index.html.haml +++ b/app/views/queue_items/index.html.haml @@ -32,7 +32,7 @@ %td = link_to queue_item.category_name, queue_item.category %td - = link_to "", method: :post do + = link_to queue_item, method: :delete do %i.glyphicon.glyphicon-remove = button_to "Update Instant Queue", nil, class: "btn btn-default" diff --git a/config/routes.rb b/config/routes.rb index 1031e9dc5..02048bbb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ end resources :categories, only: [:show] - resources :queue_items, only: [:create] + resources :queue_items, only: [:create, :destroy] get 'my_queue', to: 'queue_items#index' diff --git a/spec/controllers/queue_items_controller_spec.rb b/spec/controllers/queue_items_controller_spec.rb index 2ff1128e9..9db17eaec 100644 --- a/spec/controllers/queue_items_controller_spec.rb +++ b/spec/controllers/queue_items_controller_spec.rb @@ -67,4 +67,33 @@ 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 \ No newline at end of file