2013-11-22 21:55:21 +08:00
|
|
|
class MemosController < ApplicationController
|
2013-11-25 15:05:42 +08:00
|
|
|
layout 'base_memos' ,except: [:new ]
|
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 show
|
2013-11-25 15:05:42 +08:00
|
|
|
# @offset, @limit = api_offset_and_limit({:limit => 10})
|
|
|
|
# @forum = Forum.find(params[:id])
|
|
|
|
# @memos_all = @forum.topics
|
|
|
|
# @topic_count = @memos_all.count
|
|
|
|
# @topic_pages = Paginator.new @topic_count, @limit, params['page']
|
|
|
|
|
|
|
|
# @offset ||= @topic_pages.offset
|
|
|
|
# @memos = @memos_all.offset(@offset).limit(@limit).all
|
|
|
|
# respond_to do |format|
|
|
|
|
# format.html {
|
|
|
|
# render :layout => 'base_forums'
|
|
|
|
# }# show.html.erb
|
|
|
|
# format.json { render json: @forum }
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-23 21:13:59 +08:00
|
|
|
@memo = Memo.find_by_id(params[:id])
|
2013-11-25 15:05:42 +08:00
|
|
|
@offset, @limit = api_offset_and_limit({:limit => 10})
|
2013-11-24 20:09:24 +08:00
|
|
|
@forum = Forum.find(params[:forum_id])
|
2013-11-25 15:05:42 +08:00
|
|
|
@replies_all = @memo.replies
|
|
|
|
@repliy_count = @replies_all.count
|
|
|
|
@repliy_pages = Paginator.new @repliy_count, @limit, params['page']
|
|
|
|
@offset ||= @repliy_pages.offset
|
|
|
|
@replies = @replies_all.offset(@offset).limit(@limit).all
|
2013-11-23 10:47:28 +08:00
|
|
|
@mome_new = Memo.new
|
2013-11-25 15:05:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
# @memo = Memo.find_by_id(params[:id])
|
|
|
|
# @forum = Forum.find(params[:forum_id])
|
|
|
|
# @replies = @memo.replies
|
|
|
|
# @mome_new = Memo.new
|
2013-11-23 08:20:03 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html # show.html.erb
|
|
|
|
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-24 20:02:53 +08:00
|
|
|
@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
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @memo.save
|
2013-11-24 20:02:53 +08:00
|
|
|
@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
|
|
|
|
@parent_memo.last_reply_id = @memo.id
|
|
|
|
@parent_memo.save
|
2013-11-24 20:02:53 +08:00
|
|
|
else
|
|
|
|
@forum.topic_count += 1
|
2013-11-23 21:13:59 +08:00
|
|
|
end
|
2013-11-24 20:02:53 +08:00
|
|
|
@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
|
|
|
|
format.html { render action: "new" }
|
|
|
|
format.json { render json: @memo.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@memo = Memo.find(params[:id])
|
|
|
|
@memo.destroy
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to memos_url }
|
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
|
|
|
end
|
2013-11-22 21:55:21 +08:00
|
|
|
end
|