socialforge/app/controllers/memos_controller.rb

139 lines
4.0 KiB
Ruby

class MemosController < ApplicationController
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]
before_filter :authenticate_user_edit, :only => [:edit, :update]
before_filter :authenticate_user_destroy, :only => [:destroy]
helper :attachments
include AttachmentsHelper
layout 'base_memos'
def quote
@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
def new
@memo = Memo.new
@memo.forum_id = @forum.id
respond_to do |format|
format.html {
render action: :new ,layout: 'base'
}
format.json { render json: @memo }
end
end
def create
@memo = Memo.new(params[:memo])
@memo.forum_id = params[: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 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_memo_or_forum_url, notice: "#{l :label_memo_create_fail}" }
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end
def show
pre_count = 20
@current_count = pre_count * (params['page'].to_i - 1) if params['page'].to_i > 0
@offset, @limit = api_offset_and_limit({:limit => pre_count})
@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
respond_to do |format|
if(@memo.update_attribute(:subject, params[:memo][:subject]) &&
@memo.update_attribute(:content, params[:memo][:content]))
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
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
end
end
def destroy
@memo.destroy
respond_to do |format|
# format.html { redirect_to @back_url }
format.html { redirect_to back_memo_or_forum_url }
format.json { head :no_content }
end
end
private
def find_memo
return unless find_forum
@memo = @forum.memos.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
nil
end
def find_forum
@forum = Forum.find(params[:forum_id])
rescue ActiveRecord::RecordNotFound
render_404
nil
end
def authenticate_user_edit
find_memo
render_403 unless @memo.editable_by? User.current
end
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