Class: TopicsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#fresh_when, #redirect_back_or_default, #redirect_referrer_or_default, #render_403, #render_404, #render_optional_error_file, #require_user, #set_active_menu, #set_locale, #set_seo_meta, #store_location, #unread_notify_count

Instance Method Details

#banObject


214
215
216
217
218
# File 'app/controllers/topics_controller.rb', line 214

def ban
  @topic = Topic.find(params[:id])
  @topic.ban!
  redirect_to @topic, success: '已转移到 NoPoint 节点。'
end

#check_current_user_liked_repliesObject


84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/topics_controller.rb', line 84

def check_current_user_liked_replies
  return false unless current_user

  # 找出用户 like 过的 Reply,给 JS 处理 like 功能的状态
  @user_liked_reply_ids = []
  @replies.each do |r|
    unless r.liked_user_ids.index(current_user.id).nil?
      @user_liked_reply_ids << r.id
    end
  end
end

#check_current_user_status_for_topicObject


96
97
98
99
100
101
102
103
104
# File 'app/controllers/topics_controller.rb', line 96

def check_current_user_status_for_topic
  return false unless current_user
  # 通知处理
  current_user.read_topic(@topic, replies_ids: @replies.collect(&:id))
  # 是否关注过
  @has_followed = @topic.followed?(current_user.id)
  # 是否收藏
  @has_favorited = current_user.favorited_topic?(@topic.id)
end

#createObject


131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/topics_controller.rb', line 131

def create
  @topic = Topic.new(topic_params)
  @topic.user_id = current_user.id
  @topic.node_id = params[:node] || topic_params[:node_id]

  if @topic.save
    redirect_to(topic_path(@topic.id), notice: t('topics.create_topic_success'))
  else
    render action: 'new'
  end
end

#destroyObject


174
175
176
177
178
# File 'app/controllers/topics_controller.rb', line 174

def destroy
  @topic = Topic.find(params[:id])
  @topic.destroy_by(current_user)
  redirect_to(topics_path, notice: t('topics.delete_topic_success'))
end

#editObject


124
125
126
127
128
129
# File 'app/controllers/topics_controller.rb', line 124

def edit
  @topic = Topic.find(params[:id])
  @node = @topic.node

  set_seo_meta "#{t('topics.edit_topic')} &raquo; #{t('menu.topics')}"
end

#excellentObject


58
59
60
61
62
63
64
# File 'app/controllers/topics_controller.rb', line 58

def excellent
  @topics = Topic.excellent.recent.fields_for_list.includes(:user)
  @topics = @topics.paginate(page: params[:page], per_page: 25, total_entries: 1500)

  set_seo_meta [t('topics.topic_list.excellent'), t('menu.topics')].join(' &raquo; ')
  render action: 'index'
end

#favoriteObject


180
181
182
183
# File 'app/controllers/topics_controller.rb', line 180

def favorite
  current_user.favorite_topic(params[:id])
  render plain: '1'
end

#feedObject


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

def feed
  @topics = Topic.without_hide_nodes.recent.without_body.limit(20).includes(:node, :user, :last_reply_user)
  render layout: false
end

#followObject


190
191
192
193
194
# File 'app/controllers/topics_controller.rb', line 190

def follow
  @topic = Topic.find(params[:id])
  @topic.push_follower(current_user.id)
  render plain: '1'
end

#indexObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/topics_controller.rb', line 5

def index
  @suggest_topics = Topic.without_hide_nodes.suggest.fields_for_list.limit(3).to_a

  @topics = Topic.last_actived.without_suggest
  if current_user
    @topics = @topics.without_nodes(current_user.blocked_node_ids)
    @topics = @topics.without_users(current_user.blocked_user_ids)
  else
    @topics = @topics.without_hide_nodes
  end
  @topics = @topics.fields_for_list
  @topics = @topics.paginate(page: params[:page], per_page: 22, total_entries: 1500).to_a

  set_seo_meta t('menu.topics'), "#{Setting.app_name}#{t('menu.topics')}"
end

#newObject


113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/topics_controller.rb', line 113

