Skip to content

Commit

Permalink
Merge with server
Browse files Browse the repository at this point in the history
  • Loading branch information
nowa committed Apr 20, 2011
2 parents 2888347 + bda13dd commit baced99
Show file tree
Hide file tree
Showing 91 changed files with 2,598 additions and 735 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -7,6 +7,7 @@ gem "bson", "1.2.4"
gem "bson_ext", "1.2.4"
gem 'mongo-rails-instrumentation'
gem "mongoid-eager-loading"
# gem "mongoid-sphinx-huacnlee", :path => "/Users/jason/work/mongoid-sphinx", :require => "mongoid_sphinx"
gem "mongoid-sphinx-huacnlee", :require => "mongoid_sphinx"
gem "mongoid_rails_migrations"

Expand Down Expand Up @@ -68,4 +69,4 @@ gem "resque"
gem "resque_mailer"

# Comet
gem "juggernaut"
gem "juggernaut"
19 changes: 16 additions & 3 deletions app/controllers/application_controller.rb
Expand Up @@ -2,7 +2,20 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper :all
before_filter :load_notice
before_filter :init, :load_notice
has_mobile_fu

def init
if params[:force_format] == "mobile"
cookies[:mobile] = true
elsif params[:force_format] == "desktop"
cookies[:mobile] = nil
end

if !cookies[:mobile].blank? and request.format.to_sym == :html
force_mobile_format
end
end

def load_notice
@notice = Notice.last
Expand All @@ -14,8 +27,8 @@ def load_notice
end

# 暂时不使用mobile-fu的功能,仅仅使用其is_mobile_device?方法
include ActionController::MobileFu::InstanceMethods
helper_method :is_mobile_device?
#include ActionController::MobileFu::InstanceMethods
#helper_method :is_mobile_device?

# Comet Server
use_zomet
Expand Down
22 changes: 20 additions & 2 deletions app/controllers/asks_controller.rb
Expand Up @@ -6,8 +6,7 @@ class AsksController < ApplicationController

def index
@per_page = 20
@asks = Ask.normal.recent.includes(:user,:topics)
.paginate(:page => params[:page], :per_page => @per_page)
@asks = Ask.normal.recent.includes(:user).paginate(:page => params[:page], :per_page => @per_page)
set_seo_meta("所有问题")
end

Expand Down Expand Up @@ -73,6 +72,25 @@ def redirect
end
end

def share
@ask = Ask.find(params[:id])
if request.get?
if current_user
render "share", :layout => false
else
render_404
end
else
case params[:type]
when "email"
UserMailer.simple(params[:to], params[:subject], params[:body].gsub("\n","<br />")).deliver
@success = true
@msg = "已经将问题连接发送到了 #{params[:to]}"
end
end

end

def answer
@answer = Answer.new(params[:answer])
@answer.ask_id = params[:id]
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/cpanel/answers_controller.rb
Expand Up @@ -2,9 +2,7 @@
class Cpanel::AnswersController < CpanelController

def index
@answers = initialize_grid(Answer,
:order => 'id',
:order_direction => 'desc')
@answers = Answer.includes([:user]).desc("created_at").paginate(:page => params[:page], :per_page => 20)

respond_to do |format|
format.html # index.html.erb
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/cpanel/asks_controller.rb
Expand Up @@ -2,9 +2,7 @@
class Cpanel::AsksController < CpanelController

def index
@asks = initialize_grid(Ask,
:order => 'id',
:order_direction => 'desc')
@asks = Ask.includes([:user]).desc("created_at").paginate(:page => params[:page], :per_page => 40)

respond_to do |format|
format.html # index.html.erb
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/cpanel/comments_controller.rb
Expand Up @@ -2,9 +2,7 @@
class Cpanel::CommentsController < CpanelController

def index
@comments = initialize_grid(Comment,
:order => 'id',
:order_direction => 'desc')
@comments = Comment.desc("created_at").paginate(:page => params[:page], :per_page => 40)

respond_to do |format|
format.html # index.html.erb
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/cpanel/topics_controller.rb
Expand Up @@ -2,9 +2,7 @@
class Cpanel::TopicsController < CpanelController

def index
@topics = initialize_grid(Topic,
:order => 'id',
:order_direction => 'desc')
@topics = Topic.desc("created_at").paginate(:page => params[:page], :per_page => 40)

respond_to do |format|
format.html # index.html.erb
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/cpanel/users_controller.rb
Expand Up @@ -2,9 +2,7 @@
class Cpanel::UsersController < CpanelController

def index
@users = initialize_grid(User,
:order => 'id',
:order_direction => 'desc')
@users = User.desc("created_at").paginate(:page => params[:page], :per_page => 40)

respond_to do |format|
format.html # index.html.erb
Expand Down
68 changes: 54 additions & 14 deletions app/controllers/search_controller.rb
Expand Up @@ -2,52 +2,92 @@
class SearchController < ApplicationController

def index
if params[:format] == "json"
result = Search.query(params["w"].to_s.strip,:limit => 10)
render :json => result.to_json
else
@asks = Ask.search_title(params["w"].to_s.strip,:limit => 20)
set_seo_meta("关于“#{params[:w]}”的搜索结果")
render "/asks/index"
@asks = Ask.search_title(params["w"].to_s.strip,:limit => 20)
set_seo_meta("关于“#{params[:w]}”的搜索结果")
render "/asks/index"
end

def all
result = Search.query(params[:q].strip,:limit => 10)
lines = []
result.each do |item|
case item['type']
when "Ask"
lines << complete_line_ask(item)
when "User"
lines << complete_line_user(item)
when "Topic"
lines << complete_line_topic(item)
end
end
render :text => lines.join("\n")
end

