diff --git a/app/controllers/cpanel/comments_controller.rb b/app/controllers/cpanel/comments_controller.rb new file mode 100644 index 0000000..54e72c6 --- /dev/null +++ b/app/controllers/cpanel/comments_controller.rb @@ -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 diff --git a/app/helpers/cpanel/comments_helper.rb b/app/helpers/cpanel/comments_helper.rb new file mode 100644 index 0000000..5f9bf07 --- /dev/null +++ b/app/helpers/cpanel/comments_helper.rb @@ -0,0 +1,2 @@ +module Cpanel::CommentsHelper +end diff --git a/app/models/answer.rb b/app/models/answer.rb index fa95c0d..f7db1d1 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -1,5 +1,5 @@ # coding: utf-8 -class Answer +class Answer < BaseModel include Mongoid::Document include Mongoid::Timestamps include Mongoid::Voteable @@ -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 diff --git a/app/models/ask.rb b/app/models/ask.rb index 88431c0..690f4eb 100644 --- a/app/models/ask.rb +++ b/app/models/ask.rb @@ -1,5 +1,5 @@ # coding: utf-8 -class Ask +class Ask < BaseModel include Mongoid::Document include Mongoid::Timestamps include Mongoid::Sphinx @@ -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) @@ -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 diff --git a/app/models/base_model.rb b/app/models/base_model.rb new file mode 100644 index 0000000..6c210f1 --- /dev/null +++ b/app/models/base_model.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 32692e8..5de0d7d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,5 @@ # coding: utf-8 -class Comment +class Comment < BaseModel include Mongoid::Document include Mongoid::Timestamps @@ -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 diff --git a/app/models/topic.rb b/app/models/topic.rb index f7ac895..9e753ef 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -1,5 +1,5 @@ # coding: utf-8 -class Topic +class Topic < BaseModel include Mongoid::Document field :name @@ -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| diff --git a/app/models/user.rb b/app/models/user.rb index 13ba0d1..d4ac7f7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ # coding: utf-8 -class User +class User < BaseModel include Mongoid::Document include Mongoid::Timestamps include Mongoid::Voter @@ -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? diff --git a/app/views/asks/_form.html.erb b/app/views/asks/_form.html.erb index b9977a0..6aec25a 100644 --- a/app/views/asks/_form.html.erb +++ b/app/views/asks/_form.html.erb @@ -1,8 +1,11 @@
<%= simple_form_for(@ask) do |f| %>
- <%= 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"} %> +
或者 <%= link_to '返回', asks_path %> diff --git a/app/views/cpanel/comments/_base.html.erb b/app/views/cpanel/comments/_base.html.erb new file mode 100644 index 0000000..4905c76 --- /dev/null +++ b/app/views/cpanel/comments/_base.html.erb @@ -0,0 +1,15 @@ +<%= content_for :base_sitemap do %> +cpanel_comments » +<% end %> +<%= content_for :styles do %> +<% end %> + +<%= spaceless do %> +
+ ">查看所有 + <% if ["edit","update"].index(params[:action]) %> + 编辑 + <% end %> + ">新建 +
+<% end %> diff --git a/app/views/cpanel/comments/_form.html.erb b/app/views/cpanel/comments/_form.html.erb new file mode 100644 index 0000000..9ee6d15 --- /dev/null +++ b/app/views/cpanel/comments/_form.html.erb @@ -0,0 +1,16 @@ +
+<%= simple_form_for(@comment, + :url => (@comment.id.blank? ? cpanel_comments_path : cpanel_comment_path(@comment.id)) ) do |f| %> + +
+ <%= f.input :body %> + <%= f.input :user_id %> + <%= f.input :commentable_type %> + <%= f.input :commentable_id %> +
+
+ + 或者 <%= link_to '返回', cpanel_comments_path %> +
+<% end %> +
diff --git a/app/views/cpanel/comments/edit.html.erb b/app/views/cpanel/comments/edit.html.erb new file mode 100644 index 0000000..9f3bf71 --- /dev/null +++ b/app/views/cpanel/comments/edit.html.erb @@ -0,0 +1,6 @@ +<%= content_for :sitemap do %> +修改 +<% end %> +<%= render 'base' %> +<%= render 'form' %> + diff --git a/app/views/cpanel/comments/index.html.erb b/app/views/cpanel/comments/index.html.erb new file mode 100644 index 0000000..9c2b428 --- /dev/null +++ b/app/views/cpanel/comments/index.html.erb @@ -0,0 +1,27 @@ +<%= content_for :sitemap do %> +列表 +<% end %> +<%= render 'base' %> + +
+ <%= 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 %> +
+ + diff --git a/app/views/cpanel/comments/new.html.erb b/app/views/cpanel/comments/new.html.erb new file mode 100644 index 0000000..a6c1d08 --- /dev/null +++ b/app/views/cpanel/comments/new.html.erb @@ -0,0 +1,5 @@ +<%= content_for :sitemap do %> +新建 +<% end %> +<%= render 'base' %> +<%= render 'form' %> diff --git a/app/views/cpanel/comments/show.html.erb b/app/views/cpanel/comments/show.html.erb new file mode 100644 index 0000000..8eb3ed3 --- /dev/null +++ b/app/views/cpanel/comments/show.html.erb @@ -0,0 +1,26 @@ +<%= content_for :sitemap do %> +查看 +<% end %> +<%= render 'base' %> +
+ <%= link_to '修改', edit_cpanel_comment_path(@comment) %> | + <%= link_to '返回', cpanel_comments_path %> +
+
+

