Skip to content

Commit

Permalink
Merge branch 'master' of github.com:huacnlee/quora
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 4, 2011
2 parents 500a656 + 95d38f7 commit 581cd71
Show file tree
Hide file tree
Showing 25 changed files with 321 additions and 15 deletions.
74 changes: 74 additions & 0 deletions app/controllers/cpanel/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# coding: UTF-8
class Cpanel::CommentsController < CpanelController

def index
@comments = initialize_grid(Comment,
:order => 'id',
:order_direction => 'desc')

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

def show
@comment = Comment.find(params[:id])

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

def new
@comment = Comment.new

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

def edit
@comment = Comment.find(params[:id])
end

def create
@comment = Comment.new(params[:comment])

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

def update
@comment = Comment.find(params[:id])

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

def destroy
@comment = Comment.find(params[:id])
@comment.destroy

respond_to do |format|
format.html { redirect_to(cpanel_comments_path,:notice => "删除成功。") }
format.json
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/cpanel/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Cpanel::CommentsHelper
end
9 changes: 8 additions & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
class Answer
class Answer < BaseModel
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Voteable
Expand All @@ -15,6 +15,13 @@ class Answer
has_many :logs, :class_name => "Log", :foreign_key => "target_id"

validates_presence_of :user_id, :body
# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("body")
return false
end
end

after_create :save_to_ask_and_update_answered_at
before_update :log_update
Expand Down
22 changes: 20 additions & 2 deletions app/models/ask.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
class Ask
class Ask < BaseModel
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Sphinx
Expand Down Expand Up @@ -39,7 +39,8 @@ class Ask

attr_protected :user_id
attr_accessor :current_user_id
validates_presence_of :user_id, :title, :current_user_id
validates_presence_of :user_id, :title
validates_presence_of :current_user_id, :if => proc { |obj| obj.title_changed? or obj.body_changed? }

# 正常可显示的问题, 前台调用都带上这个过滤
scope :normal, where(:spams_count.lt => Setting.ask_spam_max)
Expand All @@ -59,6 +60,23 @@ class Ask
after_destroy :dec_counter_cache
before_update :update_log

# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("title")
return false
end

if self.spam?("body")
return false
end

if self.spam?("topics")
return false
end

end

def inc_counter_cache
self.user.inc(:asks_count, 1)
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/base_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8
class BaseModel
# 检测敏感词
def spam?(attr)
value = eval("self.#{attr}")
return false if value.blank?
if value.class == [].class
value = value.join(" ")
end
spam_reg = Regexp.new(Setting.spam_words)
if matched = spam_reg.match(value)
self.errors.add(attr,"带有敏感内容[#{matched.to_a.join(",")}],请注意一下!")
return false
end
end
end
10 changes: 9 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
class Comment
class Comment < BaseModel
include Mongoid::Document
include Mongoid::Timestamps

Expand All @@ -11,6 +11,14 @@ class Comment

validates_presence_of :body

# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("body")
return false
end
end

before_create :fix_commentable_id
def fix_commentable_id
if self.commentable_id.class == "".class
Expand Down
13 changes: 12 additions & 1 deletion app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
class Topic
class Topic < BaseModel
include Mongoid::Document

field :name
Expand All @@ -17,6 +17,17 @@ class Topic

validates_presence_of :name
validates_uniqueness_of :name, :case_insensitive => true
# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("name")
return false
end

if self.spam?("summary")
return false
end
end

def self.save_topics(topics, current_user_id)
topics.each do |item|
Expand Down
18 changes: 17 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
class User
class User < BaseModel
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Voter
Expand Down Expand Up @@ -47,6 +47,22 @@ class User

validates_presence_of :name, :slug
validates_uniqueness_of :slug
# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("tagline")
return false
end
if self.spam?("name")
return false
end
if self.spam?("slug")
return false
end
if self.spam?("bio")
return false
end
end

def password_required?
!persisted? || password.present? || password_confirmation.present?
Expand Down
7 changes: 5 additions & 2 deletions app/views/asks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<div id="ask">
<%= simple_form_for(@ask) do |f| %>
<div class="inputs">
<%= f.input :title, :input_html => {:class => "long"}%>
<%= f.input :body, :as => :text, :input_html => {:class => "long"} %>
<%= f.input :title, :label => "标题", :input_html => {:class => "long"}%>
<%= f.input :body, :label => "正文", :as => :text, :input_html => {:class => "long"} %>
<script type="text/javascript">
$("#ask_body").qeditor();
</script>
</div>
<div class="actions">
<button class="submit">提交问题</button> 或者 <%= link_to '返回', asks_path %>
Expand Down
15 changes: 15 additions & 0 deletions app/views/cpanel/comments/_base.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= content_for :base_sitemap do %>
<a href="<%= cpanel_comments_path %>">cpanel_comments</a> »
<% end %>
<%= content_for :styles do %>
<% end %>