def topics
result = Search.query(params[:q],:type => "Topic",:limit => 10)
result = Search.complete(params[:q].strip,:type => "Topic",:limit => 10)
if params[:format] == "json"
render :json => result.to_json
lines = []
result.each do |item|
lines << complete_line_topic(item)
end
render :text => lines.join("\n")
else
lines = []
result.each do |item|
lines << item['title']
lines << complete_line_topic(item)
end
render :text => lines.join("\n")
end
end

def asks
result = Search.query(params[:q],:type => "Ask",:limit => 10)
result = Search.query(params[:q].strip,:type => "Ask",:limit => 10)
if params[:format] == "json"
render :json => result.to_json
else
lines = []
result.each do |item|
lines << "#{item['title']}#!##{item['id']}"
lines << complete_line_ask(item)
end
render :text => lines.join("\n")
end
end

def users
result = Search.query(params[:q],:type => "User",:limit => 10)
result = Search.complete(params[:q],:type => "User",:limit => 10)
if params[:format] == "json"
render :json => result.to_json
else
lines = []
result.each do |item|
lines << "#{item['title']}#!##{item['id']}#!##{item['tagline']}#!##{item['avatar_small']}#!##{item['slug']}"
lines << complete_line_user(item)
end
render :text => lines.join("\n")
end
end

private
def complete_line_ask(item,hash = true)
if hash
"#{item['title']}#!##{item['id']}#!##{item['topics'].join(',')}#!#Ask"
else
"#{item.title.gsub("\n",'')}#!##{item.id}#!##{item.topics.join(',')}#!#Ask"
end
end

def complete_line_topic(item,hash = true)
if hash
"#{item['title']}#!##{item['followers_count']}#!##{item['cover_small']}#!#Topic"
else
"#{item.name}#!##{item.followers_count}#!##{item.cover_small}#!#Topic"
end
end

def complete_line_user(item,hash = true)
if hash
"#{item['title']}#!##{item['id']}#!##{item['tagline']}#!##{item['avatar_small']}#!##{item['slug']}#!#User"
else
"#{item.name}#!##{item.id}#!##{item.tagline}#!##{item.avatar_small}#!##{item.slug}#!#User"
end
end
end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -110,6 +110,7 @@ def auth_callback
auth = request.env["omniauth.auth"]
redirect_to root_path if auth.blank?
provider_name = auth['provider'].gsub(/^t/,"").titleize
Rails.logger.debug { auth }

if current_user
Authorization.create_from_hash(auth, current_user)
Expand Down
6 changes: 6 additions & 0 deletions app/mailers/user_mailer.rb
Expand Up @@ -61,5 +61,11 @@ def ask_user(ask_id)
mail(:to => @user.email, :subject => @title, :from => Setting.email_sender)
end

def simple(to, subject, content)
@title = subject
@content = content
mail(:to => to, :subject => @title, :from => Setting.email_sender)
end


end
14 changes: 9 additions & 5 deletions app/models/ask.rb
Expand Up @@ -51,16 +51,16 @@ class Ask
# 正常可显示的问题, 前台调用都带上这个过滤
scope :normal, where(:spams_count.lt => Setting.ask_spam_max)
scope :last_actived, desc(:answered_at)
scope :recent, desc("$natural")
scope :recent, desc("created_at")
# 除开一些 id,如用到 mute 的问题,传入用户的 muted_ask_ids
scope :exclude_ids, lambda { |id_array| not_in("_id" => (id_array ||= [])) }
scope :only_ids, lambda { |id_array| any_in("_id" => (id_array ||= [])) }
# 问我的问题
scope :asked_to, lambda { |to_user_id| where(:to_user_id => to_user_id) }

# FullText indexes
search_index(:fields => [:title,:body, :topics],
:attributes => [],
search_index(:fields => [:title,:topics],
:attributes => [:title,:topics],
:options => {} )

redis_search_index(:title_field => :title,:ext_fields => [:topics])
Expand Down Expand Up @@ -180,9 +180,13 @@ def self.mmseg_text(text)
result = Ask.search(text,:max_matches => 1)
words = []
result.raw_result[:words].each do |w|
next if w[0] == "ask"
words << ((w[0] == "rubi" and text.downcase.index("ruby")) ? "ruby" : w[0])
t = w[0].dup.force_encoding("utf-8")
next if t == "ask"
words << ((t == "rubi" and text.downcase.index("ruby")) ? "ruby" : t )
end
# 修正顺序
words = words.sort { |x,y| (text.index(x) || -1) <=> (text.index(y) || -1) }
Rails.logger.debug { "mmseg:#{words}" }
words
end

Expand Down
9 changes: 6 additions & 3 deletions app/models/base_model.rb
Expand Up @@ -15,7 +15,6 @@ def spam?(attr)
return false
end
end

end

module ClassMethods
Expand Down Expand Up @@ -49,12 +48,16 @@ def remove_search_index
def update_search_index
index_fields_changed = false
#{ext_fields}.each do |f|
next if f.to_s == "id"
if instance_eval(f.to_s + "_changed?")
index_fields_changed = true
end
end
if(self.#{title_field}_changed?)
index_fields_changed = true
begin
if(self.#{title_field}_changed?)
index_fields_changed = true
end
rescue
end
if index_fields_changed
Rails.logger.debug { "-- update_search_index --" }
Expand Down

0 comments on commit baced99

Please sign in to comment.