forked from gotealeaf/myflix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Jameskcoleman/week_6_part_1
Week 6 part 1
- Loading branch information
Showing
24 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class LargeCoverUploader < CarrierWave::Uploader::Base | ||
include CarrierWave::MiniMagick | ||
process :resize_to_fill => [665, 375] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class SmallCoverUploader < CarrierWave::Uploader::Base | ||
include CarrierWave::MiniMagick | ||
process :resize_to_fill => [166, 236] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddAdminToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :admin, :boolean | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
db/migrate/20140804182605_add_large_cover_and_small_cover_to_videos.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddVideoUrlToVideos < ActiveRecord::Migration | ||
def change | ||
add_column :videos, :video_url, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.