Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 6 part 1 #11

Merged
merged 3 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem 'sidekiq'
gem 'unicorn'
gem 'sentry-raven'
gem 'paratrooper'
gem 'carrierwave'
gem 'mini_magick'

group :development do
gem 'sqlite3'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ GEM
capybara-email (2.4.0)
capybara (~> 2.4)
mail
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
celluloid (0.15.2)
timers (~> 1.1.0)
coderay (1.1.0)
Expand Down Expand Up @@ -99,6 +104,8 @@ GEM
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25.1)
mini_magick (3.8.0)
subexec (~> 0.2.1)
mini_portile (0.6.0)
minitest (5.3.4)
multi_json (1.10.1)
Expand Down Expand Up @@ -192,6 +199,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.8)
subexec (0.2.3)
thin (1.5.0)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
Expand Down Expand Up @@ -227,6 +235,7 @@ DEPENDENCIES
bootstrap_form
capybara
capybara-email
carrierwave
coffee-rails
database_cleaner (= 1.2.0)
fabrication
Expand All @@ -235,6 +244,7 @@ DEPENDENCIES
jquery-rails
launchy
letter_opener
mini_magick
paratrooper
pg
pry
Expand Down
32 changes: 32 additions & 0 deletions app/controllers/admin/videos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Admin::VideosController < ApplicationController
before_filter :require_user
before_filter :require_admin

def new
@video = Video.new
end

def create
@video = Video.new(video_params)
if @video.save
flash[:success] = "You have successfully added the video '#{@video.title}'."
redirect_to new_admin_video_path
else
flash[:danger] = "You cannot add this video. Please check the errors."
render :new
end
end

private

def video_params
params.require(:video).permit!
end

def require_admin
if !current_user.admin?
flash[:danger] = "You are not authorized to do that."
redirect_to home_path
end
end
end
5 changes: 5 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class Video < ActiveRecord::Base
belongs_to :category
has_many :reviews, -> { order "created_at DESC" }
has_many :queue_items

mount_uploader :large_cover, LargeCoverUploader
mount_uploader :small_cover, SmallCoverUploader

validates_presence_of :title, :description

def self.search_by_title(search_term)
Expand Down
4 changes: 4 additions & 0 deletions app/uploaders/large_cover_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class LargeCoverUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :resize_to_fill => [665, 375]
end
4 changes: 4 additions & 0 deletions app/uploaders/small_cover_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SmallCoverUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :resize_to_fill => [166, 236]
end
22 changes: 22 additions & 0 deletions app/views/admin/videos/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%section.admin_add_video
.container
.row
.col-md-10.col-md-offset-1
= bootstrap_form_for [:admin, @video], html: { class: "form-horizontal" } do |f|
%ul.nav.nav-tabs
%li
%a(href="/ui/admin_views_payments") Recent Payments
%li.active
%a(href="") Add a New Video
%br
%fieldset
= f.text_field :title, class: "span3"
= f.select :category_id, options_for_select(Category.all.map {|category| [category.name, category.id]})
= f.text_area :description, rows: 8, class: "span6"
= f.file_field :large_cover, class: "btn btn-file"
= f.file_field :small_cover, class: "btn btn-file"
= f.text_field :video_url, class: "span3", label: "Video URL"

%fieldset.actions.form-group
.col-sm-6.col-md-offset-3
%input(type="submit" value="Add Video" class="btn btn-default")
1 change: 1 addition & 0 deletions app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
%span Rating: 4.5/5.0
%p= @video.description
.actions
%a.btn.btn-primary(href="#{@video.video_url}") Watch Now
= link_to "+ My Queue", queue_items_path(video_id: @video.id), method: :post, class: 'btn' unless current_user.queued_video?(@video)
%section.reviews.container
.row
Expand Down
14 changes: 14 additions & 0 deletions config/initializers/carrier_wave.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CarrierWave.configure do |config|
if Rails.env.staging? || Rails.env.production?
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxx', # required
:aws_secret_access_key => 'yyy', # required
}
config.fog_directory = 'name_of_directory' # required
else
config.storage = :file
config.enable_processing = Rails.env.development?
end
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
resources :reviews, only: [:create]
end

namespace :admin do
resources :videos, only: [:new, :create]
end

resources :users, only: [:show]
get 'people', to: 'relationships#index'
resources :relationships, only: [:create, :destroy]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20140729012152_add_admin_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddLargeCoverAndSmallCoverToVideos < ActiveRecord::Migration
def change
add_column :videos, :large_cover, :string
add_column :videos, :small_cover, :string
remove_column :videos, :large_cover_url
remove_column :videos, :small_cover_url
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140804220527_add_video_url_to_videos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddVideoUrlToVideos < ActiveRecord::Migration
def change
add_column :videos, :video_url, :string
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140717130842) do
ActiveRecord::Schema.define(version: 20140804220527) do

