1 class TopicsController < ApplicationController
2 before_filter :find_forum_and_topic, :except => :index
3 before_filter :login_required, :only => [:new, :create, :edit, :update, :destroy]
5 caches_formatted_page :rss, :show
6 cache_sweeper :posts_sweeper, :only => [:create, :update, :destroy]
10 format.html { redirect_to forum_path(params[:forum_id]) }
12 @topics = Topic.find_all_by_forum_id(params[:forum_id], :order => 'sticky desc, replied_at desc', :limit => 25)
13 render :xml => @topics.to_xml
23 respond_to do |format|
25 # see notes in application.rb on how this works
27 # keep track of when we last viewed this topic for activity indicators
28 (session[:topics] ||= {})[@topic.id] = Time.now.utc if logged_in?
29 # authors of topics don't get counted towards total hits
30 @topic.hit! unless logged_in? and @topic.user == current_user
31 @posts = Post.paginate_by_topic_id params[:id], :include => :user, :order => "#{Post.table_name}.created_at", :per_page => 25, :page => params[:page]
35 render :xml => @topic.to_xml
38 @posts = @topic.posts.find(:all, :order => 'created_at desc', :limit => 25)
39 render :action => 'show', :layout => false
45 # this is icky - move the topic/first post workings into the topic model?
47 @topic = @forum.topics.build(params[:topic])
49 @post = @topic.posts.build(params[:topic])
51 @post.user = current_user
52 # only save topic if post is valid so in the view topic will be a new record if there was an error
53 @topic.body = @post.body # incase save fails and we go back to the form
54 @topic.save! if @post.valid?
57 respond_to do |format|
58 format.html { redirect_to topic_path(@forum, @topic) }
59 format.xml { head :created, :location => formatted_topic_url(:forum_id => @forum, :id => @topic, :format => :xml) }
64 @topic.attributes = params[:topic]
67 respond_to do |format|
68 format.html { redirect_to topic_path(@forum, @topic) }
69 format.xml { head 200 }
75 flash[:notice] = "Topic '{title}' was deleted."[:topic_deleted_message, @topic.title]
76 respond_to do |format|
77 format.html { redirect_to forum_path(@forum) }
78 format.xml { head 200 }
84 @topic.user = current_user if @topic.new_record?
85 # admins and moderators can sticky and lock topics
86 return unless admin? or current_user.moderator_of?(@topic.forum)
87 @topic.sticky, @topic.locked = params[:topic][:sticky], params[:topic][:locked]
88 # only admins can move
90 @topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id]
93 def find_forum_and_topic
94 @forum = Forum.find(params[:forum_id])
95 @topic = @forum.topics.find(params[:id]) if params[:id]
99 %w(new create).include?(action_name) || @topic.editable_by?(current_user)