diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 601ebb8a..0e783c74 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -61,6 +61,9 @@ class ApplicationController < ActionController::Base # rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token rescue_from ::Unauthorized, :with => :deny_access rescue_from ::ActionView::MissingTemplate, :with => :missing_template + rescue_from ::ActiveRecord::RecordNotFound do + render_404 + end include Redmine::Search::Controller include Redmine::MenuManager::MenuController diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index f3559b05..68b36df0 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -21,7 +21,7 @@ class LibrariesController < ApplicationController end def show - @library = Library.find(params[:id]) + @library = current_library return render_403 unless admin_or_self? || @library.published? @library_applies = @library.library_applies.where(status: :refused).order('created_at desc') @@ -50,11 +50,13 @@ class LibrariesController < ApplicationController end def edit + return render_403 unless admin_or_self? @library = current_library - redirect_to library_path(id: @library.id) unless @library.editable? end def update + return render_403 unless admin_or_self? + @library = current_library Libraries::SaveService.new(@library, current_user, form_params).call if with_publish? @@ -71,6 +73,24 @@ class LibrariesController < ApplicationController render 'edit' end + def destroy + if admin_or_business? + current_library.destroy + elsif current_library.user_id == current_user.id + unless current_library.pending? + render json: { status: -1, message: '只有草稿才能删除' } + return + end + + current_library.destroy + else + render_403 + return + end + + render json: { status: 0, message: 'success' } + end + def publish Libraries::SubmitService.new(current_library).call render json: { status: 0 } @@ -84,7 +104,7 @@ class LibrariesController < ApplicationController private def current_library - @_current_library ||= current_user.libraries.find(params[:id]) + @_current_library ||= Library.find(params[:id]) end def form_params @@ -100,6 +120,6 @@ class LibrariesController < ApplicationController end def admin_or_self? - @library.user_id == current_user.id || current_user.admin? + current_library.user_id == current_user.id || admin_or_business? end end \ No newline at end of file diff --git a/app/models/library.rb b/app/models/library.rb index 78bc7fc5..63df5c91 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -45,8 +45,4 @@ class Library < ActiveRecord::Base def increment_visited_count! Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}") end - - def editable? - pending? || refused? - end end \ No newline at end of file diff --git a/app/services/libraries/save_service.rb b/app/services/libraries/save_service.rb index 3a81d379..db244836 100644 --- a/app/services/libraries/save_service.rb +++ b/app/services/libraries/save_service.rb @@ -17,7 +17,7 @@ class Libraries::SaveService library.generate_uuid end - attachment_ids = params.delete(:attachment_ids) + attachment_ids = params.delete(:attachment_ids).try(:compact) ActiveRecord::Base.transaction do library.assign_attributes(params) @@ -32,6 +32,6 @@ class Libraries::SaveService private def validate_params! - raise Error, '附件不能为空' if params[:attachment_ids].blank? + raise Error, '附件不能为空' if params[:attachment_ids].try(:compact).blank? end end diff --git a/app/services/libraries/submit_service.rb b/app/services/libraries/submit_service.rb index 965ba673..6af3284b 100644 --- a/app/services/libraries/submit_service.rb +++ b/app/services/libraries/submit_service.rb @@ -8,6 +8,8 @@ class Libraries::SubmitService end def call + return if library.processing? + raise Error, '该状态下不能提交审核' unless library.may_submit? ActiveRecord::Base.transaction do diff --git a/app/views/attachments/_from_libraries.html.erb b/app/views/attachments/_from_libraries.html.erb index 4abb3c80..a6fe347d 100644 --- a/app/views/attachments/_from_libraries.html.erb +++ b/app/views/attachments/_from_libraries.html.erb @@ -16,6 +16,7 @@ <%= number_to_human_size attachment.filesize %> <%= link_to(''.html_safe, attachment_path(attachment, :attachment_id => "p#{i}", :format => 'js'), :method => 'delete', :remote => true, :class => 'remove-upload') unless attachment.id.nil? %> <%= hidden_field_tag "attachments[p#{i}][token]", "#{attachment.token}" %> + <%= hidden_field_tag "attachments[p#{i}][attachment_id]", attachment.id %> <% end %> <% container.saved_attachments.each_with_index do |attachment, i| %> diff --git a/app/views/layouts/base_library.html.erb b/app/views/layouts/base_library.html.erb index 766069bb..ebc56f4d 100644 --- a/app/views/layouts/base_library.html.erb +++ b/app/views/layouts/base_library.html.erb @@ -67,5 +67,11 @@ \ No newline at end of file diff --git a/app/views/libraries/_form.html.erb b/app/views/libraries/_form.html.erb index a1692e2b..86abea58 100644 --- a/app/views/libraries/_form.html.erb +++ b/app/views/libraries/_form.html.erb @@ -49,7 +49,7 @@
- <%= link_to '确认提交', 'javascript:void(0)', class: 'white-btn edu-blueback-btn changebtn mr20 fl apply-publish-btn' %> + <%= link_to '申请发布', 'javascript:void(0)', class: 'white-btn edu-blueback-btn changebtn mr20 fl apply-publish-btn' %> <%= link_to '保存', 'javascript:void(0)', class: 'white-btn edu-blueline-btn changebtn mr20 fl submit-btn' %>
<% end %> @@ -86,43 +86,38 @@ imageUploadURL: "<%= upload_with_markdown_path(container_id: 0, container_type: 'MarkDown') %>" //url }); - $(function(){ - var submitForm = function(){ - var title = $("input[name='library[title]']").val(); - var content = $("textarea[name='library[content]']").val(); + var submitForm = function(){ + var title = $("input[name='library[title]']").val(); + var content = $("textarea[name='library[content]']").val(); - if (!title || title.length == 0) { - $("#title_notice").removeClass("none"); - return - }else{ - $("#title_notice").addClass("none"); - } - if (!content || content.length == 0) { - $("#des_notice").removeClass("none"); - return - }else{ - $("#des_notice").addClass("none"); - } + if (!title || title.length == 0) { + $("#title_notice").removeClass("none"); + return + }else{ + $("#title_notice").addClass("none"); + } + if (!content || content.length == 0) { + $("#des_notice").removeClass("none"); + return + }else{ + $("#des_notice").addClass("none"); + } - if($('.attachments_fields .attachment').length == 0){ - $("#file_notice").removeClass("none"); - return - }else{ - $("#file_notice").addClass("none"); - } + if($('.attachments_fields .attachment').length == 0){ + $("#file_notice").removeClass("none"); + return + }else{ + $("#file_notice").addClass("none"); + } - $('.library-form-container form').submit(); - }; + $('.library-form-container form').submit(); + }; + $(function(){ $('.apply-publish-btn').on('click', function(){ - $('input[name="apply_publish"]').val(true); + $("input[name='apply_publish']").val(true); submitForm(); }); $('.submit-btn').on('click', submitForm); - - var message = '<%= flash[:message] %>'; - if (message.length > 0) { - sure_confirm_box("提示",450,200,message); - } - }) + }); \ No newline at end of file diff --git a/app/views/libraries/index.html.erb b/app/views/libraries/index.html.erb index 7c8d099f..cc34dc81 100644 --- a/app/views/libraries/index.html.erb +++ b/app/views/libraries/index.html.erb @@ -16,7 +16,7 @@
<%= hidden_field_tag(:type, params[:type]) %> - +
@@ -36,7 +36,7 @@ var search = $("#search_name").val(); var type = $("input[name='type']").val(); $.ajax({ - url: "/libraries", + url: "<%= libraries_path %>", dataType: 'script', data: {search: search, type: type} }); diff --git a/app/views/libraries/show.html.erb b/app/views/libraries/show.html.erb index 5b196712..9de5e65e 100644 --- a/app/views/libraries/show.html.erb +++ b/app/views/libraries/show.html.erb @@ -1,5 +1,5 @@ <% - admin_or_self = User.current.admin? || @library.user_id == User.current.id + admin_or_self = admin_or_business? || @library.user_id == User.current.id %>

