asciinema.org/app/controllers/asciicasts_controller.rb

80 lines
1.9 KiB
Ruby
Raw Normal View History

2011-11-23 21:30:09 +00:00
class AsciicastsController < ApplicationController
2012-07-25 18:24:20 +00:00
PER_PAGE = 15
2012-03-04 14:18:09 +00:00
2012-04-06 19:51:05 +00:00
before_filter :load_resource, :only => [:show, :edit, :update, :destroy]
2012-07-25 18:24:20 +00:00
before_filter :count_view, :only => [:show]
2012-04-03 18:47:59 +00:00
before_filter :ensure_authenticated!, :only => [:edit, :update, :destroy]
before_filter :ensure_owner!, :only => [:edit, :update, :destroy]
2012-04-03 17:57:47 +00:00
2012-03-03 17:38:11 +00:00
respond_to :html, :json
2011-11-23 21:30:09 +00:00
def index
2012-04-06 19:51:05 +00:00
collection = Asciicast.
order("created_at DESC").
page(params[:page]).
per(PER_PAGE)
2012-04-06 19:51:05 +00:00
2012-07-25 18:24:20 +00:00
@category_name = "All Asciicasts"
@current_category = :all
2012-04-06 19:51:05 +00:00
@asciicasts = AsciicastDecorator.decorate(collection)
2011-11-27 12:54:10 +00:00
end
2012-07-25 18:24:20 +00:00
def popular
collection = Asciicast.
order("views_count DESC").
where("views_count > 0").
page(params[:page]).
per(PER_PAGE)
@category_name = "Popular Asciicasts"
@current_category = :popular
@asciicasts = AsciicastDecorator.decorate(collection)
render :index
end
2011-11-27 12:54:10 +00:00
def show
@asciicast = AsciicastDecorator.new(@asciicast)
2012-07-25 18:24:20 +00:00
@asciicast_author = UserDecorator.new(@asciicast.user)
@title = @asciicast.smart_title
respond_with @asciicast
2011-11-23 21:30:09 +00:00
end
2012-04-03 18:47:59 +00:00
def edit
end
def update
@asciicast.update_attributes(params[:asciicast])
redirect_to asciicast_path(@asciicast),
2012-07-25 18:24:20 +00:00
:notice => 'Asciicast was updated.'
2012-04-03 18:47:59 +00:00
end
2012-04-03 17:57:47 +00:00
def destroy
2012-04-03 18:47:59 +00:00
if @asciicast.destroy
2012-04-03 17:57:47 +00:00
redirect_to profile_path(current_user),
2012-07-25 18:24:20 +00:00
:notice => 'Asciicast was deleted.'
2012-04-03 17:57:47 +00:00
else
2012-04-03 18:47:59 +00:00
redirect_to asciicast_path(@asciicast),
2012-07-25 18:24:20 +00:00
:alert => "Oops, we couldn't remove this asciicast. Try again later."
2012-04-03 17:57:47 +00:00
end
end
private
def load_resource
@asciicast = Asciicast.find(params[:id])
end
2012-04-03 18:47:59 +00:00
2012-07-25 18:24:20 +00:00
def count_view
unless request.xhr?
Asciicast.increment_counter :views_count, @asciicast.id
end
end
2012-04-03 18:47:59 +00:00
def ensure_owner!
if @asciicast.user != current_user
redirect_to asciicast_path(@asciicast), :alert => "You can't do that."
end
end
2011-11-23 21:30:09 +00:00
end