<%= spaceless do %>
<div class="tools">
<a href="<%= cpanel_comments_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_comment_path %>" class="button small right<%= ' checked' if ["new","create"].index(params[:action]) %>">新建</a>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/cpanel/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="cpanel_comment" class="form">
<%= simple_form_for(@comment,
:url => (@comment.id.blank? ? cpanel_comments_path : cpanel_comment_path(@comment.id)) ) do |f| %>

<div class="inputs">
<%= f.input :body %>
<%= f.input :user_id %>
<%= f.input :commentable_type %>
<%= f.input :commentable_id %>
</div>
<div class="actions">
<button type="submit">提交</button>
或者 <%= link_to '返回', cpanel_comments_path %>
</div>
<% end %>
</div>
6 changes: 6 additions & 0 deletions app/views/cpanel/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= content_for :sitemap do %>
<span class="current">修改</span>
<% end %>
<%= render 'base' %>
<%= render 'form' %>

27 changes: 27 additions & 0 deletions app/views/cpanel/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= content_for :sitemap do %>
<span class="current">列表</span>
<% end %>
<%= render 'base' %>

<div id="cpanel_comments">
<%= grid(@comments, :show_filters => :false) do |g|
g.column :column_name => 'body', :attribute_name => 'body' do |c|
c.body if !c.body.blank?
end
g.column :column_name => 'user_id', :attribute_name => 'user_id' do |c|
c.user_id if !c.user_id.blank?
end
g.column :column_name => 'commentable_type', :attribute_name => 'commentable_type' do |c|
c.commentable_type if !c.commentable_type.blank?
end
g.column :column_name => 'commentable_type', :attribute_name => 'commentable_type' do |c|
c.commentable_id if !c.commentable_id.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 image_tag("wice_grid/delete.png", :style => "margin-bottom:-4px"), cpanel_comment_path(c.id), :method => :delete, :confirm => '确定要删除吗?'}"
end
end %>
</div>


5 changes: 5 additions & 0 deletions app/views/cpanel/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= content_for :sitemap do %>
<span class="current">新建</span>
<% end %>
<%= render 'base' %>
<%= render 'form' %>
26 changes: 26 additions & 0 deletions app/views/cpanel/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= content_for :sitemap do %>
<span class="current">查看</span>
<% end %>
<%= render 'base' %>
<div class="tools">
<%= link_to '修改', edit_cpanel_comment_path(@comment) %> |
<%= link_to '返回', cpanel_comments_path %>
</div>
<div id="cpanel_comment">
<p>
<b>Body:</b>
<%= @comment.body %>
</p>
<p>
<b>User:</b>
<%= @comment.user_id %>
</p>
<p>
<b>Commentable type:</b>
<%= @comment.commentable_type %>
</p>
<p>
<b>Commentable:</b>
<%= @comment.commentable_id %>
</p>
</div>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= yield :styles %>
<meta name="keywords" content="<%= @meta_keywords %>" />
<meta name="description" content="<%= @meta_description %>" />
<link rel="shortcut icon" href="/favicon.ico?v=001" />
<link rel="shortcut icon" href="/favicon.ico?v=002" />
<%= csrf_meta_tag %>
<%= javascript_include_tag "jquery.min","rails","facebox","jquery.jdialog","jquery.qeditor","jcaches","application","asks", :cache => "cached_application" %>
<%= yield :scripts %>
Expand Down
11 changes: 5 additions & 6 deletions app/views/users/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
<div class="detail">
<h1 id="user_name"><%= user.name %><span></span><span id="user_tagline"><%= user.tagline %></span>
<% if owner?user %>
<%= in_place_edit_tag(user,:tagline, :type => :textarea,:width => 200, :height => 80,
:rich => false, :text_id => "user_tagline") %>
<%= in_place_edit_tag(user,:tagline,:rich => false, :text_id => "user_tagline") %>
<% end %></h1>
<% if !user.website.blank? %>
<p class="website">
<div class="website">
<a href="<%= user.website %>" rel="nofollow" target="_blank" id="user_website"><%= user.website %></a>
<% if owner?user %> <%= in_place_edit_tag(user,:website, :text_id => "user_website") %> <% end %>
</p>
</div>
<% end %>
<p>
<div>
<span id="user_bio">
<% if @user.bio.blank? and (not owner?@user) %>
<%= @user.name %>还没来得及写他的个人介绍.
Expand All @@ -33,7 +32,7 @@
<% end %>
</span>
<% if owner?@user %> <%= in_place_edit_tag(@user,:bio,:type => :textarea, :text_id => "user_bio", :label => "修改个人经历") %> <% end %>
</p>
</div>
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
resources :asks
resources :answers
resources :topics
resources :comments
end
end
Loading

0 comments on commit 581cd71

Please sign in to comment.