modify library edit && add delete library

dev_bj
p31729568 5 years ago
parent 4cd076a926
commit 91219c9e99

@ -61,6 +61,9 @@ class ApplicationController < ActionController::Base
# rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token # rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
rescue_from ::Unauthorized, :with => :deny_access rescue_from ::Unauthorized, :with => :deny_access
rescue_from ::ActionView::MissingTemplate, :with => :missing_template rescue_from ::ActionView::MissingTemplate, :with => :missing_template
rescue_from ::ActiveRecord::RecordNotFound do
render_404
end
include Redmine::Search::Controller include Redmine::Search::Controller
include Redmine::MenuManager::MenuController include Redmine::MenuManager::MenuController

@ -21,7 +21,7 @@ class LibrariesController < ApplicationController
end end
def show def show
@library = Library.find(params[:id]) @library = current_library
return render_403 unless admin_or_self? || @library.published? return render_403 unless admin_or_self? || @library.published?
@library_applies = @library.library_applies.where(status: :refused).order('created_at desc') @library_applies = @library.library_applies.where(status: :refused).order('created_at desc')
@ -50,11 +50,13 @@ class LibrariesController < ApplicationController
end end
def edit def edit
return render_403 unless admin_or_self?
@library = current_library @library = current_library
redirect_to library_path(id: @library.id) unless @library.editable?
end end
def update def update
return render_403 unless admin_or_self?
@library = current_library @library = current_library
Libraries::SaveService.new(@library, current_user, form_params).call Libraries::SaveService.new(@library, current_user, form_params).call
if with_publish? if with_publish?
@ -71,6 +73,24 @@ class LibrariesController < ApplicationController
render 'edit' render 'edit'
end 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 def publish
Libraries::SubmitService.new(current_library).call Libraries::SubmitService.new(current_library).call
render json: { status: 0 } render json: { status: 0 }
@ -84,7 +104,7 @@ class LibrariesController < ApplicationController
private private
def current_library def current_library
@_current_library ||= current_user.libraries.find(params[:id]) @_current_library ||= Library.find(params[:id])
end end
def form_params def form_params
@ -100,6 +120,6 @@ class LibrariesController < ApplicationController
end end
def admin_or_self? 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
end end

@ -45,8 +45,4 @@ class Library < ActiveRecord::Base
def increment_visited_count! def increment_visited_count!
Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}") Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}")
end end
def editable?
pending? || refused?
end
end end

@ -17,7 +17,7 @@ class Libraries::SaveService
library.generate_uuid library.generate_uuid
end end
attachment_ids = params.delete(:attachment_ids) attachment_ids = params.delete(:attachment_ids).try(:compact)
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
library.assign_attributes(params) library.assign_attributes(params)
@ -32,6 +32,6 @@ class Libraries::SaveService
private private
def validate_params! def validate_params!
raise Error, '附件不能为空' if params[:attachment_ids].blank? raise Error, '附件不能为空' if params[:attachment_ids].try(:compact).blank?
end end
end end

@ -8,6 +8,8 @@ class Libraries::SubmitService
end end
def call def call
return if library.processing?
raise Error, '该状态下不能提交审核' unless library.may_submit? raise Error, '该状态下不能提交审核' unless library.may_submit?
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do

@ -16,6 +16,7 @@
<span class="color-grey mr10 fl"><%= number_to_human_size attachment.filesize %></span> <span class="color-grey mr10 fl"><%= number_to_human_size attachment.filesize %></span>
<%= link_to('<i class="fa fa-trash-o mr5"></i>'.html_safe, attachment_path(attachment, :attachment_id => "p#{i}", :format => 'js'), :method => 'delete', :remote => true, :class => 'remove-upload') unless attachment.id.nil? %> <%= link_to('<i class="fa fa-trash-o mr5"></i>'.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}][token]", "#{attachment.token}" %>
<%= hidden_field_tag "attachments[p#{i}][attachment_id]", attachment.id %>
</span> </span>
<% end %> <% end %>
<% container.saved_attachments.each_with_index do |attachment, i| %> <% container.saved_attachments.each_with_index do |attachment, i| %>

@ -67,5 +67,11 @@
</div> </div>
</body> </body>
<script> <script>
$(function(){
var message = '<%= flash[:message] %>';
if (message.length > 0) {
sure_confirm_box("提示",450,200,message);
}
})
</script> </script>
</html> </html>

