Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
用户页面缓存和组件化;
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Sep 15, 2011
1 parent 3959de3 commit 4c3d4b6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 42 deletions.
23 changes: 23 additions & 0 deletions app/cells/user/followed_topics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if !@followed_topics.blank? %>
<div class="box gray">
<h2>关注 <a href="<%= following_topics_user_path(@user.slug) %>"><%= @user.followed_topic_ids.size %>个话题</a></h2>
<ul class="followed-item topic-listing">
<% for topic in @followed_topics %>
<li>
<div class="item">
<a href="<%= topic_path(topic.name) %>"><%= topic.name %></a>
</div>
<% if @current_user %>
<div class="action">
<% if @current_user.topic_followed?(topic) %>
<a class="flat_button small" href="#" onclick="return Topics.unfollow(this, '<%= topic.name %>','small');">取消关注</a>
<% else %>
<a class="green_button small" href="#" onclick="return Topics.follow(this, '<%= topic.name %>','small');">关注</a>
<% end %>
</div>
<% end %>
</li>
<% end %>
</ul>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/cells/user/followers.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if [email protected]? %>
<div class="box standard gray">
<h2>最近关注<%= user_sex_title(@user) %>的人</h2>
<div class="inner">
<div class="followers">
<% for follower in @followers %>
<a href="/users/<%= follower.slug %>" title="<%= follower.name %>">
<%= user_avatar_tag(follower, :small) %>
</a>
<% end %>
</div>
</div>
</div>
<% end %>
15 changes: 15 additions & 0 deletions app/cells/user/following.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% if [email protected]? %>
<div class="box standard gray">
<h2><%= user_sex_title(@user) %>最近关注的人</h2>
<div class="inner">
<div class="followers">
<% for follower in @following %>
<a href="/users/<%= follower.slug %>" title="<%= follower.name %>">
<%= user_avatar_tag(follower, :small) %>
</a>
<% end %>
</div>
</div>
</div>
<% end %>

41 changes: 41 additions & 0 deletions app/cells/user_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class UserCell < Cell::Rails
helper :users
cache :followers, :followers_key, :expires_in => 3.days
cache :following, :following_key, :expires_in => 3.days

def followed_topics(opts = {})
@user = opts[:user] || nil
@current_user = opts[:current_user] || nil
if @user
@followed_topics = @user.followed_topics.desc("$natural").limit(7)
end
render
end

def followers(opts = {})
@user = opts[:user] || nil
if @user
@followers = @user.followers.desc("$natural").limit(42)
end
render
end

def following(opts = {})
@user = opts[:user] || nil
if @user
@following = @user.following.desc("$natural").limit(42)
end
render
end

private
def followers_key(opts = {})
return "followers/nil" if opts[:user].blank?
"followers/#{opts[:user].id}/#{opts[:user].follower_ids.to_s.md5}"
end

def following_key(opts = {})
return "following/nil" if opts[:user].blank?
"following/#{opts[:user].id}/#{opts[:user].following_ids.to_s.md5}"
end
end
3 changes: 0 additions & 3 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def user_tagline_tag(user,options = {})

def user_sex_title(user)
return "" if user.blank?
if current_user
return "我" if user.id == current_user.id
end
user.girl.blank? == true ? "他" : "她"
end

Expand Down
42 changes: 3 additions & 39 deletions app/views/users/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,9 @@
<div class="user-regdate">注册于 <%= l user.created_at.to_date, :format => :long %></div>
</div>

<% if @user.followed_topic_ids.size > 0 %>
<div class="box gray">
<h2>关注 <a href="<%= following_topics_user_path(@user.slug) %>"><%= @user.followed_topic_ids.size %>个话题</a></h2>
<ul class="followed-item topic-listing">
<% for topic in @user.followed_topics.desc("$natural").limit(7) %>
<%= render "topic", :topic => topic %>
<% end %>
</ul>
</div>
<% end %>

<% if @user.follower_ids.size > 0 %>
<div class="box standard gray">
<h2>最近关注<%= user_sex_title(@user) %>的人</h2>
<div class="inner">
<div class="followers">
<% for follower in @user.followers.desc("$natural").limit(42) %>
<a href="/users/<%= follower.slug %>" title="<%= follower.name %>">
<%= user_avatar_tag(follower, :small) %>
</a>
<% end %>
</div>
</div>
</div>
<% end %>
<%= render_cell :user, :followed_topics, :user => @user, :current_user => current_user %>
<%= render_cell :user, :followers, :user => @user %>
<%= render_cell :user, :following, :user => @user %>

<% if @user.following_ids.size > 0 %>
<div class="box standard gray">
<h2><%= user_sex_title(@user) %>最近关注的人</h2>
<div class="inner">
<div class="followers">
<% for follower in @user.following.desc("$natural").limit(42) %>
<a href="/users/<%= follower.slug %>" title="<%= follower.name %>">
<%= user_avatar_tag(follower, :small) %>
</a>
<% end %>
</div>
</div>
</div>
<% end %>

<% end %>

0 comments on commit 4c3d4b6

Please sign in to comment.