Files
danbooru/app/controllers/posts_controller.rb
2013-02-22 12:24:10 -05:00

71 lines
1.9 KiB
Ruby

class PostsController < ApplicationController
before_filter :member_only, :except => [:show, :index]
after_filter :save_recent_tags, :only => [:update]
respond_to :html, :xml, :json
rescue_from PostSets::SearchError, :with => :rescue_exception
rescue_from Post::SearchError, :with => :rescue_exception
def index
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit])
@posts = @post_set.posts
respond_with(@posts) do |format|
format.atom
end
rescue ::ActiveRecord::StatementInvalid => e
if e.to_s =~ /statement timeout/
@error_message = "The database timed out running your query. Try a simpler query that returns fewer results."
render :action => "error"
else
raise
end
end
def show
@post = Post.find(params[:id])
@post_flag = PostFlag.new(:post_id => @post.id)
@post_appeal = PostAppeal.new(:post_id => @post.id)
respond_with(@post)
end
def update
@post = Post.find(params[:id])
@post.update_attributes(params[:post], :as => CurrentUser.role)
respond_with(@post) do |format|
format.html do
if @post.errors.any?
@error_message = @post.errors.full_messages.join("; ")
render :action => "error"
else
redirect_to post_path(@post)
end
end
format.json do
render :json => @post.to_json
end
end
end
def revert
@post = Post.find(params[:id])
@version = PostVersion.find(params[:version_id])
@post.revert_to!(@version)
respond_with(@post) do |format|
format.js
end
end
private
def tag_query
params[:tags] || (params[:post] && params[:post][:tags])
end
def save_recent_tags
if tag_query
tags = Tag.scan_tags(tag_query)
tags = TagAlias.to_aliased(tags) + Tag.scan_tags(session[:recent_tags])
session[:recent_tags] = tags.uniq.slice(0, 40).join(" ")
end
end
end