+ Body: + <%= @comment.body %> +

+

+ User: + <%= @comment.user_id %> +

+

+ Commentable type: + <%= @comment.commentable_type %> +

+

+ Commentable: + <%= @comment.commentable_id %> +

+
\ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 99d623b..97d753a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,7 +10,7 @@ <%= yield :styles %> - + <%= csrf_meta_tag %> <%= javascript_include_tag "jquery.min","rails","facebox","jquery.jdialog","jquery.qeditor","jcaches","application","asks", :cache => "cached_application" %> <%= yield :scripts %> diff --git a/app/views/users/_head.html.erb b/app/views/users/_head.html.erb index ec6673f..d7bcfce 100644 --- a/app/views/users/_head.html.erb +++ b/app/views/users/_head.html.erb @@ -15,16 +15,15 @@

<%= user.name %><%= user.tagline %> <% 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 %>

<% if !user.website.blank? %> -

+

<%= user.website %> <% if owner?user %> <%= in_place_edit_tag(user,:website, :text_id => "user_website") %> <% end %> -

+
<% end %> -

+

<% if @user.bio.blank? and (not owner?@user) %> <%= @user.name %>还没来得及写他的个人介绍. @@ -33,7 +32,7 @@ <% end %> <% if owner?@user %> <%= in_place_edit_tag(@user,:bio,:type => :textarea, :text_id => "user_bio", :label => "修改个人经历") %> <% end %> -

+
diff --git a/config/routes.rb b/config/routes.rb index e6d1068..e1594eb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -67,5 +67,6 @@ resources :asks resources :answers resources :topics + resources :comments end end diff --git a/config/setting.yml.default b/config/setting.yml.default index 29341ce..01664ef 100644 --- a/config/setting.yml.default +++ b/config/setting.yml.default @@ -17,6 +17,7 @@ default: &defaults # 问题被多少次 spam 自动删除 ask_spam_max: 8 google_analytics_id: "UA-22413508-1" + spam_words: /敏感词1|敏感词2/ development: <<: *defaults diff --git a/public/favicon.ico b/public/favicon.ico index 923e7cc..e7542b6 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ diff --git a/public/images/favicon.png b/public/images/favicon.png new file mode 100644 index 0000000..81babca Binary files /dev/null and b/public/images/favicon.png differ diff --git a/public/images/icon.png b/public/images/icon.png new file mode 100644 index 0000000..bd3b1f9 Binary files /dev/null and b/public/images/icon.png differ diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index f4c5a72..f43f065 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -72,6 +72,8 @@ a.green_button:active { .simple_form .actions { margin:5px 0 10px 0; } .simple_form label { font-size: 12px; color:#666; display:block; } .form span.error-message { color:red; } +.simple_form span.error { display:block; color:red; display:block; } +.form span.error { display:block; color:red; display:block; } .alert_message, .notice_message, .error_explanation, #error_explanation { font-family: "adelle-1", "adelle-2", tahoma, sans-serif; diff --git a/test/functional/cpanel/comments_controller_test.rb b/test/functional/cpanel/comments_controller_test.rb new file mode 100644 index 0000000..c8a1e9b --- /dev/null +++ b/test/functional/cpanel/comments_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class Cpanel::CommentsControllerTest < ActionController::TestCase + setup do + @cpanel_comment = cpanel_comments(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:cpanel_comments) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create cpanel_comment" do + assert_difference('Cpanel::Comment.count') do + post :create, :cpanel_comment => @cpanel_comment.attributes + end + + assert_redirected_to cpanel_comment_path(assigns(:cpanel_comment)) + end + + test "should show cpanel_comment" do + get :show, :id => @cpanel_comment.to_param + assert_response :success + end + + test "should get edit" do + get :edit, :id => @cpanel_comment.to_param + assert_response :success + end + + test "should update cpanel_comment" do + put :update, :id => @cpanel_comment.to_param, :cpanel_comment => @cpanel_comment.attributes + assert_redirected_to cpanel_comment_path(assigns(:cpanel_comment)) + end + + test "should destroy cpanel_comment" do + assert_difference('Cpanel::Comment.count', -1) do + delete :destroy, :id => @cpanel_comment.to_param + end + + assert_redirected_to cpanel_comments_path + end +end diff --git a/test/unit/helpers/cpanel/comments_helper_test.rb b/test/unit/helpers/cpanel/comments_helper_test.rb new file mode 100644 index 0000000..b2b0d65 --- /dev/null +++ b/test/unit/helpers/cpanel/comments_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class Cpanel::CommentsHelperTest < ActionView::TestCase +end