app/controllers/an_axe/forums/topics_controller.rb
module AnAxe
module Forums
class TopicsController < ApplicationController
before_filter :find_forum_and_topic, :except => :index
before_filter :login_required, :only => [:new, :create, :edit, :update, :destroy]
helper AnAxe::Forums::TopicsHelper
#TODO: put page parameter in URL path so :index & :show can be cached
#cache_action :index, :show # how to cache rss?
#cache_sweeper AnAxe::PostsSweeper, :only => [ :create, :update, :destroy ]
def index
respond_to do |format|
format.html { redirect_to forum_path(params[:forum_id]) }
format.xml do
@topics = AnAxe::Topic.paginate_by_forum_id(params[:forum_id], :order => 'sticky desc, replied_at desc', :page => params[:page])
render :xml => @topics.to_xml
end
end
end
def new
@topic = Topic.new
respond_to do |format|
format.html
end
end
def show
respond_to do |format|
format.html do
# see notes in application.rb on how this works
update_last_seen_at
# keep track of when we last viewed this topic for activity indicators
(session[:topics] ||= {})[@topic.id] = Time.now.utc if logged_in?
# authors of topics don't get counted towards total hits
@topic.hit! unless logged_in? and @topic.send(AnAxe::Config.user_relation) == current_user
@posts = @topic.posts.paginate :page => params[:page]
AnAxe::Config.user_class.find(:all, :conditions => ['id IN (?)', @posts.collect { |p| p.user_id }.uniq]) unless @posts.blank?
@post = AnAxe::Post.new
end
format.xml do
render :xml => @topic.to_xml
end
format.rss do
@posts = @topic.posts.find(:all, :order => 'created_at desc', :limit => 25)
render :action => 'show', :layout => false
end
end
end
def create
topic_saved, post_saved = false, false
# this is icky - move the topic/first post workings into the topic model?
AmAxe::Topic.transaction do
@topic = @forum.topics.build(params[:topic])
assign_protected
@post = @topic.posts.build(params[:topic])
# The tags don't seem to set properly with a build call, so we check
# for them manually and set them if found
@post.body_list = params[:topic]['body_list'] if params[:topic]['body_list']
@post.topic = @topic
@post.send("#{AnAxe::Config.user_relation}=", current_user)
# only save topic if post is valid so in the view topic will be a new record if there was an error
@topic.body = @post.body # incase save fails and we go back to the form
topic_saved = @topic.save if @post.valid?
post_saved = @post.save
end
if topic_saved && post_saved
respond_to do |format|
format.html { redirect_to forums_forum_topic_path(@forum, @topic) }
format.xml { head :created, :location => topic_url(:forum_id => @forum, :id => @topic, :format => :xml) }
end
else
render :action => "new"
end
end
def update
@topic.attributes = params[:topic]
assign_protected
@topic.save!
respond_to do |format|
format.html { redirect_to forums_forum_topic_path(@forum, @topic) }
format.xml { head 200 }
end
end
def destroy
@topic.destroy
flash[:notice] = t("savage_beast.topic_deleted_message", :topic => @topic.title)
respond_to do |format|
format.html { redirect_to forum_path(@forum) }
format.xml { head 200 }
end
end
protected
def assign_protected
@topic.send("#{AnAxe::Config.user_relation}=", current_user) if @topic.new_record?
# admins and moderators can sticky and lock topics
return unless admin? or current_user.moderator_of?(@topic.forum)
@topic.sticky, @topic.locked = params[:topic][:sticky], params[:topic][:locked]
# only admins can move
return unless admin?
@topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id]
end
def find_forum_and_topic
@forum = AnAxe::Forum.find(params[:forum_id])
@topic = @forum.topics.find(params[:id]) if params[:id]
end
def authorized?
%w(new create).include?(action_name) || @topic.editable_by?(current_user)
end
end
end
end