diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index f27e0bb01..97445e72a 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -1,18 +1,17 @@ class MemosController < ApplicationController - default_search_scope :messages - before_filter :find_forum, :only => [:new, :preview] + default_search_scope :memos + before_filter :find_forum, :only => [:new, :create, :preview] before_filter :find_attachments, :only => [:preview] - before_filter :find_memo, :except => [:new, :create , :preview, :update] + before_filter :find_memo, :except => [:new, :create, :preview] before_filter :authenticate_user_edit, :only => [:edit, :update] before_filter :authenticate_user_destroy, :only => [:destroy] helper :attachments include AttachmentsHelper - layout 'base_memos'# ,except: [:new ] + layout 'base_memos' def quote - @memo = Memo.find_by_id(params[:id]) @subject = @memo.subject @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:') @@ -22,14 +21,13 @@ class MemosController < ApplicationController end def new - @forum = Forum.find(params[:forum_id]) @memo = Memo.new @memo.forum_id = @forum.id respond_to do |format| format.html { render action: :new ,layout: 'base' - }# new.html.erb + } format.json { render json: @memo } end end @@ -38,33 +36,16 @@ class MemosController < ApplicationController @memo = Memo.new(params[:memo]) @memo.forum_id = params[:forum_id] @memo.author_id = User.current.id - @forum = Forum.find(params[:forum_id]) - - if @memo.parent_id - @parent_memo = Memo.find_by_id(@memo.parent_id) - @parent_memo.replies_count += 1 - end @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads])) respond_to do |format| if @memo.save - @forum.memo_count += 1 - @forum.last_memo_id = @memo.id - @back_memo_id = (@memo.parent_id.nil? ? @memo.id : @memo.parent_id) - if @parent_memo - @parent_memo.last_reply_id = @memo - @parent_memo.save - else - @forum.topic_count += 1 - end - @forum.save - - format.html { redirect_to forum_memo_path(@memo.forum_id, @back_memo_id), notice: "#{l :label_memo_create_succ}" } + format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" } format.json { render json: @memo, status: :created, location: @memo } else - back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id) - format.html { redirect_to back_error_page, notice: "#{l :label_memo_create_fail}" } + # 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, notice: "#{l :label_memo_create_fail}" } format.json { render json: @memo.errors, status: :unprocessable_entity } end end @@ -73,9 +54,7 @@ class MemosController < ApplicationController def show pre_count = 20 @current_count = pre_count * (params['page'].to_i - 1) if params['page'].to_i > 0 - @memo = Memo.find_by_id(params[:id]) @offset, @limit = api_offset_and_limit({:limit => pre_count}) - @forum = Forum.find(params[:forum_id]) @replies_all = @memo.replies @reply_count = @replies_all.count @reply_pages = Paginator.new @reply_count, @limit, params['page'] @@ -99,32 +78,23 @@ class MemosController < ApplicationController end def update - @forum = Forum.find(params[:forum_id]) - @memo = Memo.find(params[:id]) - if(@memo.update_attribute(:subject, params[:memo][:subject]) && - @memo.update_attribute(:content, params[:memo][:content])) - respond_to do |format| - format.html {redirect_to forum_memo_path(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id)), notice: "#{l :label_memo_create_succ}"} - #format.html redirect_to forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id)), notice: 'Memo was successfully updated.' + respond_to do |format| + if(@memo.update_attribute(:subject, params[:memo][:subject]) && + @memo.update_attribute(:content, params[:memo][:content])) + format.html {redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}"} + else + format.html { render action: "edit" } + format.json { render json: @person.errors, status: :unprocessable_entity } end - else - format.html { render action: "edit" } - format.json { render json: @person.errors, status: :unprocessable_entity } end end def destroy - @memo = Memo.find(params[:id]) @memo.destroy - if @memo.parent_id - @back_url = forum_memo_url(params[:forum_id], @memo.parent_id) - else - @back_url = forum_url(params[:forum_id]) - end - respond_to do |format| - format.html { redirect_to @back_url } + # format.html { redirect_to @back_url } + format.html { redirect_to back_memo_or_forum_url } format.json { head :no_content } end end @@ -134,7 +104,6 @@ class MemosController < ApplicationController def find_memo return unless find_forum @memo = @forum.memos.find(params[:id]) - #@memo = rescue ActiveRecord::RecordNotFound render_404 nil @@ -155,6 +124,14 @@ class MemosController < ApplicationController def authenticate_user_destroy find_memo render_403 unless @memo.destroyable_by? User.current - end + + def back_memo_url + forum_memo_path(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id)) + end + + def back_memo_or_forum_url + @memo.parent_id.nil? ? forum_url(@forum) : forum_memo_url(@forum, @memo.parent_id) + end + end diff --git a/app/models/memo.rb b/app/models/memo.rb index 0c1f7032e..051bbe3f0 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -40,6 +40,8 @@ class Memo < ActiveRecord::Base # after_update :update_memos_forum after_destroy :reset_counters! # after_create :send_notification + after_save :plusParentAndForum + after_destroy :minusParentAndForum # scope :visible, lambda { |*args| # includes(:forum => ).where() @@ -105,4 +107,36 @@ class Memo < ActiveRecord::Base end end + def plusParentAndForum + @forum = Forum.find(self.forum_id) + @forum.memo_count = @forum.memo_count.to_int + 1 + @forum.last_memo_id = self.id + if self.parent_id + @parent_memo = Memo.find_by_id(self.parent_id) + @parent_memo.last_reply_id = self + @parent_memo.replies_count = @parent_memo.replies_count.to_int + 1 + @parent_memo.save + else + @forum.topic_count = @forum.topic_count.to_int + 1 + end + @forum.save + end + + def minusParentAndForum + @forum = Forum.find(self.forum_id) + @forum.memo_count = @forum.memo_count.to_int - 1 + @forum.memo_count = 0 if @forum.memo_count.to_int < 0 + # @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id + if self.parent_id + @parent_memo = Memo.find_by_id(self.parent_id) + # @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id + @parent_memo.replies_count = @parent_memo.replies_count.to_int - 1 + @parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0 + @parent_memo.save + else + @forum.topic_count = @forum.topic_count.to_int - 1 + @forum.topic_count = 0 if @forum.topic_count.to_int < 0 + end + @forum.save + end end