def new
  @topic = Topic.new
  unless params[:node].blank?
    @topic.node_id = params[:node]
    @node = Node.find_by_id(params[:node])
    render_404 if @node.blank?
  end

  set_seo_meta "#{t('topics.post_topic')} &raquo; #{t('menu.topics')}"
end

#nodeObject


26
27
28
29
30
31
32
33
# File 'app/controllers/topics_controller.rb', line 26

def node
  @node = Node.find(params[:id])
  @topics = @node.topics.last_actived.fields_for_list
  @topics = @topics.includes(:user).paginate(page: params[:page], per_page: 25)
  title = @node.jobs? ? @node.name : "#{@node.name} &raquo; #{t('menu.topics')}"
  set_seo_meta title, "#{Setting.app_name}#{t('menu.topics')}#{@node.name}", @node.summary
  render action: 'index'
end

#node_feedObject


35
36
37
38
39
# File 'app/controllers/topics_controller.rb', line 35

def node_feed
  @node = Node.find(params[:id])
  @topics = @node.topics.recent.without_body.limit(20)
  render layout: false
end

#previewObject


143
144
145
146
147
148
149
# File 'app/controllers/topics_controller.rb', line 143

def preview
  @body = params[:body]

  respond_to do |format|
    format.json
  end
end

#recentObject


51
52
53
54
55
56
# File 'app/controllers/topics_controller.rb', line 51

def recent
  @topics = Topic.without_hide_nodes.recent.fields_for_list.includes(:user)
  @topics = @topics.paginate(page: params[:page], per_page: 25, total_entries: 1500)
  set_seo_meta [t('topics.topic_list.recent'), t('menu.topics')].join(' &raquo; ')
  render action: 'index'
end

#set_special_node_active_menuObject


106
107
108
109
110
111
# File 'app/controllers/topics_controller.rb', line 106

def set_special_node_active_menu
  case @node.try(:id)
  when Node.jobs_id
    @current = ['/jobs']
  end
end

#showObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/topics_controller.rb', line 66

def show
  @topic = Topic.unscoped.includes(:user).find(params[:id])
  render_404 if @topic.deleted?

  @topic.hits.incr(1)
  @node = @topic.node

  @show_raw = params[:raw] == '1'

  @replies = Reply.unscoped.where(topic_id: @topic.id).without_body.order(:id).all

  check_current_user_liked_replies
  check_current_user_status_for_topic
  set_special_node_active_menu

  set_seo_meta "#{@topic.title} &raquo; #{t('menu.topics')}"
end

#suggestObject


202
203
204
205
206
# File 'app/controllers/topics_controller.rb', line 202

def suggest
  @topic = Topic.find(params[:id])
  @topic.update_attributes(excellent: 1)
  redirect_to @topic, success: '加精成功。'
end

#unfavoriteObject


185
186
187
188
# File 'app/controllers/topics_controller.rb', line 185

def unfavorite
  current_user.unfavorite_topic(params[:id])
  render plain: '1'
end

#unfollowObject


196
197
198
199
200
# File 'app/controllers/topics_controller.rb', line 196

def unfollow
  @topic = Topic.find(params[:id])
  @topic.pull_follower(current_user.id)
  render plain: '1'
end

#unsuggestObject


208
209
210
211
212
# File 'app/controllers/topics_controller.rb', line 208

def unsuggest
  @topic = Topic.find(params[:id])
  @topic.update_attribute(:excellent, 0)
  redirect_to @topic, success: '加精已经取消。'
end

#updateObject


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/topics_controller.rb', line 151

def update
  @topic = Topic.find(params[:id])
  @topic.admin_editing = true if current_user.admin?

  if can?(:change_node, @topic)
    # 锁定接点的时候,只有管理员可以修改节点
    @topic.node_id = topic_params[:node_id]

    if current_user.admin? && @topic.node_id_changed?
      # 当管理员修改节点的时候,锁定节点
      @topic.lock_node = true
    end
  end
  @topic.title = topic_params[:title]
  @topic.body = topic_params[:body]

  if @topic.save
    redirect_to(topic_path(@topic.id), notice: t('topics.update_topic_success'))
  else
    render action: 'edit'
  end
end