@ -49,7 +49,7 @@
</div> </div>
<div class="operate mt20 mb20 clearfix"> <div class="operate mt20 mb20 clearfix">
<%= 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' %> <%= link_to '保存', 'javascript:void(0)', class: 'white-btn edu-blueline-btn changebtn mr20 fl submit-btn' %>
</div> </div>
<% end %> <% end %>
@ -86,43 +86,38 @@
imageUploadURL: "<%= upload_with_markdown_path(container_id: 0, container_type: 'MarkDown') %>" //url imageUploadURL: "<%= upload_with_markdown_path(container_id: 0, container_type: 'MarkDown') %>" //url
}); });
$(function(){ var submitForm = function(){
var submitForm = function(){ var title = $("input[name='library[title]']").val();
var title = $("input[name='library[title]']").val(); var content = $("textarea[name='library[content]']").val();
var content = $("textarea[name='library[content]']").val();
if (!title || title.length == 0) { if (!title || title.length == 0) {
$("#title_notice").removeClass("none"); $("#title_notice").removeClass("none");
return return
}else{ }else{
$("#title_notice").addClass("none"); $("#title_notice").addClass("none");
} }
if (!content || content.length == 0) { if (!content || content.length == 0) {
$("#des_notice").removeClass("none"); $("#des_notice").removeClass("none");
return return
}else{ }else{
$("#des_notice").addClass("none"); $("#des_notice").addClass("none");
} }
if($('.attachments_fields .attachment').length == 0){ if($('.attachments_fields .attachment').length == 0){
$("#file_notice").removeClass("none"); $("#file_notice").removeClass("none");
return return
}else{ }else{
$("#file_notice").addClass("none"); $("#file_notice").addClass("none");
} }
$('.library-form-container form').submit(); $('.library-form-container form').submit();
}; };
$(function(){
$('.apply-publish-btn').on('click', function(){ $('.apply-publish-btn').on('click', function(){
$('input[name="apply_publish"]').val(true); $("input[name='apply_publish']").val(true);
submitForm(); submitForm();
}); });
$('.submit-btn').on('click', submitForm); $('.submit-btn').on('click', submitForm);
});
var message = '<%= flash[:message] %>';
if (message.length > 0) {
sure_confirm_box("提示",450,200,message);
}
})
</script> </script>

@ -16,7 +16,7 @@
</ul> </ul>
<div class="edu-position fr"> <div class="edu-position fr">
<%= hidden_field_tag(:type, params[:type]) %> <%= hidden_field_tag(:type, params[:type]) %>
<input class="winput-240-30 panel-box-sizing" placeholder="输入教学案例标题、编号进行检索" type="text" id="search_name"> <input class="winput-300-35 panel-box-sizing" placeholder="输入教学案例标题、编号进行检索" type="text" id="search_name">
<a href="javascript:void(0);" class="edu-btn-search font-16 color-grey" id="search"><i class="fa fa-search"></i></a> <a href="javascript:void(0);" class="edu-btn-search font-16 color-grey" id="search"><i class="fa fa-search"></i></a>
</div> </div>
</div> </div>
@ -36,7 +36,7 @@
var search = $("#search_name").val(); var search = $("#search_name").val();
var type = $("input[name='type']").val(); var type = $("input[name='type']").val();
$.ajax({ $.ajax({
url: "/libraries", url: "<%= libraries_path %>",
dataType: 'script', dataType: 'script',
data: {search: search, type: type} data: {search: search, type: type}
}); });

@ -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
%> %>
<div class="educontent mb50"> <div class="educontent mb50">
<p class="mt10 mb20 clearfix lineh-20"> <p class="mt10 mb20 clearfix lineh-20">
@ -42,7 +42,11 @@
<div class="padding30"> <div class="padding30">
<p class="mb10 clearfix"> <p class="mb10 clearfix">
<span class="color-grey-6 font-16 mr10">详情</span> <span class="color-grey-6 font-16 mr10">详情</span>
<% 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' %> <%= link_to '编辑', edit_library_path(id: @library.id), class: 'white-btn edu-blueline-btn fr' %>
<% end %> <% end %>
</p> </p>
@ -99,13 +103,35 @@
sequenceDiagram: true // 默认不解析 sequenceDiagram: true // 默认不解析
}); });
$(function(){ $(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 height=document.getElementById("private_reason_ul").offsetHeight;
var one=parseInt($("#private_reason_ul").find("li").eq(0).height())+10; var one=parseInt($("#private_reason_ul").find("li").eq(0).height())+10;
var two=parseInt($("#private_reason_ul").find("li").eq(1).height()); var two=parseInt($("#private_reason_ul").find("li").eq(1).height());
var plus=one+two; var plus=one+two;
if(parseInt(height)>166){ if(parseInt(height)>166){
$("#actionPanel").html('点击展开<i class="iconfont icon-xiajiantou color-blue font-14 ml5"></i>'); $("#actionPanel").html('点击展开<i class="iconfont icon-xiajiantou color-blue font-14 ml5"></i>');
$(".private_reason").css({maxHeight:plus+"px"}); $(".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);
}
}
});
}
</script> </script>
Loading…
Cancel
Save