forked from jlapier/Rails-CMS-Wiki-Forum
/
message_posts_controller.rb
110 lines (96 loc) · 3.51 KB
/
message_posts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class MessagePostsController < ApplicationController
before_filter :require_user, :except => [:index, :show]
before_filter :get_forum, :only => [:index, :show, :edit, :update, :create, :destroy]
before_filter :require_forum_read_access, :only => [:show, :index]
before_filter :require_forum_write_access, :only => [:edit, :update, :destroy, :upload_handler]
def index
redirect_to @forum
end
# GET /forums/1/message_posts/1
# GET /forums/1/message_posts/1.xml
def show
@message_post = MessagePost.find(params[:id]) #@forum.message_posts.find(params[:id])
if @message_post.thread
redirect_to forum_message_post_url(@forum, @message_post.thread, :anchor => @message_post.id)
else
@child_posts = @message_post.child_posts.paginate :page => params[:page], :order => 'created_at ASC'
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @message_post }
format.atom
format.rss
end
end
end
# GET /forums/1/message_posts/new
# GET /forums/1/message_posts/new.xml
def new
@message_post = MessagePost.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @message_post }
end
end
# GET /forums/1/message_posts/1/edit
def edit
@message_post = MessagePost.find(params[:id])
end
# POST /forums/1/message_posts
# POST /forums/1/message_posts.xml
def create
@message_post = MessagePost.new(params[:message_post])
@message_post.forum = @forum unless @message_post.thread
@message_post.user = current_user
respond_to do |format|
if @message_post.save
flash[:notice] = "Posted: #{@message_post.subject}"
format.html do
if @message_post.thread
@message_post.thread.update_attribute(:updated_at, Time.now)
redirect_to forum_message_post_url(@forum, @message_post.thread, :anchor => @message_post.id,
:page => @message_post.thread.child_posts.last_page_number_for)
else
redirect_to forum_message_post_url(@forum, @message_post)
end
end
format.xml { render :xml => @message_post, :status => :created, :location => @message_post }
else
format.html { render :action => "new" }
format.xml { render :xml => @message_post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /forums/1/message_posts/1
# PUT /forums/1/message_posts/1.xml
def update
@message_post = MessagePost.find(params[:id]) #@forum.message_posts.find(params[:id])
respond_to do |format|
if @message_post.update_attributes(params[:message_post])
flash[:notice] = 'MessagePost was successfully updated.'
format.html { redirect_to forum_message_post_url(@forum, @message_post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @message_post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /forums/1/message_posts/1
# DELETE /forums/1/message_posts/1.xml
def destroy
@message_post = MessagePost.find(params[:id]) #@forum.message_posts.find(params[:id])
@message_post.destroy
respond_to do |format|
flash[:notice] = "Post '#{@message_post.subject}' deleted."
format.html { redirect_to(@forum) }
format.xml { head :ok }
end
end
protected
def get_forum
@forum ||= Forum.find(params[:forum_id])
end
def single_access_allowed?
action_name == 'show'
end
end