Skip to content

Commit

Permalink
List all Replies on user profile page.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Oct 7, 2015
1 parent 05a23ca commit 6535411
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 32 deletions.
15 changes: 10 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'will_paginate/array'
class UsersController < ApplicationController
before_action :require_user, only: [:block, :unblock, :auth_unbind, :follow, :unfollow]
before_action :find_user, only: [:show, :topics, :favorites, :notes,
before_action :find_user, only: [:show, :topics, :replies, :favorites, :notes,
:block, :unblock, :blocked,
:follow, :unfollow, :followers, :following]
caches_action :index, expires_in: 2.hours, layout: false
Expand All @@ -16,17 +16,22 @@ def show
# 排除掉几个非技术的节点
without_node_ids = [21, 22, 23, 31, 49, 51, 57, 25]
@topics = @user.topics.fields_for_list.without_node_ids(without_node_ids).high_likes.limit(20)
@replies = @user.replies.only(:topic_id, :body_html, :created_at).recent.includes(:topic).limit(10)
@replies = @user.replies.fields_for_list.recent.includes(:topic).limit(10)
set_seo_meta("#{@user.login}")
end

def topics
@topics = @user.topics.unscoped.fields_for_list.recent.paginate(page: params[:page], per_page: 30)
@topics = @user.topics.unscoped.fields_for_list.recent.paginate(page: params[:page], per_page: 40)
set_seo_meta("#{@user.login} 的帖子")
end

def replies
@replies = @user.replies.fields_for_list.recent.paginate(page: params[:page], per_page: 20)
set_seo_meta("#{@user.login} 的帖子")
end

def favorites
@topic_ids = @user.favorite_topic_ids.reverse.paginate(page: params[:page], per_page: 30)
@topic_ids = @user.favorite_topic_ids.reverse.paginate(page: params[:page], per_page: 40)
@topics = Topic.where(:_id.in => @topic_ids).fields_for_list
@topics = @topics.to_a.sort do |a, b|
@topic_ids.index(a.id) <=> @topic_ids.index(b.id)
Expand All @@ -35,7 +40,7 @@ def favorites
end

def notes
@notes = @user.notes.published.recent.paginate(page: params[:page], per_page: 30)
@notes = @user.notes.published.recent.paginate(page: params[:page], per_page: 40)
set_seo_meta("#{@user.login} 的记事本")
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Reply
delegate :title, to: :topic, prefix: true, allow_nil: true
delegate :login, to: :user, prefix: true, allow_nil: true

scope :fields_for_list, -> { only(:topic_id, :_id, :body_html, :updated_at, :created_at) }

validates :body, presence: true
validates :body, uniqueness: { scope: [:topic_id, :user_id], message: '不能重复提交。' }
validate do
Expand Down
7 changes: 4 additions & 3 deletions app/views/users/_menu.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<%= render_list class: "nav nav-tabs" do |li|
li << link_to(t("users.menu.profile"), user_path(@user))
li << link_to(t("users.menu.topics"), topics_user_path(@user))
li << link_to(t("users.menu.replies"), replies_user_path(@user))
li << link_to(t("users.menu.favorites"), favorites_user_path(@user))
li << link_to(t("users.menu.notes"), notes_user_path(@user))
li << link_to("关注者", followers_user_path(@user))
li << link_to("正在关注", following_user_path(@user))
li << link_to("已屏蔽", blocked_user_path(@user)) if owner?(@user) and current_user.blocked_users?
li << link_to(t("users.menu.following"), followers_user_path(@user))
li << link_to(t("users.menu.followers"), following_user_path(@user))
li << link_to(t("users.menu.blocked"), blocked_user_path(@user)) if owner?(@user) and current_user.blocked_users?
end %>
24 changes: 13 additions & 11 deletions app/views/users/_replies.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<ul class="list-group">
<ul class="list-group recent-replies">
<% replies.each do |reply| %>
<% next if reply.topic.blank? %>
<li class="list-group-item">
<div class="title">
<%= link_to(reply.topic.title, topic_path(reply.topic_id)) %>
<span class="info">at <%= timeago(reply.created_at) %></span>
</div>
<div class="body markdown">
<%= raw reply.body_html %>
</div>
</li>
<% cache(['users', reply]) do %>
<% next if reply.topic.blank? %>
<li class="list-group-item">
<div class="title">
<%= link_to(reply.topic.title, topic_path(reply.topic_id)) %>
<span class="info">at <%= timeago(reply.created_at) %></span>
</div>
<div class="body markdown">
<%= raw reply.body_html %>
</div>
</li>
<% end %>
<% end %>
</ul>
12 changes: 6 additions & 6 deletions app/views/users/_topics.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<td class="replies"><%=t("common.replies_count")%>/<%= t("common.likes_count") %></td>
</tr>
<% topics.each do |topic| %>
<tr class="topic <%= cycle("odd","") %> <%= 'deleted' if topic.deleted? %>">
<td class="node"><%= render_node_name(topic.node_name,topic.node_id) %></td>
<td class="title"><%= link_to(topic.title, topic_path(topic)) %> <%= topic_excellent_tag(topic) %> <em><%= timeago(topic.created_at) %></td>
<td class="replies"><%= topic.replies_count %>/<%= topic.likes_count %> </td>
</tr>
<tr class="topic <%= cycle("odd","") %> <%= 'deleted' if topic.deleted? %>">
<td class="node"><%= render_node_name(topic.node_name,topic.node_id) %></td>
<td class="title"><%= link_to(topic.title, topic_path(topic)) %> <%= topic_excellent_tag(topic) %> <em><%= timeago(topic.created_at) %></td>
<td class="replies"><%= topic.replies_count %>/<%= topic.likes_count %> </td>
</tr>
<% end %>
</table>
</table>
4 changes: 2 additions & 2 deletions app/views/users/notes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
</div>
</div>
</div>
</div>

</div>
16 changes: 16 additions & 0 deletions app/views/users/replies.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="row">
<%= render "sidebar", user: @user %>

<div class="col-md-8">
<%= render "menu" %>
<div class="panel panel-default">
<div class="panel-body">
<%= render "replies", replies: @replies %>
</div>
<div class="panel-footer">
<%= will_paginate @replies %>
</div>
</div>
</div>

</div>
6 changes: 3 additions & 3 deletions app/views/users/topics.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row">
<%= render "sidebar", user: @user %>

<div class="col-md-8">
<%= render "menu" %>
<div class="panel panel-default">
Expand All @@ -12,5 +12,5 @@
</div>
</div>
</div>
</div>

</div>
11 changes: 11 additions & 0 deletions config/locales/users.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@
hot_users: "Top 100 active users"
recent_joined: "Most recently joined users"
edit_on_gravatar: "Change you avatar you can go:"
menu:
topics: "Topics"
recent_reply_topics: "Recent replies"
like: "Likes"
profile: "Profile"
favorites: "Favorites"
notes: "Notes"
replies: "Replies"
following: "Following"
followers: "Followers"
blocked: "Blocked"
4 changes: 4 additions & 0 deletions config/locales/users.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
profile: "个人信息"
favorites: "收藏"
notes: "记事本"
replies: "回帖"
following: "正在关注"
followers: "关注者"
blocked: "已屏蔽"

8 changes: 6 additions & 2 deletions config/locales/users.zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
recent_joined: "最新加入的會員"
edit_on_gravatar: "如需修改頭像,請進入"
menu:
topics: "文章"
topics: "文章"
replies: "回帖"
recent_reply_topics: "最近回應"
like: "喜歡"
favorites: "收藏"

replies: "回帖"
following: "正在關注"
followers: "關注者"
blocked: "已屏蔽"
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
get 'topics/node:id' => 'topics#node', as: 'node_topics'
get 'topics/node:id/feed' => 'topics#node_feed', as: 'feed_node_topics', defaults: { format: 'xml' }
get 'topics/last' => 'topics#recent', as: 'recent_topics'

resources :topics do
member do
post :reply
Expand Down Expand Up @@ -127,9 +128,11 @@
# 比如 http://ruby-china.org/huacnlee
get 'users/city/:id' => 'users#city', as: 'location_users'
get 'users' => 'users#index', as: 'users'

resources :users, path: '' do
member do
get :topics
get :replies
get :favorites
get :notes
get :blocked
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
end
end

describe ':replies' do
it 'should show user replies' do
get :replies, id: user.login
expect(response).to be_success
end
end

describe ':favorites' do
it 'should show user liked stuffs' do
get :favorites, id: user.login
Expand Down

0 comments on commit 6535411

Please sign in to comment.