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

Add video #11

Merged
merged 4 commits into from
Dec 24, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@

database.yml
.env
public/uploads
.DS_Store
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ gem "slim"
gem "bootstrap-sass", "~> 3.3.1.0"
gem "omniauth-github", "~> 1.1.2"
gem "trailblazer", :github => "apotonick/trailblazer"
gem "cancancan", "~> 1.9.2"
gem "simple_form", "~> 3.1.0"
gem "carrierwave"

group :development, :test do
gem "rspec-rails", "~> 3.1.0"
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ GEM
byebug (2.7.0)
columnize (~> 0.3)
debugger-linecache (~> 1.2)
cancancan (1.9.2)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
codeclimate-test-reporter (0.4.3)
simplecov (>= 0.7.1, < 1.0.0)
coderay (1.1.0)
Expand Down Expand Up @@ -233,6 +239,9 @@ GEM
sass (~> 3.2.2)
sprockets (~> 2.8, < 3.0)
sprockets-rails (~> 2.0)
simple_form (3.1.0)
actionpack (~> 4.0)
activemodel (~> 4.0)
simplecov (0.9.1)
docile (~> 1.1.0)
multi_json (~> 1.0)
Expand Down Expand Up @@ -281,7 +290,9 @@ DEPENDENCIES
better_errors
binding_of_caller (>= 0.7.2)
bootstrap-sass (~> 3.3.1.0)
cancancan (~> 1.9.2)
capybara (~> 2.4.4)
carrierwave
codeclimate-test-reporter
coffee-rails (~> 4.0.0)
database_cleaner
Expand All @@ -299,6 +310,7 @@ DEPENDENCIES
rails (= 4.1.8)
rspec-rails (~> 3.1.0)
sass-rails (~> 4.0.3)
simple_form (~> 3.1.0)
slim
spring
spring-commands-rspec
Expand Down
34 changes: 34 additions & 0 deletions app/concepts/video/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Video < ActiveRecord::Base
class Create < Trailblazer::Operation
include CRUD
include AuthorizedOperation

model Video, :create
authorize :manage, Video

def video
model
end

contract do
model Video

property :title, :validates => { :presence => true }
property :presenter_id, :validates => { :presence => true }
property :original_url, :validates => { :presence => true }
property :host
property :embed_url, :validates => { :presence => true }
property :status
property :thumbnail
property :originally_posted_on, :validates => { :presence => true }
end

def authorised_process(params)
validate(params[:video]) do |f|
f.save
end
end
end
end


6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class ApplicationController < ActionController::Base
include Trailblazer::Operation::Controller

protect_from_forgery :with => :exception
helper_method :current_user

rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => "Access denied. You do not have permission to do that."
end

private

def current_user
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class SessionsController < ApplicationController
include Trailblazer::Operation::Controller

def create
run Authentication::LoginWithOmniAuth do |op|
login(op.user)
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class VideosController < ApplicationController
def new
form Video::Create
end

def create
run Video::Create do |op|
return redirect_to root_path, :notice => "Successfully added video!"
end

render :new
end

def process_params!(params)
params.merge!(
:current_user => current_user
)
end
end
11 changes: 11 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
module ApplicationHelper
def presenters
Presenter.order(:name).map { |p| [p.name, p.id] }
end

def hosts
Video.hosts.keys.map { |key| [key.titleize, key] }
end

def video_statuses
Video.statuses.keys.map { |key| [key.titleize, key] }
end
end
11 changes: 11 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Ability
include CanCan::Ability

def initialize(user)
return unless user.authenticated?

if user.admin?
can :manage, :all
end
end
end
2 changes: 2 additions & 0 deletions app/models/presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Presenter < ActiveRecord::Base
end
6 changes: 6 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Video < ActiveRecord::Base
mount_uploader :thumbnail, ThumbnailUploader

enum :host => %i(you_tube vimeo)
enum :status => %i(pending published)
end
7 changes: 7 additions & 0 deletions app/uploaders/thumbnail_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ThumbnailUploader < CarrierWave::Uploader::Base
storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
19 changes: 12 additions & 7 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ html
= csrf_meta_tags

body
nav class="main-menu" role="navigation"
.container
ul.nav.navbar-nav
- if can? :manage, Video
li= link_to "Add Video", new_video_path

- if current_user.authenticated?
li Logged in as #{current_user.nickname}
li= link_to "Logout", session_path, :method => :delete
- else
li= link_to "Login with GitHub", "/auth/github"

.container
.row
.col-md-12
= render "flash"
.row
.col-md-12
- if current_user.authenticated?
p Logged in as #{current_user.nickname}
p= link_to "Logout", session_path, :method => :delete
- else
p= link_to "Login with GitHub", "/auth/github"
.row
.col-md-12
= yield
12 changes: 12 additions & 0 deletions app/views/videos/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h1 Add Video

= simple_form_for @form do |f|
= f.input :title
= f.input :presenter_id, :collection => presenters
= f.input :originally_posted_on, :label => "Original date"
= f.input :original_url
= f.input :host, :collection => hosts
= f.input :embed_url
= f.input :status, :collection => video_statuses
= f.input :thumbnail
= f.submit "Save"
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

module RubyVideos
class Application < Rails::Application
config.autoload_paths << Rails.root.join("lib")
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
Loading