beast rev 2066
[beast-modified.git] / app / controllers / topics_controller.rb
blob4c65136e4ed809dce3f91af29a2ad54995ff51e7
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]
8   def index
9     respond_to do |format|
10       format.html { redirect_to forum_path(params[:forum_id]) }
11       format.xml do
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
14       end
15     end
16   end
18   def new
19     @topic = Topic.new
20   end
21   
22   def show
23     respond_to do |format|
24       format.html do
25         # see notes in application.rb on how this works
26         update_last_seen_at
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]
32         @post   = Post.new
33       end
34       format.xml do
35         render :xml => @topic.to_xml
36       end
37       format.rss do
38         @posts = @topic.posts.find(:all, :order => 'created_at desc', :limit => 25)
39         render :action => 'show', :layout => false
40       end
41     end
42   end
43   
44   def create
45     # this is icky - move the topic/first post workings into the topic model?
46     Topic.transaction do
47       @topic  = @forum.topics.build(params[:topic])
48       assign_protected
49       @post       = @topic.posts.build(params[:topic])
50       @post.topic = @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?
55       @post.save! 
56     end
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) }
60     end
61   end
62   
63   def update
64     @topic.attributes = params[:topic]
65     assign_protected
66     @topic.save!
67     respond_to do |format|
68       format.html { redirect_to topic_path(@forum, @topic) }
69       format.xml  { head 200 }
70     end
71   end
72   
73   def destroy
74     @topic.destroy
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 }
79     end
80   end
81   
82   protected
83     def assign_protected
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
89       return unless admin?
90       @topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id]
91     end
92     
93     def find_forum_and_topic
94       @forum = Forum.find(params[:forum_id])
95       @topic = @forum.topics.find(params[:id]) if params[:id]
96     end
97     
98     def authorized?
99       %w(new create).include?(action_name) || @topic.editable_by?(current_user)
100     end