forked from rubytaiwan/ruby-taiwan
-
Notifications
You must be signed in to change notification settings - Fork 2
/
topics_controller.rb
133 lines (118 loc) · 3.55 KB
/
topics_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# coding: utf-8
class TopicsController < ApplicationController
before_filter :require_user, :only => [:new,:edit,:create,:update,:destroy,:reply]
before_filter :init_list_sidebar, :only => [:index,:recent,:show,:cate,:search]
caches_page :feed, :expires_in => 1.hours
private
def init_list_sidebar
if !fragment_exist? "topic/init_list_sidebar/hot_nodes"
@hot_nodes = Node.hots.limit(20)
end
if current_user
@user_last_nodes = Node.find_last_visited_by_user(current_user.id)
end
end
public
# GET /topics
# GET /topics.xml
def index
@topics = Topic.last_actived.limit(10)
@sections = Section.all
set_seo_meta("论坛","#{APP_CONFIG['app_name']}论坛,#{APP_CONFIG['app_name']}小区论坛,#{APP_CONFIG['app_name']}业主论坛")
end
def feed
@topics = Topic.recents.limit(20)
response.headers['Content-Type'] = 'application/rss+xml'
render :layout => false
end
def node
@node = Node.find(params[:id])
if current_user
Node.set_user_last_visited(current_user.id, @node.id)
end
@topics = @node.topics.last_actived.paginate(:page => params[:page],:per_page => 50)
set_seo_meta("#{@node.name} » 社区论坛","#{APP_CONFIG['app_name']}社区#{@node.name}",@node.summary)
render :action => "index"
end
def recent
@topics = Topic.recents.limit(50)
set_seo_meta("最近活跃的50个帖子 » 社区论坛")
render :action => "index"
end
def search
@topics = Topic.search(params[:key], :page => params[:page], :per_page => 50)
set_seo_meta("搜索#{params[:s]} » 社区论坛")
render :action => "index"
end
def show
@topic = Topic.find(params[:id])
if current_user
Node.set_user_last_visited(current_user.id, @topic.node_id)
@topic.user_readed(current_user.id)
end
@node = @topic.node
@replies = @topic.replies.all
set_seo_meta("#{@topic.title} » 社区论坛")
end
# GET /topics/new
# GET /topics/new.xml
def new
@topic = Topic.new
@topic.node_id = params[:node]
@node = Node.find(params[:node])
if @node.blank?
render_404
end
set_seo_meta("发帖子 » 社区论坛")
end
def reply
@topic = Topic.find(params[:id])
@reply = @topic.replies.build(params[:reply])
@reply.user_id = current_user.id
if @reply.save
flash[:notice] = "回复成功。"
else
flash[:notice] = @reply.errors.full_messages.join("<br />")
end
redirect_to topic_path(params[:id],:anchor => 'reply')
end
# GET /topics/1/edit
def edit
@topic = current_user.topics.find(params[:id])
set_seo_meta("改帖子 » 社区论坛")
end
# POST /topics
# POST /topics.xml
def create
pt = params[:topic]
@topic = Topic.new(pt)
@topic.user_id = current_user.id
@topic.node_id = params[:node] || pt[:node_id]
if @topic.save
redirect_to(topic_path(@topic.id), :notice => '帖子创建成功.')
else
render :action => "new"
end
end
# PUT /topics/1
# PUT /topics/1.xml
def update
@topic = current_user.topics.find(params[:id])
pt = params[:topic]
@topic.node_id = pt[:node_id]
@topic.title = pt[:title]
@topic.body = pt[:body]
if @topic.save
redirect_to(topic_path(@topic.id), :notice => '帖子修改成功.')
else
render :action => "edit"
end
end
# DELETE /topics/1
# DELETE /topics/1.xml
def destroy
@topic = current_user.topics.find(params[:id])
@topic.destroy
redirect_to(topics_path, :notice => '帖子删除成功.')
end
end