Skip to content

Commit

Permalink
#170 video
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashnikovisme committed Oct 4, 2015
1 parent e7721cb commit d87c080
Show file tree
Hide file tree
Showing 19 changed files with 244 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/controllers/web/admin/videos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Web::Admin::VideosController < Web::Admin::ApplicationController
def index
@videos = {}
@videos[:active] = Content::Video.active.page(params[:page]).decorate
@videos[:unviewed] = Content::Video.unviewed.page(params[:page]).decorate
@videos[:removed] = Content::Video.removed.page(params[:page]).decorate
@videos[:search] = Content::Video.search_everywhere(params[:search]).page(params[:page]).decorate if params[:search]
end

def new
@video_form = Content::VideoForm.new_with_model
end

def edit
@video_form = Content::VideoForm.find_with_model params[:id]
end

def create
@video_form = Content::VideoForm.new_with_model
@video_form.submit(params[:video])
if @video_form.save
redirect_to admin_videos_path
else
render action: :new
end
end

def update
@video_form = Content::VideoForm.find_with_model params[:id]
@video_form.submit(params[:video])
if @video_form.save
redirect_to edit_admin_video_path @video_form.model
else
render action: :edit
end
end

def destroy
@video = Content::Video.find params[:id]
@video.remove
redirect_to admin_videos_path
end
end
3 changes: 3 additions & 0 deletions app/forms/content/video_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Content::VideoForm < ApplicationForm
attributes :title, :link, :description, :author_id, :state
end
5 changes: 5 additions & 0 deletions app/models/content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Content
def self.table_name_prefix
'content_'
end
end
27 changes: 27 additions & 0 deletions app/models/content/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Content::Video < ActiveRecord::Base
belongs_to :author, class_name: 'User'

validates :title, presence: true
validates :link, presence: :url

include Content::VideoScopes

state_machine :state, initial: :active do
state :active
state :unviewed
state :removed

event :make_active do
transition unviewed: :active
end
event :hide do
transition active: :unviewed
end
event :remove do
transition all => :removed
end
event :restore do
transition removed: :unviewed
end
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class User < ActiveRecord::Base
has_many :article
has_many :registrations, class_name: 'Event::Registration',
foreign_key: :user_id
has_many :videos, class_name: 'Content::Video',
foreign_key: :author_id

validates :email, email: true,
allow_blank: true
Expand Down
13 changes: 13 additions & 0 deletions app/scopes/content/video_scopes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Content::VideoScopes
extend ActiveSupport::Concern
include Concerns::StateMachine

included do
scope :active, -> { where(state: :active).order('created_at ASC') }
scope :unviewed, -> { where(state: :unviewed).order('id DESC') }
scope :presented, -> { where.not(state: :removed).order('id ASC') }
scope :removed, -> { where state: :removed }
scope :with_vertical, -> { where.not(vertical: nil) }
scope :with_horizontal, -> { where.not(horizontal: nil) }
end
end
18 changes: 18 additions & 0 deletions app/views/web/admin/videos/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- model_class = Content::Video
- current_title = page_title(action, model_class.model_name.human)
- title current_title, :admin
.page-header
%h1= page_title(action, Content::Video.model_name.human)
.row
.col-lg-12
= render 'layouts/web/admin/shared/messages', object: @video_form
.row
.col-lg-6
= simple_form_for [:admin, @video_form], input_html: { class: 'form-horizontal' } do |f|
= f.input :title, as: :string
= f.input :description, as: :string
= f.input :author_id, as: :select, collection: users_hash(@users), input_html: { class: :select2 }
= f.input :state_event, as: :state_event
= f.input :link, as: :string
= f.button :submit, t('helpers.links.save'), class: 'btn-success'
= link_to t('helpers.links.back'), admin_members_path, class: 'btn btn-default'
23 changes: 23 additions & 0 deletions app/views/web/admin/videos/_videos_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
= paginate videos, theme: 'twitter-bootstrap-3'
- model_class = Content::Video
%table.table.table-condensed.table-hover
%thead
%tr
%th= model_class.human_attribute_name(:id)
%th= model_class.human_attribute_name(:photo)
%th= model_class.human_attribute_name(:link)
%th= model_class.human_attribute_name(:duration)
%th= t 'helpers.links.actions'
%tbody
- videos.each do |video|
%tr.link{ class: state_color(video), data: { href: edit_admin_video_path(video) } }
%td= video.id
%td= image_tag video.small_thumb
%td= link_to video.link, video.link
%td= "#{l(video.begin_date)} - #{l(video.end_date)}"
%td.actions
= link_to edit_admin_video_path(video), class: 'btn btn-warning btn-xs' do
%span.glyphicon.glyphicon-pencil
= link_to admin_video_path(video), method: :delete, class: 'btn btn-xs btn-danger' do
%span.glyphicon.glyphicon-remove
= paginate videos, theme: 'twitter-bootstrap-3'
1 change: 1 addition & 0 deletions app/views/web/admin/videos/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render partial: 'form', locals: { action: :update }
1 change: 1 addition & 0 deletions app/views/web/admin/videos/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render 'web/admin/default/index', items: @videos
1 change: 1 addition & 0 deletions app/views/web/admin/videos/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render partial: 'form', locals: { action: :create }
1 change: 1 addition & 0 deletions config/locales/ru/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ru:
feedback: Обратная связь
comment: Комментарий
letter: Письмо
video: Видео
attributes:
user:
first_name: Имя
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
resources :categories
resources :activity_lines, except: [:show]
resources :banners, except: [:show]
resources :videos, except: [:show]
resources :feedbacks, except: [:show]
resources :events
resources :questionaries
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20151004221640_create_content_videos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateContentVideos < ActiveRecord::Migration
def change
create_table :content_videos do |t|
t.text :title
t.text :description
t.integer :author_id
t.text :link
t.text :state

