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 @@