Permalink
Browse files

加入 cells,并将相关问题和关注问题的人抽象为 cell;

加入 dalli 作为 memcached 接口,并缓存相关问题和关注问题的人;
  • Loading branch information...
1 parent 4ce9cfe commit cd1681df180d82623bbfe95dcac1f332f282b3bf @huacnlee committed Sep 15, 2011
View
2 Gemfile
@@ -8,6 +8,8 @@ gem "bson_ext", "1.2.4"
gem 'mongo-rails-instrumentation', "0.2.0"
gem "mongoid-eager-loading","0.3.1"
gem "mongoid_rails_migrations", "0.0.10"
+gem "cells","3.6.5"
+gem "dalli","1.1.2"
gem "redis", "2.1.1"
View
6 Gemfile.lock
@@ -51,7 +51,11 @@ GEM
builder (2.1.2)
carrierwave (0.5.3)
activesupport (~> 3.0)
+ cells (3.6.5)
+ actionpack (~> 3.0)
+ railties (~> 3.0)
colorize (0.5.8)
+ dalli (1.1.2)
devise (1.2.rc2)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
@@ -219,7 +223,9 @@ DEPENDENCIES
bson (= 1.2.4)
bson_ext (= 1.2.4)
carrierwave (= 0.5.3)
+ cells (= 3.6.5)
colorize (= 0.5.8)
+ dalli (= 1.1.2)
devise (= 1.2.rc2)
devise_invitable (= 0.4.rc5)
htmldiff!
View
7 app/cells/ask/followers.html.erb
@@ -0,0 +1,7 @@
+<% if !@followers.blank? %>
+ <div class="followers">
+ <% for item in @followers %>
+ <%= user_avatar_tag(item,:small) %>
+ <% end %>
+ </div>
+<% end %>
View
17 app/cells/ask/relation_asks.html.erb
@@ -0,0 +1,17 @@
+<% if !@relation_asks.blank? %>
+ <div id="relation_asks" class="box standard">
+ <h2>相关问题</h2>
+ <div class="inner">
+ <ul>
+ <% @relation_asks.each_with_index do |ask,i| %>
+ <li <%= "style='display:none;'" if i > 4 %>><a href="/asks/<%= ask.id %>"><%= ask.title %></a></li>
+ <% end %>
+ <% if @relation_asks.count > 4 %>
+ <li class="more">
+ <a href="#" onclick="$(this).parent().parent().find('li').show();$(this).hide();return false;">查看更多相关问题</a>
+ </li>
+ <% end %>
+ <ul>
+ </div>
+ </div>
+<% end %>
View
30 app/cells/ask_cell.rb
@@ -0,0 +1,30 @@
+class AskCell < Cell::Rails
+ helper :users
+ cache :relation_asks, :relation_asks_key, :expires_in => 12.hours
+ cache :followers, :followers_key
+
+ def relation_asks(opts = {})
+ @ask = opts[:ask] || nil
+ if @ask
+ @relation_asks = Ask.normal.any_in(:topics => @ask.topics).excludes(:id => @ask.id).limit(10).desc("$natural")
+ end
+ render
+ end
+
+ def followers(opts = {})
+ @ask = opts[:ask] || nil
+ @followers = @ask.followers
+ render
+ end
+
+ private
+ def relation_asks_key(opts = {})
+ return "relation_asks/nil" if opts[:ask].blank?
+ "relation_asks/#{opts[:ask].id}"
+ end
+
+ def followers_key(opts = {})
+ return "followers/nil" if opts[:ask].blank?
+ "followers/#{opts[:ask].id}/#{opts[:ask].followers.to_s.md5}"
+ end
+end
View
1 app/controllers/asks_controller.rb
@@ -40,7 +40,6 @@ def show
# 由于 voteable_mongoid 目前的按 votes_point 排序有问题,没投过票的无法排序
@answers = @ask.answers.includes(:user).order_by(:"votes.uc".desc,:"votes.dc".asc,:"created_at".asc)
@answer = Answer.new
- @relation_asks = Ask.normal.any_in(:topics => @ask.topics).excludes(:id => @ask.id).limit(10).desc("$natural")
# 被邀请回答的用户
@invites = @ask.ask_invites.includes(:user)
# 推荐话题,如果没有设置话题的话
View
2 app/controllers/gridfs_controller.rb
@@ -1,15 +1,13 @@
require 'mongo'
class GridfsController < ActionController::Metal
def serve
- Rails.logger.debug { "ENV: #{env['PATH_INFO']}" }
gridfs_path = env["PATH_INFO"].gsub("/uploads/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
rescue => e
self.status = :file_not_found
- Rails.logger.debug { "#{e}" }
self.content_type = 'text/plain'
self.response_body = ''
end
View
3 app/models/topic.rb
@@ -13,6 +13,9 @@ class Topic
field :asks_count, :type => Integer, :default => 0
index :name
+ index :follower_ids
+ index :asks_count
+
has_many :logs, :class_name => "Log", :foreign_key => "target_id"
# Followers
View
22 app/views/asks/show.html.erb
@@ -168,21 +168,7 @@
</div>
- <div id="relation_asks" class="box standard">
- <h2>相关问题</h2>
- <div class="inner">
- <ul>
- <% @relation_asks.each_with_index do |ask,i| %>
- <li <%= "style='display:none;'" if i > 4 %>><a href="/asks/<%= ask.id %>"><%= ask.title %></a></li>
- <% end %>
- <% if @relation_asks.count > 4 %>
- <li class="more">
- <a href="#" onclick="$(this).parent().parent().find('li').show();$(this).hide();return false;">查看更多相关问题</a>
- </li>
- <% end %>
- <ul>
- </div>
- </div>
+ <%= render_cell :ask, :relation_asks, :ask => @ask %>
<% if current_user %>
<div id="share_ask_box" class="box standard">
@@ -221,11 +207,7 @@
<% if @ask.user %>
<p><%= user_name_tag(@ask.user) %> 创建了该问题</p>
<% end %>
- <div class="followers">
- <% for item in @ask.followers %>
- <%= user_avatar_tag(item,:small) %>
- <% end %>
- </div>
+ <%= render_cell :ask, :followers, :ask => @ask %>
</div>
</div>
<% end %>
View
4 config/environments/development.rb
@@ -12,7 +12,7 @@
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
- config.action_controller.perform_caching = false
+ config.action_controller.perform_caching = true
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
@@ -22,5 +22,7 @@
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
+
+ config.cache_store = :dalli_store, '127.0.0.1:11211',{ :namespace => "quora", :compress => true }
end
View
2 config/environments/production.rb
@@ -46,4 +46,6 @@
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
+
+ config.cache_store = :dalli_store, '127.0.0.1:11211',{ :namespace => "quora", :compress => true }
end
View
4 lib/string_extensions.rb
@@ -27,6 +27,10 @@ def escape_javascript
''
end
end
+
+ def md5
+ Digest::SHA1.hexdigest(self)
+ end
end
String.send :include,StringExtensions
View
10 test/cells/ask_cell_test.rb
@@ -0,0 +1,10 @@
+require 'test_helper'
+
+class AskCellTest < Cell::TestCase
+ test "followers" do
+ invoke :followers
+ assert_select "p"
+ end
+
+
+end

0 comments on commit cd1681d

Please sign in to comment.