Skip to content

Commit

Permalink
Merge branch 'master' into sphinx_search
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 15, 2011
2 parents e759da3 + cf88f82 commit c9f1ed8
Show file tree
Hide file tree
Showing 40 changed files with 468 additions and 75,940 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,9 +7,11 @@ tmp/
config/*.yml
public/javascripts/cached_*
public/stylesheets/cached_*
public/google*.html
public/about.html
db/sphinx
db/dict/*.lib
db/dict/thesaurus.txt

#以bundle install --path vendor/bundle方式安装,不影响开发者现有gems环境
vendor/bundle
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -2,6 +2,16 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper :all
before_filter :load_notice

def load_notice
@notice = Notice.last
if !@notice.blank? and !@notice.end_at.blank?
if @notice.end_at < Time.now
@notice = nil
end
end
end

# 暂时不使用mobile-fu的功能,仅仅使用其is_mobile_device?方法
include ActionController::MobileFu::InstanceMethods
Expand Down
75 changes: 75 additions & 0 deletions app/controllers/cpanel/notices_controller.rb
@@ -0,0 +1,75 @@
# coding: UTF-8
class Cpanel::NoticesController < CpanelController

def index
@notices = initialize_grid(Notice,
:order => 'id',
:order_direction => 'desc')

respond_to do |format|
format.html # index.html.erb
format.json
end
end

def show
@notice = Notice.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json
end
end

def new
@notice = Notice.new
@notice.end_at = Time.now + 1.days

respond_to do |format|
format.html # new.html.erb
format.json
end
end

def edit
@notice = Notice.find(params[:id])
end

def create
@notice = Notice.new(params[:notice])

respond_to do |format|
if @notice.save
format.html { redirect_to(cpanel_notices_path, :notice => 'Notice 创建成功。') }
format.json
else
format.html { render :action => "new" }
format.json
end
end
end

def update
@notice = Notice.find(params[:id])

respond_to do |format|
if @notice.update_attributes(params[:notice])
format.html { redirect_to(cpanel_notices_path, :notice => 'Notice 更新成功。') }
format.json
else
format.html { render :action => "edit" }
format.json
end
end
end

def destroy
@notice = Notice.find(params[:id])
@notice.destroy

respond_to do |format|
format.html { redirect_to(cpanel_notices_path,:notice => "删除成功。") }
format.json
end
end
end
18 changes: 15 additions & 3 deletions app/controllers/users_controller.rb
Expand Up @@ -20,6 +20,18 @@ def answered
render "/users/answered_asks.js"
end
end

def asked_to
@per_page = 10
@asks = Ask.normal.recent.asked_to(@user.id)
.paginate(:page => params[:page], :per_page => @per_page)
set_seo_meta("问#{@user.name}的问题")
if params[:format] == "js"
render "/asks/index.js"
else
render "asked"
end
end

def show
@per_page = 10
Expand All @@ -42,7 +54,7 @@ def asked
end

def following_topics
@per_page = 10
@per_page = 20
@topics = @user.followed_topics.desc("$natural")
.paginate(:page => params[:page], :per_page => @per_page)

Expand All @@ -53,7 +65,7 @@ def following_topics
end

def followers
@per_page = 10
@per_page = 20
@followers = @user.followers.desc("$natural")
.paginate(:page => params[:page], :per_page => @per_page)

Expand All @@ -64,7 +76,7 @@ def followers
end

def following
@per_page = 10
@per_page = 20
@followers = @user.following.desc("$natural")
.paginate(:page => params[:page], :per_page => @per_page)

Expand Down
15 changes: 9 additions & 6 deletions app/helpers/application_helper.rb
Expand Up @@ -11,8 +11,11 @@ def use_yahei_font?(ua)
return use
end

def ask_notification_tag(ask_id, log, a, show_ask = true)
def ask_notification_tag(ask_id, notify, show_ask = true)
return if ask_id.nil?

log = notify.log
a = notify.action
tag = ""
ask = Ask.find(ask_id)
return "" if ask.nil? or log.user.nil?
Expand All @@ -22,29 +25,29 @@ def ask_notification_tag(ask_id, log, a, show_ask = true)
case a
when "AGREE_ANSWER", "NEW_ANSWER_COMMENT"
tag += user_tag + " #{a == "AGREE_ANSWER" ? "赞成" : "评论"}了你在"
ask_tag = "<a href=\"#{ask_path(ask)}#{a == "AGREE_ANSWER" ? "#answer_" + log.target_id.to_s : "?eawc=yes&awid=" + log.title.to_s + "#answer_" + log.title.to_s}\">#{show_ask ? ask.title : "该问题中的回答。"}</a>" + (show_ask ? " 中的回答。" : "")
ask_tag = "<a onclick=\"mark_notifies_as_read(this, '#{notify.id}');\" href=\"#{ask_path(ask)}#{a == "AGREE_ANSWER" ? "#answer_" + log.target_id.to_s : "?eawc=yes&awid=" + log.title.to_s + "#answer_" + log.title.to_s}\">#{show_ask ? ask.title : "该问题中的回答。"}</a>" + (show_ask ? " 中的回答。" : "")
tag += (show_ask ? "问题 " : "") + ask_tag
when "NEW_ANSWER", "NEW_ASK_COMMENT"
tag += user_tag + " #{a == "NEW_ANSWER" ? "回答" : "评论"}了"
ask_tag = "<a href=\"#{ask_path(ask)}#{a == "NEW_ASK_COMMENT" ? "?easc=yes&asid=" + log.target_parent_id.to_s : ""}#answer_#{log.target_id.to_s}\">#{show_ask ? ask.title : "该问题。"}</a>"
ask_tag = "<a onclick=\"mark_notifies_as_read(this, '#{notify.id}');\" href=\"#{ask_path(ask)}#{a == "NEW_ASK_COMMENT" ? "?easc=yes&asid=" + log.target_parent_id.to_s : ""}#answer_#{log.target_id.to_s}\">#{show_ask ? ask.title : "该问题。"}</a>"
tag += (show_ask ? "问题 " : "") + ask_tag
when "THANK_ANSWER"
tag += user_tag + "感谢了你"
if show_ask
ask_tag = "在 <a href=\"#{ask_path(ask)}?nr=1#answer_#{log.target_id.to_s}\">#{ask.title}</a> 的回答。"
ask_tag = "在 <a onclick=\"mark_notifies_as_read(this, '#{notify.id}');\" href=\"#{ask_path(ask)}?nr=1#answer_#{log.target_id.to_s}\">#{ask.title}</a> 的回答。"
else
ask_tag = "的回答。"
end
tag += ask_tag
when "INVITE_TO_ANSWER"
tag += user_tag + "邀请你回答 "
if show_ask
tag += "<a href=\"#{ask_path(ask)}?nr=1\">#{ask.title}</a>"
tag += "<a onclick=\"mark_notifies_as_read(this, '#{notify.id}');\" href=\"#{ask_path(ask)}?nr=1\">#{ask.title}</a>"
end
when "ASK_USER"
tag += user_tag + "向你询问 "
if show_ask
tag += "<a href=\"#{ask_path(ask)}?nr=1\">#{ask.title}</a>"
tag += "<a onclick=\"mark_notifies_as_read(this, '#{notify.id}');\" href=\"#{ask_path(ask)}?nr=1\">#{ask.title}</a>"
end
end
return tag
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/cpanel/notices_helper.rb
@@ -0,0 +1,2 @@
module Cpanel::NoticesHelper
end
2 changes: 1 addition & 1 deletion app/mailers/user_mailer.rb
Expand Up @@ -57,7 +57,7 @@ def invite_to_answer(ask_id, user_id, invitor_ids)
def ask_user(ask_id)
@ask = Ask.find(ask_id)
@user = @ask.to_user
@title = "#{@ask.user.name}对向你询问#{@ask.title}》"
@title = "#{@ask.user.name}向你询问#{@ask.title}》"
mail(:to => @user.email, :subject => @title, :from => Setting.email_sender)
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/ask.rb
Expand Up @@ -55,6 +55,8 @@ class Ask
# 除开一些 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,:topics],
Expand Down
8 changes: 8 additions & 0 deletions app/models/notice.rb
@@ -0,0 +1,8 @@
class Notice
include Mongoid::Document
include Mongoid::Timestamps

field :body
field :end_at, :type => DateTime

end
2 changes: 1 addition & 1 deletion app/views/asks/show.html.erb
Expand Up @@ -71,7 +71,7 @@
<% end %>
<a href="#new_answer" onclick="return $('#new_answer .qeditor_preview').focus();">添加答案</a>
<a href="#" onclick="return Asks.report(this);">举报</a>
<a class="spam<%= ' spamed' if spamed?(@ask) %>" onclick="return Asks.spamAsk(this,'<%= @ask.id %>');" title="问题被<%= Setting.ask_spam_max %>个网友不感兴趣后,问题将会隐藏。" href="#">不感兴趣</a>
<a class="spam<%= ' spamed' if spamed?(@ask) %>" onclick="return Asks.spamAsk(this,'<%= @ask.id %>');" title="问题被<%= Setting.ask_spam_max %>个网友认为是烂问题以后,问题将会隐藏。" href="#">烂问题</a>
<% if !@ask.spams_count.blank? %>(<span id="spams_count"><%= @ask.spams_count %></span>人认为)<% end %>
<span class="date"><%= l @ask.created_at.getlocal, :format => :short %></span>
</div>
Expand Down
15 changes: 15 additions & 0 deletions app/views/cpanel/notices/_base.html.erb
@@ -0,0 +1,15 @@
<%= content_for :base_sitemap do %>
<a href="<%= cpanel_notices_path %>">cpanel_notices</a> »
<% end %>
<%= content_for :styles do %>
<% end %>
<%= spaceless do %>
<div class="tools">
<a href="<%= cpanel_notices_path %>" class="button small left<%= ' checked' if params[:action] == "index" %>">查看所有</a>
<% if ["edit","update"].index(params[:action]) %>
<a href="#" class="button small checked">编辑</a>
<% end %>
<a href="<%= new_cpanel_notice_path %>" class="button small right<%= ' checked' if ["new","create"].index(params[:action]) %>">新建</a>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/views/cpanel/notices/_form.html.erb
@@ -0,0 +1,14 @@
<div id="cpanel_notice" class="form">
<%= simple_form_for(@notice,
:url => (@notice.created_at.blank? ? cpanel_notices_path : cpanel_notice_path(@notice.id)) ) do |f| %>

<div class="inputs">
<%= f.input :body, :as => :text %>
<%= f.input :end_at, :as => :string %>
</div>
<div class="actions">
<button type="submit">提交</button>
或者 <%= link_to '返回', cpanel_notices_path %>
</div>
<% end %>
</div>
6 changes: 6 additions & 0 deletions app/views/cpanel/notices/edit.html.erb
@@ -0,0 +1,6 @@
<%= content_for :sitemap do %>
<span class="current">修改</span>
<% end %>
<%= render 'base' %>
<%= render 'form' %>

23 changes: 23 additions & 0 deletions app/views/cpanel/notices/index.html.erb
@@ -0,0 +1,23 @@
<%= content_for :sitemap do %>
<span class="current">列表</span>
<% end %>
<%= render 'base' %>

<div id="cpanel_notices">
<%= grid(@notices, :show_filters => :always) do |g|
g.column :column_name => '', :attribute_name => '_id'
g.column :column_name => 'body', :attribute_name => 'body' do |c|
c.body if !c.body.blank?
end
g.column :column_name => 'end_at', :attribute_name => 'end_at' do |c|
c.end_at if !c.end_at.blank?
end
g.column :column_name => '创建时间', :attribute_name => 'created_at', :td_html_attrs => { :style => "width:120px;" }
g.column :td_html_attrs => { :style => "width:50px; text-align:right;" } do |c|
raw "#{link_to '修改', edit_cpanel_notice_path(c.id)}
#{link_to image_tag("wice_grid/delete.png", :style => "margin-bottom:-4px"), cpanel_notice_path(c.id), :method => :delete, :confirm => '确定要删除吗?'}"
end
end %>
</div>


5 changes: 5 additions & 0 deletions app/views/cpanel/notices/new.html.erb
@@ -0,0 +1,5 @@
<%= content_for :sitemap do %>
<span class="current">新建</span>
<% end %>
<%= render 'base' %>
<%= render 'form' %>
18 changes: 18 additions & 0 deletions app/views/cpanel/notices/show.html.erb
@@ -0,0 +1,18 @@
<%= content_for :sitemap do %>
<span class="current">查看</span>
<% end %>
<%= render 'base' %>
<div class="tools">
<%= link_to '修改', edit_cpanel_notice_path(@notice) %> |
<%= link_to '返回', cpanel_notices_path %>
</div>
<div id="cpanel_notice">
<p>
<b>Body:</b>
<%= @notice.body %>
</p>
<p>
<b>End at:</b>
<%= @notice.end_at %>
</p>
</div>
16 changes: 14 additions & 2 deletions app/views/layouts/application.html.erb
Expand Up @@ -16,7 +16,7 @@
<meta name="description" content="<%= @meta_description %>" />
<link rel="shortcut icon" href="/favicon.ico?v=003" />
<%= csrf_meta_tag %>
<%= javascript_include_tag "jquery.min","rails","facebox","jquery.jdialog","jquery.qeditor","jquery.autocomplete","jcaches","application","asks", :cache => "cached_application" %>
<%= javascript_include_tag "jquery.min","rails","facebox","jquery.jdialog","jquery.qeditor","jquery.autocomplete", "jquery.cookie" ,"jcaches","application","asks","topics","users", :cache => "cached_application" %>
<%= yield :scripts %>
<script type="text/javascript">
<% if current_user %>
Expand Down Expand Up @@ -57,6 +57,17 @@
<div id="main">
<div class="container">
<div class="left_wrapper">
<% if !@notice.blank? %>
<% if cookies[:hide_notice] != @notice.id.to_s %>
<div id="sys_notice">
<%= raw @notice.body %>
<div class="info">
<span class="date"><%= l @notice.updated_at.getlocal, :format => :long %></span>
<a href="#" class="gray_button small" onclick="return App.hideNotice('<%= @notice.id %>');">我知道了</a>
</div>
</div>
<% end %>
<% end %>
<% if !notice.blank? %>
<div class="notice_message"><%= notice %></div>
<script type="text/javascript">
Expand All @@ -81,6 +92,7 @@
<div class="left_wrapper">
<div class="links">
<a href="/about">关于者也</a>
<a href="/asks/4d95f33bfd503c3bf2000011">每天进步一点</a>
<a href="http://blog.zheye.org" target="_blank">博客</a>
<a href="/register">如何注册</a>
<a href="http://twitter.com/zheyecn" target="_blank">Twitter</a>
Expand Down Expand Up @@ -108,7 +120,7 @@
<div class="logo"></div>
<div class="text">加载中…</div>
</div>
<div style="width:0px;height;0px;overflow:hidden;">
<div style="width:0px;height:0px;overflow:hidden;">
<%= image_tag("loading.gif") %>
<div id="report_page">
<h2>举报内容</h2>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/cpanel.html.erb
Expand Up @@ -16,6 +16,7 @@
控制台 <a href="<%= root_path %>" class="out">回前台</a>
</td>
<td id="nav">
<a href="<%= cpanel_notices_path %>">公告</a> |
<a href="<%= cpanel_users_path %>">用户</a> |
<a href="<%= cpanel_asks_path %>">问题</a> |
<a href="<%= cpanel_answers_path %>">回答</a> |
Expand Down
4 changes: 3 additions & 1 deletion app/views/logs/_log.html.erb
Expand Up @@ -76,7 +76,9 @@ when "UserLog"
item = User.first(:conditions => { :id => log.target_id})
elsif ["AGREE"].include?(action)
answer = Answer.first(:conditions => {:id => log.target_id})
item = answer.ask
if !answer.blank?
item = answer.ask
end
elsif ["THANK_ANSWER"].include?(action)
item = Answer.first(:conditions => {:id => log.target_id})
end
Expand Down

0 comments on commit c9f1ed8

Please sign in to comment.