bigdir/app/controllers/memos_controller.rb

148 lines
4.4 KiB
Ruby
Raw Normal View History

2013-11-22 21:55:21 +08:00
class MemosController < ApplicationController
2013-11-26 16:32:08 +08:00
default_search_scope :messages
before_filter :find_forum, :only => [:new, :preview]
before_filter :find_attachments, :only => [:preview]
before_filter :find_memo, :except => [:new, :create , :preview, :update]
helper :attachments
include AttachmentsHelper
layout 'base_memos'# ,except: [:new ]
def quote
@memo = Memo.find_by_id(params[:id])
@subject = @memo.subject
@subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
@content = "#{ll(Setting.default_language, :text_user_wrote, @memo.author)} <br/> &nbsp; "
@content << @memo.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n") + "</blockquote>\n\n<br/>"
@content = "<blockquote>" << @content
end
2013-11-23 08:20:03 +08:00
def new
2013-11-25 15:05:42 +08:00
@forum = Forum.find(params[:forum_id])
2013-11-23 08:20:03 +08:00
@memo = Memo.new
2013-11-25 15:05:42 +08:00
@memo.forum_id = @forum.id
2013-11-23 08:20:03 +08:00
respond_to do |format|
2013-11-25 15:05:42 +08:00
format.html {
render action: :new ,layout: 'base'
}# new.html.erb
2013-11-23 08:20:03 +08:00
format.json { render json: @memo }
end
end
def create
@memo = Memo.new(params[:memo])
2013-11-23 20:56:22 +08:00
@memo.forum_id = params[:forum_id]
2013-11-23 08:20:03 +08:00
@memo.author_id = User.current.id
@forum = Forum.find(params[:forum_id])
2013-11-23 10:47:28 +08:00
if @memo.parent_id
@parent_memo = Memo.find_by_id(@memo.parent_id)
@parent_memo.replies_count += 1
end
2013-11-23 08:20:03 +08:00
2013-11-26 16:32:08 +08:00
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
2013-11-23 08:20:03 +08:00
respond_to do |format|
if @memo.save
@forum.memo_count += 1
@forum.last_memo_id = @memo.id
2013-11-23 21:13:59 +08:00
@back_memo_id = (@memo.parent_id.nil? ? @memo.id : @memo.parent_id)
if @parent_memo
2013-11-26 16:32:08 +08:00
@parent_memo.last_reply_id = @memo
2013-11-23 21:13:59 +08:00
@parent_memo.save
else
@forum.topic_count += 1
2013-11-23 21:13:59 +08:00
end
@forum.save
2013-11-23 21:13:59 +08:00
2013-11-23 20:56:22 +08:00
format.html { redirect_to forum_memo_path(@memo.forum_id, @back_memo_id), notice: 'Memo was successfully created.' }
format.json { render json: @memo, status: :created, location: @memo }
2013-11-23 08:20:03 +08:00
else
2013-11-26 16:32:08 +08:00
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: 'Memo was failed created.' }
2013-11-23 08:20:03 +08:00
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end
2013-11-26 16:32:08 +08:00
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']
@offset ||= @reply_pages.offset
@replies = @replies_all.offset(@offset).limit(@limit).all
@mome_new = Memo.new
# @memo = Memo.find_by_id(params[:id])
# @forum = Forum.find(params[:forum_id])
# @replies = @memo.replies
# @mome_new = Memo.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @memo }
end
end
def edit
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: 'Memo was successfully updated.'}
#format.html redirect_to forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id)), notice: 'Memo was successfully updated.'
end
else
format.html { render action: "edit" }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
2013-11-23 08:20:03 +08:00
def destroy
@memo = Memo.find(params[:id])
@memo.destroy
2013-11-26 16:32:08 +08:00
if @memo.parent_id
@back_url = forum_memo_url(params[:forum_id], @memo.parent_id)
else
@back_url = forum_url(params[:forum_id])
end
2013-11-23 08:20:03 +08:00
respond_to do |format|
2013-11-26 16:32:08 +08:00
format.html { redirect_to @back_url }
2013-11-23 08:20:03 +08:00
format.json { head :no_content }
end
end
2013-11-26 16:32:08 +08:00
private
def find_memo
return unless find_forum
@memo = @forum.memos.find(params[:id])
#@memo =
rescue ActiveRecord::RecordNotFound
render_404
nil
end
def find_forum
@forum = Forum.find(params[:forum_id])
rescue ActiveRecord::RecordNotFound
render_404
nil
end
2013-11-22 21:55:21 +08:00
end