You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.6 KiB
139 lines
4.6 KiB
6 years ago
|
class MemosController < ApplicationController
|
||
|
before_action :set_memo, only: [:show, :edit, :update, :destroy]
|
||
|
|
||
|
include ApplicationHelper
|
||
|
# GET /memos
|
||
|
# GET /memos.json
|
||
|
def index
|
||
|
@user = current_user
|
||
|
@memos = Memo.all
|
||
|
s_order = (params[:order] == "replies_count" ? "all_replies_count" : params[:order]) || "updated_at"
|
||
|
#@tidding_count = unviewed_tiddings(current_user) if current_user.present?
|
||
|
page = params[:page].to_i
|
||
|
search = params[:search]
|
||
|
offset = page * 15
|
||
|
forum_id = params[:forum]
|
||
|
user_id = params[:user_id]
|
||
|
if user_id == -1
|
||
|
user_id = current_user.try(:id)
|
||
|
end
|
||
|
tag_repertoire_id = params[:tag_repertoire_id]
|
||
|
|
||
|
sql =
|
||
|
if forum_id
|
||
|
search ? "forum_id = #{forum_id} and root_id is null and subject like '%#{search}%'" :
|
||
|
"forum_id = #{forum_id} and root_id is null"
|
||
|
elsif search
|
||
|
user_id ? "author_id = #{user_id.to_i} and forum_id in(3, 5) and root_id is null and subject like '%#{search}%'" :
|
||
|
"forum_id in(3, 5) and root_id is null and subject like '%#{search}%'"
|
||
|
else
|
||
|
user_id ? "author_id = #{user_id.to_i} and forum_id in(3, 5) and root_id is null" :
|
||
|
"forum_id in(3, 5) and root_id is null"
|
||
|
end
|
||
|
|
||
|
if tag_repertoire_id
|
||
|
memo_ids = MemoTagRepertoire.where(tag_repertoire_id: tag_repertoire_id).pluck(:memo_id)
|
||
|
memo_ids = memo_ids ? memo_ids.join(",") : -1
|
||
|
sql += " and #{Memo.table_name}.id in(#{memo_ids})"
|
||
|
end
|
||
|
|
||
|
if params[:order] == "updated_at"
|
||
|
sql += " and all_replies_count != 0"
|
||
|
end
|
||
|
|
||
|
memos = Memo.field_for_list.includes(:praise_tread, :author).where("#{sql}")
|
||
|
@memos_count = memos.length
|
||
|
@memos = memos.order("sticky = 1 desc, #{Memo.table_name}.#{s_order} desc").offset(offset).limit(15)
|
||
|
@my_memos_count = Memo.user_posts(current_user.try(:id)).count
|
||
|
@tags_info = MemoTagRepertoire.find_by_sql("SELECT tag_repertoire_id, tr.name, count(*) cnt
|
||
|
FROM memo_tag_repertoires mtr join tag_repertoires tr on
|
||
|
tr.id = mtr.tag_repertoire_id group by tag_repertoire_id order by cnt desc,
|
||
|
tag_repertoire_id desc limit 9")
|
||
|
@hot_memos = Memo.field_for_recommend.posts.hot.limit(4)
|
||
|
end
|
||
|
|
||
|
# GET /memos/1
|
||
|
# GET /memos/1.json
|
||
|
def show
|
||
|
# tidding_count = unviewed_tiddings(current_user) if current_user
|
||
|
@user = current_user
|
||
|
# TODO 附件最后再做
|
||
|
# attachments_list =
|
||
|
@memo.update_column(:viewed_count, @memo.viewed_count+1)
|
||
|
@memos = @memo.reply_for_memo.includes(:praise_tread, :author).order("created_at desc").limit(10)
|
||
|
|
||
|
end
|
||
|
|
||
|
# GET /memos/new
|
||
|
def new
|
||
|
@csrf_token = session[:_csrf_toke] ||= SecureRandom.base64(32)
|
||
|
@tag_list = TagRepertoire.field_for_list.order("name asc")
|
||
|
|
||
|
end
|
||
|
|
||
|
# GET /memos/1/edit
|
||
|
def edit
|
||
|
end
|
||
|
|
||
|
# POST /memos
|
||
|
# POST /memos.json
|
||
|
def create
|
||
|
ActiveRecord::Base.transaction do
|
||
|
begin
|
||
|
@memo = Memo.new(memo_params)
|
||
|
@memo.author = current_user
|
||
|
# TODO 保存附件
|
||
|
# @memo.save_attachments(params[:attachments]) if params[:attachments]
|
||
|
@memo.save!
|
||
|
params[:tags].each do |tag|
|
||
|
MemoTagRepertoire.create(:memo_id => @memo.id, :tag_repertoire_id => tag)
|
||
|
end
|
||
|
@status = 0
|
||
|
@message = "帖子创建成功!"
|
||
|
rescue Exception => e
|
||
|
@status = -1
|
||
|
@message = "帖子创建失败,原因:#{e}"
|
||
|
raise ActiveRecord::Rollback
|
||
|
end
|
||
|
end
|
||
|
|
||
|
|
||
|
end
|
||
|
|
||
|
# PATCH/PUT /memos/1
|
||
|
# PATCH/PUT /memos/1.json
|
||
|
def update
|
||
|
respond_to do |format|
|
||
|
if @memo.update(memo_params)
|
||
|
format.html { redirect_to @memo, notice: 'Memo was successfully updated.' }
|
||
|
format.json { render :show, status: :ok, location: @memo }
|
||
|
else
|
||
|
format.html { render :edit }
|
||
|
format.json { render json: @memo.errors, status: :unprocessable_entity }
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# DELETE /memos/1
|
||
|
# DELETE /memos/1.json
|
||
|
def destroy
|
||
|
@memo.destroy
|
||
|
respond_to do |format|
|
||
|
format.html { redirect_to memos_url, notice: 'Memo was successfully destroyed.' }
|
||
|
format.json { head :no_content }
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
# Use callbacks to share common setup or constraints between actions.
|
||
|
def set_memo
|
||
|
@memo = Memo.find(params[:id])
|
||
|
end
|
||
|
|
||
|
# Never trust parameters from the scary internet, only allow the white list through.
|
||
|
def memo_params
|
||
|
params.fetch(:memo, {})
|
||
|
end
|
||
|
|
||
|
end
|