Class: TopicsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/topics_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/topics_controller.rb', line 36

def create
  topic_save, post_save = false, false
 
  Topic.transaction do
    @topic  = Topic.new(:title => params[:topic][:title])
    @topic.forum_id = @forum.id
    assign_protected
    if @topic.save
      topic_save = true
      @post       = Post.new(:body => params[:topic][:body])
      @post.topic_id = @topic.id
      @post.forum_id = @topic.forum_id
      @post.user  = current_user
      @topic.body = @post.body
      post_save = true if @post.save
    end
  end

  if topic_save && post_save
    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



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

def destroy
  @topic.destroy
  respond_to do |format|
    format.html { redirect_to forum_path(@forum) }
    format.xml  { head 200 }
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/topics_controller.rb', line 5

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



15
16
17
# File 'app/controllers/topics_controller.rb', line 15

def new
  @topic = Topic.new
end

#showObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/topics_controller.rb', line 19

def show
  respond_to do |format|
    format.html do
      # keep track of when we last viewed this topic for activity indicators
      (session[:topics] ||= {})[@topic.id] = Time.now.utc if !current_user.nil?
      # authors of topics don't get counted towards total hits
      @topic.hit! unless current_user.nil? 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.nil?
      @post   = Post.new
    end
    format.xml do
      render :xml => @topic.to_xml
    end
  end
end

#updateObject



64
65
66
67
68
69
70
71
72
# File 'app/controllers/topics_controller.rb', line 64

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