create_table "categories", force: true do |t|
t.string "name"
Expand Down Expand Up @@ -60,16 +60,18 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
t.boolean "admin"
end

create_table "videos", force: true do |t|
t.string "title"
t.string "small_cover_url"
t.string "large_cover_url"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "category_id"
t.string "large_cover"
t.string "small_cover"
t.string "video_url"
end

end
Binary file added public/uploads/futurama.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/monk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/monk_large.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions spec/controllers/admin/videos_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'spec_helper'

describe Admin::VideosController do
describe "GET new" do
it_behaves_like "requires sign in" do
let(:action) { get :new }
end
it_behaves_like "requires admin" do
let(:action) { get :new }
end
it "sets the @video to a new video" do
set_current_admin
get :new
expect(assigns(:video)).to be_instance_of Video
expect(assigns(:video)).to be_new_record
end
it "redirects the regular user to the home path" do
set_current_user
get :new
expect(response).to redirect_to home_path
end
it "sets the flash error message for regular user" do
set_current_user
get :new
expect(flash[:danger]).to be_present
end
end

describe "POST create" do
it_behaves_like "requires sign in" do
let(:action) { post :create }
end
it_behaves_like "requires admin" do
let(:action) { post :create }
end

context "with valid input" do
it "redirects to the add new video page" do
set_current_admin
category = Fabricate(:category)
post :create, video: { title: "Monk", category_id: category.id, description: "good show!" }
expect(response).to redirect_to new_admin_video_path
end
it "creates a video" do
set_current_admin
category = Fabricate(:category)
post :create, video: { title: "Monk", category_id: category.id, description: "good show!" }
expect(category.videos.count).to eq(1)
end
it "sets the flash success message" do
set_current_admin
category = Fabricate(:category)
post :create, video: { title: "Monk", category_id: category.id, description: "good show!" }
expect(flash[:success]).to be_present
end
end

context "with invalid input" do
it "does not create a video" do
set_current_admin
category = Fabricate(:category)
post :create, video: { category_id: category.id, description: "good show!" }
expect(category.videos.count).to eq(0)
end
it "renders the :new template" do
set_current_admin
category = Fabricate(:category)
post :create, video: { category_id: category.id, description: "good show!" }
expect(response).to render_template :new
end
it "sets the @video variable" do
set_current_admin
category = Fabricate(:category)
post :create, video: { category_id: category.id, description: "good show!" }
expect(assigns(:video)).to be_present
end
it "sets the flash danger message" do
set_current_admin
category = Fabricate(:category)
post :create, video: { category_id: category.id, description: "good show!" }
expect(flash[:danger]).to be_present
end
end
end
end
5 changes: 5 additions & 0 deletions spec/fabricators/user_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
email { Faker::Internet.email }
password 'password'
full_name { Faker::Name.name }
admin false
end

Fabricator(:admin, from: :user) do
admin true
end
25 changes: 25 additions & 0 deletions spec/features/admin_adds_new_video_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

feature 'Admin adds new video' do
scenario 'Admin successfully adds a new video' do
admin = Fabricate(:admin)
drama = Fabricate(:category, name: "Dramas")
sign_in(admin)
visit new_admin_video_path

fill_in "Title", with: "Monk"
select "Dramas", from: "Category"
fill_in "Description", with: "SF detective"
attach_file "Large cover", "spec/support/uploads/monk_large.jpg"
attach_file "Small cover", "spec/support/uploads/monk.jpg"
fill_in "Video URL", with: "http://www.example.com/my_video.mp4"
click_button "Add Video"

sign_out
sign_in

visit video_path(Video.first)
expect(page).to have_selector("img[src='/uploads/monk_large.jpg']")
expect(page).to have_selector("a[href='http://www.example.com/my_video.mp4']")
end
end
4 changes: 4 additions & 0 deletions spec/support/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ def set_current_user(user=nil)
session[:user_id] = (user || Fabricate(:user)).id
end

def set_current_admin(admin=nil)
session[:user_id] = (admin || Fabricate(:admin)).id
end

def sign_in(a_user=nil)
user = a_user || Fabricate(:user)
visit sign_in_path
Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
end
end

shared_examples "requires admin" do
it "redirects to the sign in page" do
session[:user_id] = Fabricate(:user)
action
expect(response).to redirect_to home_path
end
end


shared_examples "tokenable" do
it "generates a random token when the user is created" do
alice = Fabricate(:user)
Expand Down
Binary file added spec/support/uploads/monk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/support/uploads/monk_large.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.