t.timestamps null: false
end
end
end
12 changes: 11 additions & 1 deletion 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: 20150917064102) do
ActiveRecord::Schema.define(version: 20151004221640) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -112,6 +112,16 @@
t.datetime "updated_at", null: false
end

create_table "content_videos", force: :cascade do |t|
t.text "title"
t.text "description"
t.integer "author_id"
t.text "link"
t.text "state"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "documents", force: :cascade do |t|
t.text "file"
t.text "title"
Expand Down
1 change: 1 addition & 0 deletions lib/yaml/russian_cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cases:
'отделения': отделений
'руководитель': руководителя
'письмо': письмо
'видео': видео
dative:
'местное отделение': местном отделении
'первичная организация': первичной организации
Expand Down
64 changes: 64 additions & 0 deletions test/controllers/web/admin/videos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'test_helper'

class Web::Admin::VideosControllerTest < ActionController::TestCase
setup do
@video = create :content_video
admin = create :admin
sign_in admin
@exceptions_attributes = ['id', 'created_at', 'updated_at']
end

test 'should get new' do
get :new
assert_response :success, @response.body
end

test 'should get index' do
get :index
assert_response :success, @response.body
end

test 'should get index with search' do
get :index, search: @video.link
assert_response :success, @response.body
end

test 'should get index without instances' do
Content::Video.all.map &:destroy
get :index
assert_response :success, @response.body
end

test 'should create video' do
attributes = attributes_for :video
post :create, video: attributes
assert_response :redirect, @response.body
assert_redirected_to admin_videos_path
video = Content::Video.last
video.attributes.keys.except(*@exceptions_attributes).each do |key|
assert_equal attributes[key.to_sym], video.send(key), key
end
end

test 'should get edit' do
get :edit, id: @video
assert_response :success, @response.body
end

test 'should patch update' do
attributes = attributes_for :video
patch :update, video: attributes, id: @video
assert_response :redirect, @response.body
assert_redirected_to edit_admin_video_path @video
@video.reload
@video.attributes.keys.except(*@exceptions_attributes).each do |key|
assert_equal attributes[key.to_sym], @video.send(key), key
end
end

test 'should delete destroy' do
delete :destroy, id: @video
@video.reload
assert @video.removed?
end
end
9 changes: 9 additions & 0 deletions test/factories/content_videos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do
factory :content_video, class: 'Content::Video' do
title { generate :string }
description { generate :string }
link { generate :url }
author_id { User.last ? User.last.id : create(:user).id }
state { Content::Video.state_machines[:state].states.map(&:name).first.to_s }
end
end
7 changes: 7 additions & 0 deletions test/models/content/video_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Content::VideoTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit d87c080

Please sign in to comment.