@@ -42,7 +42,11 @@

详情 - <% if admin_or_self && @library.editable? %> + <% if admin_or_business? || @library.user_id == User.current.id && @Library.pending? %> + <%= link_to '删除', 'javascript:void(0);', data: { id: @library.id }, + class: 'white-btn edu-blueline-btn fr ml20 delete-btn' %> + <% end %> + <% if admin_or_self %> <%= link_to '编辑', edit_library_path(id: @library.id), class: 'white-btn edu-blueline-btn fr' %> <% end %>

@@ -99,13 +103,35 @@ sequenceDiagram: true // 默认不解析 }); $(function(){ + // 删除 + $('.delete-btn').on('click', function(){ + var id = $(this).data('id'); + op_confirm_tip_1("是否确认删除?", "destroyLibrary(" + id + ");") + }); + + if ($('#private_reason_ul').length != 0) { var height=document.getElementById("private_reason_ul").offsetHeight; var one=parseInt($("#private_reason_ul").find("li").eq(0).height())+10; var two=parseInt($("#private_reason_ul").find("li").eq(1).height()); var plus=one+two; if(parseInt(height)>166){ - $("#actionPanel").html('点击展开'); - $(".private_reason").css({maxHeight:plus+"px"}); + $("#actionPanel").html('点击展开'); + $(".private_reason").css({maxHeight:plus+"px"}); } + } }) + + function destroyLibrary (id) { + $.ajax({ + type: "DELETE", + url: "<%= libraries_path %>/" + id, + success: function (data) { + if(data && data.status == 0){ + notice_box_redirect("<%= libraries_path %>", "删除成功"); + } else { + notice_box(data.message); + } + } + }); + } \ No newline at end of file