socialforge/app/controllers/memos_controller.rb

184 lines
5.8 KiB
Ruby
Raw Normal View History

2013-11-22 21:55:21 +08:00
class MemosController < ApplicationController
2013-11-27 10:16:35 +08:00
default_search_scope :memos
before_filter :find_forum, :only => [:new, :create, :preview]
2013-11-26 16:32:08 +08:00
before_filter :find_attachments, :only => [:preview]
2013-11-27 10:16:35 +08:00
before_filter :find_memo, :except => [:new, :create, :preview]
2013-11-26 16:55:46 +08:00
before_filter :authenticate_user_edit, :only => [:edit, :update]
before_filter :authenticate_user_destroy, :only => [:destroy]
2014-03-08 11:08:48 +08:00
before_filter :require_login, :only => [:new, :create]
2013-11-26 16:32:08 +08:00
helper :attachments
include AttachmentsHelper
2013-11-27 10:16:35 +08:00
layout 'base_memos'
2013-11-26 16:32:08 +08:00
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
2013-11-23 08:20:03 +08:00
def new
@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'
2013-11-27 10:16:35 +08:00
}
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
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
2013-11-27 10:16:35 +08:00
format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" }
2013-11-23 20:56:22 +08:00
format.json { render json: @memo, status: :created, location: @memo }
2013-11-23 08:20:03 +08:00
else
flash[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
2013-11-27 10:16:35 +08:00
# back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
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]}" }
2013-11-23 08:20:03 +08:00
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end
REPLIES_PER_PAGE = 20 unless const_defined?(:REPLIES_PER_PAGE)
2013-11-26 16:32:08 +08:00
def show
pre_count = REPLIES_PER_PAGE
@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
@memo_new = Memo.new
2013-11-26 16:32:08 +08:00
# @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 }
2014-04-01 20:05:14 +08:00
format.xml { render xml: @memo }
2013-11-26 16:32:08 +08:00
end
end
def edit
2013-12-11 22:19:41 +08:00
@replying = false
2013-11-26 16:32:08 +08:00
end
def update
2013-11-27 10:16:35 +08:00
respond_to do |format|
if( @memo.update_column(:subject, params[:memo][:subject]) &&
@memo.update_column(:content, params[:memo][:content]) &&
@memo.update_column(:sticky, params[:memo][:sticky]) &&
@memo.update_column(:lock, params[:memo][:lock]))
2013-11-27 14:52:36 +08:00
@memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
# @memo.root.update_attribute(:updated_at, @memo.updated_at)
2013-11-27 10:16:35 +08:00
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 }
2013-11-26 16:32:08 +08:00
end
end
end
2013-11-23 08:20:03 +08:00
def destroy
@memo.destroy
respond_to do |format|
2013-11-27 10:16:35 +08:00
# format.html { redirect_to @back_url }
format.html { redirect_to back_memo_or_forum_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
2014-05-04 18:40:49 +08:00
#@memo = @forum.memos.find(params[:id])
@memo = Memo.find(params[:id])
2013-11-26 16:32:08 +08:00
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-26 16:55:46 +08:00
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
2013-11-27 10:16:35 +08:00
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
2013-11-22 21:55:21 +08:00
end