Class: TopicsController

Inherits:
BeastApplicationController show all
Defined in:
app/controllers/topics_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/topics_controller.rb', line 47

def create
  topic_saved, post_saved = false, false
# this is icky - move the topic/first post workings into the topic model?
  Topic.transaction do
   @topic  = @forum.topics.build(params[:topic])
    assign_protected
    @post       = @topic.posts.build(params[:topic])
    @post.topic = @topic
    @post.user  = 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 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

#destroyObject



82
83
84
85
86
87
88
89
# File 'app/controllers/topics_controller.rb', line 82

def destroy
  @topic.destroy
  flash[:notice] = "Topic '{title}' was deleted."[:topic_deleted_message, @topic.title]
  respond_to do |format|
    format.html { redirect_to forum_path(@forum) }
    format.xml  { head 200 }
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
# File 'app/controllers/topics_controller.rb', line 10

def index
  respond_to do |format|
    format.html { redirect_to forum_path(params[:forum_id]) }
    format.xml do
      @topics = 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

#newObject



20
21
22
# File 'app/controllers/topics_controller.rb', line 20

def new
  @topic = Topic.new
end

#showObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/topics_controller.rb', line 24

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.user == current_user
      @posts = @topic.posts.paginate :page => params[:page]
      User.find(:all, :conditions => ['id IN (?)', @posts.collect { |p| p.user_id }.uniq]) unless @posts.blank?
      @post   = 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

#updateObject



72
73
74
75
76
77
78
79
80
# File 'app/controllers/topics_controller.rb', line 72

def update
  @topic.attributes = params[:topic]
  assign_protected
  @topic.save!
  respond_to do |format|
    format.html { redirect_to forum_topic_path(@forum, @topic) }
    format.xml  { head 200 }
  end
end