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

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 14, 2011
2 parents 183b555 + 83b0f98 commit 8dff887
Show file tree
Hide file tree
Showing 71 changed files with 76,943 additions and 127 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ public/javascripts/cached_*
public/stylesheets/cached_*
public/about.html
db/sphinx
db/dict/*.lib

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

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

# set page title, meta keywords, meta description
def set_seo_meta(title, options = {})
Expand Down Expand Up @@ -74,4 +78,34 @@ def require_user_text
require_user(:format => :text)
end

def tag_options(options, escape = true)
unless options.blank?
attrs = []
options.each_pair do |key, value|
if BOOLEAN_ATTRIBUTES.include?(key)
attrs << %(#{key}="#{key}") if value
elsif !value.nil?
final_value = value.is_a?(Array) ? value.join(" ") : value
final_value = html_escape(final_value) if escape
attrs << %(#{key}="#{final_value}")
end
end
" #{attrs.sort * ' '}".html_safe unless attrs.empty?
end
end

def tag(name, options = nil, open = false, escape = true)
"<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe
end

def simple_format(text, html_options={}, options={})
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p><br />#{start_tag}") # 2+ newline -> paragraph
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
text.insert 0, start_tag
text.html_safe.safe_concat("</p>")
end

end
2 changes: 2 additions & 0 deletions app/controllers/asks_controller.rb
Expand Up @@ -78,6 +78,8 @@ def answer
@answer.ask_id = params[:id]
@answer.user_id = current_user.id

@answer.body = simple_format(@answer.body.strip) if params[:did_editor_content_formatted] == "no"

if @answer.save
@success = true
else
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/cpanel/report_spams_controller.rb
@@ -0,0 +1,33 @@
# coding: UTF-8
class Cpanel::ReportSpamsController < CpanelController

def index
@report_spams = initialize_grid(ReportSpam,
:order => 'id',
:order_direction => 'desc')

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

def show
@report_spam = ReportSpam.find(params[:id])

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

def destroy
@report_spam = ReportSpam.find(params[:id])
@report_spam.destroy

respond_to do |format|
format.html { redirect_to(cpanel_report_spams_path,:notice => "删除成功。") }
format.json
end
end
end
15 changes: 14 additions & 1 deletion app/controllers/home_controller.rb
Expand Up @@ -16,6 +16,7 @@ def index
when "FOLLOW" then @notifies[notify.target_id][:type] = "USER"
when "THANK_ANSWER" then @notifies[notify.target_id][:type] = "THANK_ANSWER"
when "INVITE_TO_ANSWER" then @notifies[notify.target_id][:type] = "INVITE_TO_ANSWER"
when "NEW_TO_USER" then @notifies[notify.target_id][:type] = "ASK_USER"
else
@notifies[notify.target_id][:type] = "ASK"
end
Expand Down Expand Up @@ -62,7 +63,7 @@ def index
def newbie
ask_logs = Log.any_of({:_type => "AskLog"}, {:_type => "UserLog", :action.in => ["FOLLOW_ASK", "UNFOLLOW_ASK"]}).where(:created_at.gte => (Time.now - 12.hours))
answer_logs = Log.any_of({:_type => "AnswerLog"}, {:_type => "UserLog", :action => "AGREE"}).where(:created_at.gte => (Time.now - 12.hours))
@asks = Ask.any_of({:_id.in => ask_logs.map {|l| l.target_id}.uniq}, {:_id.in => answer_logs.map {|l| l.target_parent_id}.uniq}).order_by(:answers_count.asc, :views_count.asc)
@asks = Ask.normal.any_of({:_id.in => ask_logs.map {|l| l.target_id}.uniq}, {:_id.in => answer_logs.map {|l| l.target_parent_id}.uniq}).order_by(:answers_count.asc, :views_count.asc)
h = {}
# 将回答次数*topic,以加入回答次数
@hot_topics = @asks.inject([]) { |memo, ask|
Expand Down Expand Up @@ -140,6 +141,8 @@ def update_in_place
case klass
when "user" then return if current_user.id.to_s != id
end

params[:value] = simple_format(params[:value].to_s.strip) if params[:did_editor_content_formatted] == "no"

object = klass.camelize.constantize.find(id)
update_hash = {field => params[:value]}
Expand Down Expand Up @@ -172,4 +175,14 @@ def mark_notifies_as_read
end
end

def report
name = "访客"
if current_user
name = current_user.name
end
ReportSpam.add(params[:url],params[:desc],name)
flash[:notice] = "举报信息已经提交,谢谢你。"
redirect_to params[:url]
end

end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -8,6 +8,7 @@ def init_user
if @user.blank?
render_404
end
@ask_to_user = Ask.new
end

def answered
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -41,6 +41,11 @@ def ask_notification_tag(ask_id, log, a, show_ask = true)
if show_ask
tag += "<a 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>"
end
end
return tag
end
Expand Down
14 changes: 13 additions & 1 deletion app/helpers/asks_helper.rb
@@ -1,3 +1,4 @@
# coding: utf-8
module AsksHelper
def topics_name_tag(topics,limit = 20)
html = []
Expand All @@ -11,9 +12,20 @@ def topics_name_tag(topics,limit = 20)
return raw html.join("")
end

def ask_title_tag(ask, options = {})
class_name = options[:class] || ""
prefix = ""
if !ask.to_user.blank?
prefix = "#{ask.to_user.name}:"
end
raw "<a href=\"/asks/#{ask.id}\" class=\"#{class_name}\">#{prefix}#{ask.title}</a>"
end

def md_body(str)
return "" if str.blank?
str = sanitize(str,:tags => %w(strong b i u strike s ol ul li blockquote address br div), :attributes => %w(src))
# str = simple_format(str) if str =~ /\n/
# Rails.logger.info "str: #{str.inspect}"
str = sanitize(str,:tags => %w(strong b i u strike s ol ul li blockquote address br div p), :attributes => %w(src))
str = auto_link_urls(str,{:target => "_blank", :rel => "nofollow" }, {:limit => 80 })
return raw str
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/cpanel/report_spams_helper.rb
@@ -0,0 +1,2 @@
module Cpanel::ReportSpamsHelper
end
2 changes: 1 addition & 1 deletion app/helpers/in_place_edit_helper.rb
Expand Up @@ -15,7 +15,7 @@ def in_place_edit_tag(object, field, options={})
:'data-type' => options[:type],
:'data-text-id' => options[:text_id],
:'data-rich' => options[:rich],
:onclick => "return App.inPlaceEdit(this);",
:onclick => "return App.inPlaceEdit(this, {'is_mobile_device': #{is_mobile_device? ? 'true' : 'false'}});",
:'data-url' => update_url}
if !options[:width].blank?
html_options[:"data-width"] = options[:width]
Expand Down
8 changes: 6 additions & 2 deletions app/helpers/users_helper.rb
@@ -1,12 +1,16 @@
# coding: utf-8
module UsersHelper
def user_name_tag(user)
def user_name_tag(user, options = {})
options[:url] ||= false
return "" if user.blank?
raw "<a href=\"#{user_path(user.slug)}\" class=\"user\" title=\"#{user.name}\">#{user.name}</a>"
return user.name if user.slug.blank?
url = options[:url] == true ? user_url(user.slug) : user_path(user.slug)
raw "<a href=\"#{url}\" class=\"user\" title=\"#{user.name}\">#{user.name}</a>"
end

def user_avatar_tag(user,size)
return "" if user.blank?
return "" if user.slug.blank?
url = eval("user.avatar.#{size}.url")
if url.blank?
url = ""
Expand Down
8 changes: 8 additions & 0 deletions app/mailers/user_mailer.rb
Expand Up @@ -53,5 +53,13 @@ def invite_to_answer(ask_id, user_id, invitor_ids)
mail(:to => @user.email, :subject => @title, :from => Setting.email_sender)
end

# 向用户提问
def ask_user(ask_id)
@ask = Ask.find(ask_id)
@user = @ask.to_user
@title = "#{@ask.user.name}对向你询问《#{@ask.title}》"
mail(:to => @user.email, :subject => @title, :from => Setting.email_sender)
end


end
27 changes: 21 additions & 6 deletions app/models/ask.rb
Expand Up @@ -25,6 +25,8 @@ class Ask

# 提问人
belongs_to :user, :inverse_of => :asks
# 对指定人的提问
belongs_to :to_user, :class_name => "User"

# 回答
has_many :answers
Expand Down Expand Up @@ -55,21 +57,30 @@ class Ask
scope :only_ids, lambda { |id_array| any_in("_id" => (id_array ||= [])) }

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

redis_search_index(:title_field => :title,:ext_fields => [:topics])

before_save :fill_default_values
after_create :create_log, :inc_counter_cache
after_create :create_log, :inc_counter_cache, :send_mails
after_destroy :dec_counter_cache
before_update :update_log

def view!
self.inc(:views_count, 1)
end

def send_mails
# 向某人提问
if !self.to_user.blank?
if self.to_user.mail_ask_me
UserMailer.ask_user(self.id).deliver
end
end
end

def inc_counter_cache
self.user.inc(:asks_count, 1)
end
Expand Down Expand Up @@ -128,7 +139,8 @@ def fill_default_values
# 参数 add true 增加, false 去掉
def update_topics(topics, add = true, current_user_id = nil)
self.topics = [] if self.topics.blank?
topics = [topics] if topics.class != [].class
# 分割逗号
topics = topics.split(/,|,/) if topics.class != [].class
# 去两边空格
topics = topics.collect { |t| t.strip if !t.blank? }.compact
action = nil
Expand Down Expand Up @@ -174,8 +186,7 @@ def self.mmseg_text(text)

def self.search_title(text,options = {})
limit = options[:limit] || 10
words = mmseg_text(text)
Ask.all_in(:title => words.collect { |w| /#{w}/i }).recent.normal.limit(limit)
Ask.search(text,:limit => limit)
end

def self.find_by_title(title)
Expand Down Expand Up @@ -227,6 +238,10 @@ def insert_action_log(action)
log.ask = self
log.target_id = self.id
log.target_attr = (self.title_changed? ? "TITLE" : (self.body_changed? ? "BODY" : "")) if action == "EDIT"
if(action == "NEW" and !self.to_user_id.blank?)
action = "NEW_TO_USER"
log.target_parent_id = self.to_user_id
end
log.action = action
log.diff = ""
log.save
Expand Down
6 changes: 5 additions & 1 deletion app/models/ask_invite.rb
Expand Up @@ -30,6 +30,8 @@ def self.insert_log(ask_id, user_id, invitor_id)

def self.invite(ask_id,user_id,invitor_id)
item = find_or_create_by(:ask_id => ask_id,:user_id => user_id)
user = item.user
return -1 if user.blank?
item.invitor_ids ||= []
item.count ||= 0
return item if item.invitor_ids.include?(invitor_id)
Expand All @@ -38,7 +40,9 @@ def self.invite(ask_id,user_id,invitor_id)

# 发送邮件
if(item.mail_sent <= 1)
UserMailer.invite_to_answer(item.ask_id, item.user_id, item.invitor_ids).deliver
if item.user.mail_invite_to_ask
UserMailer.invite_to_answer(item.ask_id, item.user_id, item.invitor_ids).deliver
end
item.mail_sent += 1
end

Expand Down
9 changes: 7 additions & 2 deletions app/models/log.rb
Expand Up @@ -27,10 +27,15 @@ class AskLog < Log
def send_notification
case self.action
when "INVITE_TO_ANSWER"
Notification.create(user_id: self.target_id,
Notification.create(user_id: self.target_parent_id,
log_id: self.id,
target_id: self.target_parent_id,
target_id: self.target_id,
action: "INVITE_TO_ANSWER")
when "NEW_TO_USER"
Notification.create(user_id: self.target_parent_id,
log_id: self.id,
target_id: self.target_id,
action: "ASK_USER")
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/report_spam.rb
@@ -0,0 +1,16 @@
class ReportSpam
include Mongoid::Document
include Mongoid::Timestamps
include BaseModel

field :url
field :descriptions, :type => Array, :default => []

index :url

def self.add(url, description, user_name)
report = find_or_create_by(:url => url)
report.descriptions << "#{user_name}:\n#{description}"
report.save
end
end
5 changes: 4 additions & 1 deletion app/models/user.rb
Expand Up @@ -28,6 +28,8 @@ class User
# Email 提醒的状态
field :mail_be_followed, :type => Boolean, :default => true
field :mail_new_answer, :type => Boolean, :default => true
field :mail_invite_to_ask, :type => Boolean, :default => true
field :mail_ask_me, :type => Boolean, :default => true
field :thanked_answer_ids, :type => Array, :default => []

# 邀请字段
Expand All @@ -51,7 +53,8 @@ class User
has_many :logs, :class_name => "Log", :foreign_key => "target_id"

attr_accessor :password_confirmation
attr_accessible :email, :password,:name, :slug, :tagline, :bio, :avatar, :website, :girl, :mail_new_answer, :mail_be_followed
attr_accessible :email, :password,:name, :slug, :tagline, :bio, :avatar, :website, :girl,
:mail_new_answer, :mail_be_followed, :mail_invite_to_ask, :mail_ask_me

validates_presence_of :name, :slug
validates_uniqueness_of :slug
Expand Down
4 changes: 2 additions & 2 deletions app/uploaders/cover_uploader.rb
Expand Up @@ -4,11 +4,11 @@ def default_url
end

version :small do
process :resize_to_fill => [25, 25]
process :resize_to_fit => [25, 25]
end

version :normal do
process :resize_to_fill => [100, 100]
process :resize_to_fit => [100, 100]
end

end

0 comments on commit 8dff887

Please sign in to comment.