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

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

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

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

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

@ -16,6 +16,7 @@
<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? %>
<%= hidden_field_tag "attachments[p#{i}][token]", "#{attachment.token}" %>
<%= hidden_field_tag "attachments[p#{i}][attachment_id]", attachment.id %>
</span>
<% end %>
<% container.saved_attachments.each_with_index do |attachment, i| %>

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

@ -49,7 +49,7 @@
</div>
<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' %>
</div>
<% end %>
@ -86,7 +86,6 @@
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();
@ -114,15 +113,11 @@
$('.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);
}
})
});
</script>

@ -16,7 +16,7 @@
</ul>
<div class="edu-position fr">
<%= 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>
</div>
</div>
@ -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}
});

@ -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">
<p class="mt10 mb20 clearfix lineh-20">
@ -42,7 +42,11 @@
<div class="padding30">
<p class="mb10 clearfix">
<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' %>
<% end %>
</p>
@ -99,6 +103,13 @@
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());
@ -107,5 +118,20 @@
$("#actionPanel").html('点击展开<i class="iconfont icon-xiajiantou color-blue font-14 ml5"></i>');
$(".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>
Loading…
Cancel
Save