From 07e3cf3c862abbb3384b50d3baa3461ec1e7d9ba Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 25 Jun 2019 18:29:03 +0800 Subject: [PATCH 1/2] modify code style --- app/controllers/attachments_controller.rb | 64 +++++++++++------------ app/controllers/boards_controller.rb | 43 +++++++-------- 2 files changed, 49 insertions(+), 58 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 0a89bb3f9..bf5c11856 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -25,51 +25,49 @@ class AttachmentsController < ApplicationController # 2. 上传到云 upload_file = params["file"] || params["#{params[:file_param_name]}"] # 这里的file_param_name是为了方便其他插件名称 uid_logger("#########################file_params####{params["#{params[:file_param_name]}"]}") - if upload_file - folder = edu_setting('attachment_folder') - raise "存储目录未定义" unless folder.present? + raise "未上传文件" unless upload_file - month_folder = current_month_folder - save_path = File.join(folder, month_folder) + folder = edu_setting('attachment_folder') + raise "存储目录未定义" unless folder.present? - ext = file_ext(upload_file.original_filename) + month_folder = current_month_folder + save_path = File.join(folder, month_folder) - local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext) + ext = file_ext(upload_file.original_filename) - content_type = upload_file.content_type.presence || 'application/octet-stream' + local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext) - remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type) + content_type = upload_file.content_type.presence || 'application/octet-stream' - logger.info "local_path: #{local_path}" - logger.info "remote_path: #{remote_path}" + remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type) + logger.info "local_path: #{local_path}" + logger.info "remote_path: #{remote_path}" - disk_filename = local_path[save_path.size + 1, local_path.size] - #存数据库 - # - @attachment = Attachment.where(disk_filename: disk_filename, - author_id: current_user.id, - cloud_url: remote_path).first - unless @attachment.present? - @attachment = Attachment.new - @attachment.filename = upload_file.original_filename - @attachment.disk_filename = local_path[save_path.size + 1, local_path.size] - @attachment.filesize = upload_file.tempfile.size - @attachment.content_type = content_type - @attachment.digest = digest - @attachment.author_id = current_user.id - @attachment.disk_directory = month_folder - @attachment.cloud_url = remote_path - @attachment.save! - else - logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" - end + disk_filename = local_path[save_path.size + 1, local_path.size] + #存数据库 + # + @attachment = Attachment.where(disk_filename: disk_filename, + author_id: current_user.id, + cloud_url: remote_path).first - render_json + if @attachment.blank? + @attachment = Attachment.new + @attachment.filename = upload_file.original_filename + @attachment.disk_filename = local_path[save_path.size + 1, local_path.size] + @attachment.filesize = upload_file.tempfile.size + @attachment.content_type = content_type + @attachment.digest = digest + @attachment.author_id = current_user.id + @attachment.disk_directory = month_folder + @attachment.cloud_url = remote_path + @attachment.save! else - raise "未上传文件" + logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" end + + render_json end def destroy diff --git a/app/controllers/boards_controller.rb b/app/controllers/boards_controller.rb index 51c8397b1..2e82c36d7 100644 --- a/app/controllers/boards_controller.rb +++ b/app/controllers/boards_controller.rb @@ -9,46 +9,39 @@ class BoardsController < ApplicationController end def show - - end - - def new end def create ActiveRecord::Base.transaction do - begin - board = @course.course_board - new_board = Board.new(board_params) - new_board.course_id = @course.id - new_board.project_id = -1 - new_board.parent_id = board.try(:id) - new_board.position = board.children.count + 1 - new_board.save! - normal_status(0, "添加成功") - rescue Exception => e - uid_logger_error(e.message) - tip_exception(e.message) - raise ActiveRecord::Rollback - end + board = @course.course_board + new_board = Board.new(board_params) + new_board.course_id = @course.id + new_board.project_id = -1 + new_board.parent_id = board.try(:id) + new_board.position = board.children.count + 1 + new_board.save! end + + normal_status(0, "添加成功") end # 子目录的拖动 def move_category tip_exception("移动失败") if params[:position].blank? - unless params[:position].to_i == @board.position - course_board = @course.course_board + return normal_status(-1, "位置没有变化") if params[:position].to_i == @board.position + + course_board = @course.course_board + ActiveRecord::Base.transaction do if params[:position].to_i < @board.position - course_board.children.where("position < #{@board.position} and position >= ?", params[:position]).update_all("position = position + 1") + course_board.children.where("position < ? and position >= ?", @board.position, params[:position]) + .update_all("position = position + 1") else - course_board.children.where("position > #{@board.position} and position <= ?", params[:position]).update_all("position = position - 1") + course_board.children.where("position > ? and position <= ?", @board.position, params[:position]) + .update_all("position = position - 1") end @board.update_attributes(position: params[:position]) - normal_status(0, "移动成功") - else - normal_status(-1, "位置没有变化") end + normal_status(0, "移动成功") end def destroy From d21c46339bb23595d32b04de9da259e762a56491 Mon Sep 17 00:00:00 2001 From: jasder Date: Wed, 26 Jun 2019 09:58:59 +0800 Subject: [PATCH 2/2] ADD competition some eelated featrue --- app/controllers/messages_controller.rb | 8 +++++++- app/models/message.rb | 2 +- app/views/messages/reply_list.json.jbuilder | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 60a1c171c..33f4e5bfb 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -43,9 +43,15 @@ class MessagesController < ApplicationController @page_size = params[:page_size] || 10 @current_user = current_user || nil - @messages = @message.children.preload_messages + @messages = @message.children.preload_messages.includes(:message_detail, :praise_treads) @messages = @messages.ordered(sort: 1) unless @message.parent_id.nil? + @user_course_identity = current_user.course_identity(@message.board.course) + case @user_course_identity + when 5 || 6 || 7 + @messages = @messages.visible + end + @messages = @messages.page(@page).per(@page_size) end diff --git a/app/models/message.rb b/app/models/message.rb index c3b3bca40..6b41cdd95 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -18,7 +18,7 @@ class Message < ApplicationRecord scope :root_nodes, -> { where("parent_id IS NULL") } #判断该信息是帖子还是回复。null为发布的帖子 scope :reply_nodes, -> { where("parent_id IS NOT NULL") } - scope :visible, -> { where(is_hidden: false)} + scope :visible, -> { where(is_hidden: false) } scope :by_user, ->(user) { visible if user.nil? || !user.admin? } scope :preload_messages, -> { includes(:author, :message_detail) } scope :short, -> { select(:id, :subject, :created_on, :replies_count, :visits, :sticky, :praises_count) } diff --git a/app/views/messages/reply_list.json.jbuilder b/app/views/messages/reply_list.json.jbuilder index 2b7ef7178..b5c1ce256 100644 --- a/app/views/messages/reply_list.json.jbuilder +++ b/app/views/messages/reply_list.json.jbuilder @@ -1,6 +1,6 @@ json.partial! "commons/success" json.data do - json.user_course_identity @current_user.course_identity(@message.board.course) + json.user_course_identity @user_course_identity json.id @message.id json.total_count @messages.total_count json.total_replies_count @message.total_replies_count