|  |  |  | @ -1,5 +1,8 @@ | 
			
		
	
		
			
				
					|  |  |  |  | class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  |   before_action :set_memo, only: [:show, :edit, :update, :destroy] | 
			
		
	
		
			
				
					|  |  |  |  |   before_action :validate_memo_params, only: [:create, :update] | 
			
		
	
		
			
				
					|  |  |  |  |   before_action :owner_or_admin, only: [:edit, :update, :destroy] | 
			
		
	
		
			
				
					|  |  |  |  |   before_action :is_admin, only: [] | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   include ApplicationHelper | 
			
		
	
		
			
				
					|  |  |  |  |   # GET /memos | 
			
		
	
	
		
			
				
					|  |  |  | @ -9,25 +12,19 @@ class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  |     @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 | 
			
		
	
		
			
				
					|  |  |  |  |     page = params[:page] || 1 | 
			
		
	
		
			
				
					|  |  |  |  |     limit = params[:limit] || 15 | 
			
		
	
		
			
				
					|  |  |  |  |     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}%'" : | 
			
		
	
		
			
				
					|  |  |  |  |           !search.blank? ? "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}%'" : | 
			
		
	
		
			
				
					|  |  |  |  |         elsif !search.blank? | 
			
		
	
		
			
				
					|  |  |  |  |           "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 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -41,27 +38,27 @@ class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  |       sql += " and all_replies_count != 0" | 
			
		
	
		
			
				
					|  |  |  |  |     end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     memos = Memo.field_for_list.includes(:praise_tread, :author).where("#{sql}") | 
			
		
	
		
			
				
					|  |  |  |  |     memos = Memo.field_for_list.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 | 
			
		
	
		
			
				
					|  |  |  |  |     @memos = memos.order("sticky = 1 desc, #{Memo.table_name}.#{s_order} desc").page(page).per(limit) | 
			
		
	
		
			
				
					|  |  |  |  |     @memos = @memos.includes(:praise_treads, :tag_repertoires, author: :user_extension) | 
			
		
	
		
			
				
					|  |  |  |  |     # @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) | 
			
		
	
		
			
				
					|  |  |  |  |     @hot_memos = Memo.field_for_recommend.posts.hot.includes(:tag_repertoires).limit(4) | 
			
		
	
		
			
				
					|  |  |  |  |     @recommend_shixuns = DiscussesService.new.recommends | 
			
		
	
		
			
				
					|  |  |  |  |   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) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     @memos = @memo.reply_for_memo.includes(:praise_treads, author: :user_extension).order("created_at desc").limit(10) | 
			
		
	
		
			
				
					|  |  |  |  |     @attachments = @memo.attachments | 
			
		
	
		
			
				
					|  |  |  |  |     @recommend_shixuns = DiscussesService.new.recommends | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   # GET /memos/new | 
			
		
	
	
		
			
				
					|  |  |  | @ -71,43 +68,44 @@ class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   # GET /memos/1/edit | 
			
		
	
		
			
				
					|  |  |  |  |   def edit | 
			
		
	
		
			
				
					|  |  |  |  |     @tag_list = TagRepertoire.field_for_list.order("name asc") | 
			
		
	
		
			
				
					|  |  |  |  |     @memo_tags = @memo.tag_repertoires.field_for_list | 
			
		
	
		
			
				
					|  |  |  |  |     @attachments = @memo.attachments | 
			
		
	
		
			
				
					|  |  |  |  |   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! | 
			
		
	
		
			
				
					|  |  |  |  |         Attachment.associate_container(params[:attachment_ids], @memo.id, @memo.class.name) | 
			
		
	
		
			
				
					|  |  |  |  |         params[:tags].each do |tag| | 
			
		
	
		
			
				
					|  |  |  |  |           MemoTagRepertoire.create(:memo_id => @memo.id, :tag_repertoire_id => tag) | 
			
		
	
		
			
				
					|  |  |  |  |           MemoTagRepertoire.create!(memo_id: @memo.id, tag_repertoire_id: tag) | 
			
		
	
		
			
				
					|  |  |  |  |         end | 
			
		
	
		
			
				
					|  |  |  |  |         @status = 0 | 
			
		
	
		
			
				
					|  |  |  |  |         @message = "帖子创建成功!" | 
			
		
	
		
			
				
					|  |  |  |  |         normal_status("帖子创建成功") | 
			
		
	
		
			
				
					|  |  |  |  |       rescue Exception => e | 
			
		
	
		
			
				
					|  |  |  |  |         @status = -1 | 
			
		
	
		
			
				
					|  |  |  |  |         @message = "帖子创建失败,原因:#{e}" | 
			
		
	
		
			
				
					|  |  |  |  |         tip_exception("帖子创建失败,原因:#{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 } | 
			
		
	
		
			
				
					|  |  |  |  |     ActiveRecord::Base.transaction do | 
			
		
	
		
			
				
					|  |  |  |  |       begin | 
			
		
	
		
			
				
					|  |  |  |  |         @memo.update_attributes!(memo_params) | 
			
		
	
		
			
				
					|  |  |  |  |         Attachment.associate_container(params[:attachment_ids], @memo.id, @memo.class.name) | 
			
		
	
		
			
				
					|  |  |  |  |         @memo.memo_tag_repertoires.destroy_all | 
			
		
	
		
			
				
					|  |  |  |  |         params[:tags].each do |tag| | 
			
		
	
		
			
				
					|  |  |  |  |           MemoTagRepertoire.create!(memo_id: @memo.id, tag_repertoire_id: tag) | 
			
		
	
		
			
				
					|  |  |  |  |         end | 
			
		
	
		
			
				
					|  |  |  |  |         normal_status("帖子更新成功") | 
			
		
	
		
			
				
					|  |  |  |  |       rescue Exception => e | 
			
		
	
		
			
				
					|  |  |  |  |         tip_exception("帖子更新失败,原因:#{e}") | 
			
		
	
		
			
				
					|  |  |  |  |         raise ActiveRecord::Rollback | 
			
		
	
		
			
				
					|  |  |  |  |       end | 
			
		
	
		
			
				
					|  |  |  |  |     end | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
	
		
			
				
					|  |  |  | @ -116,10 +114,7 @@ class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  |   # 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 | 
			
		
	
		
			
				
					|  |  |  |  |     normal_status("删除成功") | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   private | 
			
		
	
	
		
			
				
					|  |  |  | @ -128,9 +123,24 @@ class MemosController < ApplicationController | 
			
		
	
		
			
				
					|  |  |  |  |     @memo = Memo.find(params[:id]) | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   def owner_or_admin | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception(403, "无权限操作") unless @memo.author == current_user || current_user.admin? || current_user.business? | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   def is_admin | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception(403, "无权限操作") unless current_user.admin? || current_user.business? | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   # Never trust parameters from the scary internet, only allow the white list through. | 
			
		
	
		
			
				
					|  |  |  |  |   def memo_params | 
			
		
	
		
			
				
					|  |  |  |  |       params.fetch(:memo, {}) | 
			
		
	
		
			
				
					|  |  |  |  |     params.require(:memo).permit(:subject, :content, :forum_id) | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   def validate_memo_params | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception("话题名称不能为空") if params[:subject].blank? | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception("话题内容不能为空") if params[:content].blank? | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception("话题类型不能为空") if params[:forum_id].blank? | 
			
		
	
		
			
				
					|  |  |  |  |     tip_exception("技术标签不能为空") if params[:tags].blank? | 
			
		
	
		
			
				
					|  |  |  |  |   end | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | end | 
			
		
	
	
		
			
				
					|  |  |  | 
 |