From 4f5eff09c837ba3a862b92c1ff8978d480a562eb Mon Sep 17 00:00:00 2001 From: yanxd Date: Mon, 14 Apr 2014 10:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=96=E5=86=85=E5=AE=B9=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=B8=8D=E9=80=9A=E8=BF=87=E4=B8=8D=E4=BC=9A=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E4=BF=A1=E6=81=AF=E4=BA=86=EF=BC=8C=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E4=BA=86=E6=89=80=E6=9C=89=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/forums_controller.rb | 36 ++++++++++++++++++++++++++++ app/controllers/memos_controller.rb | 27 +++++++++++++++++++-- app/models/memo.rb | 5 ++-- app/views/forums/show.html.erb | 2 +- app/views/memos/_reply_box.html.erb | 2 +- config/routes.rb | 1 + 6 files changed, 67 insertions(+), 6 deletions(-) diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index be1a331e0..991af0154 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -11,6 +11,42 @@ class ForumsController < ApplicationController include SortHelper PageLimit = 20 + + def create_memo + @memo = Memo.new(params[:memo]) + @memo.forum_id = @forum.id + @memo.author_id = User.current.id + + @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads])) + + respond_to do |format| + if @memo.save + format.html { redirect_to (forum_memo_path(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" } + format.json { render json: @memo, status: :created, location: @memo } + else + sort_init 'updated_at', 'desc' + sort_update 'created_at' => "#{Memo.table_name}.created_at", + 'replies' => "#{Memo.table_name}.replies_count", + 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)" + + @topic_count = @forum.topics.count + @topic_pages = Paginator.new @topic_count, per_page_option, params['page'] + @memos = @forum.topics. + reorder("#{Memo.table_name}.sticky DESC"). + includes(:last_reply). + limit(@topic_pages.per_page). + offset(@topic_pages.offset). + order(sort_clause). + preload(:author, {:last_reply => :author}). + all + + flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" + # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id) + format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end + end + end def index @offset, @limit = api_offset_and_limit({:limit => 10}) diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index a21086da3..f6ad48801 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -47,7 +47,30 @@ class MemosController < ApplicationController else flash[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id) - format.html { redirect_to back_memo_or_forum_url}#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + pre_count = REPLIES_PER_PAGE + + @memo_new = @memo.dup + @memo = @memo.root # 取出楼主,防止输入帖子id让回复作为主贴显示 + @memo.update_column(:viewed_count, (@memo.viewed_count.to_i + 1)) + + page = params[:page] + if params[:r] && page.nil? + offset = @memo.children.where("#{Memo.table_name}.id < ?", params[:r].to_i).count + page = 1 + offset / pre_count + else + + end + @reply_count = @memo.children.count + @reply_pages = Paginator.new @reply_count, pre_count, page + @replies = @memo.children. + includes(:author, :attachments). + reorder("#{Memo.table_name}.created_at ASC"). + limit(@reply_pages.per_page). + offset(@reply_pages.offset). + all + + format.html { render action: :show }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + # format.html { redirect_to back_memo_or_forum_url}#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } format.json { render json: @memo.errors, status: :unprocessable_entity } end end @@ -76,7 +99,7 @@ class MemosController < ApplicationController offset(@reply_pages.offset). all - @mome_new = Memo.new + @memo_new = Memo.new # @memo = Memo.find_by_id(params[:id]) diff --git a/app/models/memo.rb b/app/models/memo.rb index 2e4ddb810..a0581d945 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -4,9 +4,10 @@ class Memo < ActiveRecord::Base belongs_to :author, :class_name => "User", :foreign_key => 'author_id' validates_presence_of :author_id, :forum_id, :subject - #validates :content, presence: true + # 若是主题帖,则内容可以是空 + validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? } validates_length_of :subject, maximum: 50 - #validates_length_of :content, maximum: 3072 + validates_length_of :content, maximum: 3072 validate :cannot_reply_to_locked_topic, :on => :create acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC" diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index f3f0524ae..677416a6d 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -2,7 +2,7 @@