parent
088c9fc4a1
commit
cefa6fa68b
@ -0,0 +1,61 @@
|
|||||||
|
class LibrariesController < ApplicationController
|
||||||
|
before_filter :require_login
|
||||||
|
|
||||||
|
def index
|
||||||
|
libraries = Library.where(nil)
|
||||||
|
|
||||||
|
libraries =
|
||||||
|
if params[:filter] == 'mine'
|
||||||
|
libraries.where(user_id: current_user.id).order('created_at desc')
|
||||||
|
else
|
||||||
|
libraries.where(status: :published).order('visited_count desc')
|
||||||
|
end
|
||||||
|
|
||||||
|
search = params[:search].to_s.strip
|
||||||
|
libraries = libraries.where('title LIKE ?', "%#{search}%") if search.present?
|
||||||
|
|
||||||
|
@libraries = paginateHelper libraries.includes(user: :user_extensions)
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@library = Library.find(params[:id])
|
||||||
|
@library.increment_visited_count!
|
||||||
|
end
|
||||||
|
|
||||||
|
def new
|
||||||
|
@library = current_user.libraries.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@library = current_user.libraries.new
|
||||||
|
Libraries::SaveService.new(@library, current_user, params).call
|
||||||
|
redirect_to library_path(id: @library.id)
|
||||||
|
rescue ActiveRecord::RecordInvalid => _
|
||||||
|
render 'new'
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit
|
||||||
|
@library = current_library
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@library = current_library
|
||||||
|
Libraries::SaveService.new(@library, current_user, params).call
|
||||||
|
redirect_to library_path(id: @library.id)
|
||||||
|
rescue ActiveRecord::RecordInvalid => _
|
||||||
|
render 'edit'
|
||||||
|
end
|
||||||
|
|
||||||
|
def publish
|
||||||
|
Libraries::SubmitService.new(current_library).call
|
||||||
|
render json: { status: 0 }
|
||||||
|
rescue Libraries::SubmitService::Error => ex
|
||||||
|
render json: { status: 0, message: ex.message }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_library
|
||||||
|
@_current_library ||= current_user.libraries.find(params[:id])
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,47 @@
|
|||||||
|
class Managements::LibraryAppliesController < Managements::BaseController
|
||||||
|
before_filter :set_menu_type
|
||||||
|
|
||||||
|
def index
|
||||||
|
applies = LibraryApply.order('library_applies.updated_at desc')
|
||||||
|
|
||||||
|
search = params[:search].to_s.strip
|
||||||
|
if search.present?
|
||||||
|
applies = applies.joins(:library)
|
||||||
|
.where('libraries.uuid like :search or libraries.title like :search', search: "%#{search}%")
|
||||||
|
end
|
||||||
|
|
||||||
|
applies = applies.where(status: params[:status].presence || :pending)
|
||||||
|
|
||||||
|
@library_applies = paginateHelper applies.includes(library: { user: :user_extensions })
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
format.html
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def agree
|
||||||
|
Libraries::AgreeApplyService.new(current_library_apply, current_user).call
|
||||||
|
render json: { status: 0 }
|
||||||
|
rescue Libraries::AgreeApplyService::Error => e
|
||||||
|
render json: { status: -1, message: e.message }
|
||||||
|
end
|
||||||
|
|
||||||
|
def refuse
|
||||||
|
Libraries::RefuseApplyService.new(current_library_apply, current_user, reason: params[:reason]).call
|
||||||
|
render json: { status: 0 }
|
||||||
|
rescue Libraries::RefuseApplyService::Error => e
|
||||||
|
render json: { status: -1, message: e.message }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_library_apply
|
||||||
|
@_current_library_apply ||= LibraryApply.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_menu_type
|
||||||
|
@menu_type = 10
|
||||||
|
@sub_type = 8
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,7 @@
|
|||||||
|
module Util
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def generate_time_uuid
|
||||||
|
"#{Time.zone.now.strftime('%Y%m%d%H%M%S')}#{Random.rand(10**8).to_i}"
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,46 @@
|
|||||||
|
class Library < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
belongs_to :user
|
||||||
|
|
||||||
|
has_many :library_applies, dependent: :delete_all
|
||||||
|
has_many :attachments, as: :container
|
||||||
|
|
||||||
|
attr_accessible :title, :content
|
||||||
|
|
||||||
|
validates :title, presence: true
|
||||||
|
validates :content, presence: true
|
||||||
|
validates :uuid, presence: true, uniqueness: true
|
||||||
|
|
||||||
|
aasm(:status) do
|
||||||
|
state :pending, initiali: true
|
||||||
|
state :processing
|
||||||
|
state :refused
|
||||||
|
state :published
|
||||||
|
|
||||||
|
event :submit do
|
||||||
|
transitions from: [:pending, :refused], to: :processing
|
||||||
|
end
|
||||||
|
|
||||||
|
event :refuse do
|
||||||
|
transitions from: :processing, to: :refused
|
||||||
|
end
|
||||||
|
|
||||||
|
event :publish do
|
||||||
|
transitions from: :processing, to: :published
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_uuid
|
||||||
|
uuid = Util.generate_time_uuid
|
||||||
|
while Library.exists?(uuid: uuid)
|
||||||
|
uuid = Util.generate_time_uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
self.uuid = uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
def increment_visited_count!
|
||||||
|
Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}")
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,19 @@
|
|||||||
|
class LibraryApply < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
belongs_to :library
|
||||||
|
|
||||||
|
aasm(:status) do
|
||||||
|
state :pending, initiali: true
|
||||||
|
state :refused
|
||||||
|
state :agreed
|
||||||
|
|
||||||
|
event :refuse do
|
||||||
|
transitions from: :pending, to: :refused
|
||||||
|
end
|
||||||
|
|
||||||
|
event :agree do
|
||||||
|
transitions from: :pending, to: :agreed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,32 @@
|
|||||||
|
class Libraries::AgreeApplyService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :library_apply, :library, :user
|
||||||
|
|
||||||
|
def initialize(library_apply, user)
|
||||||
|
@library_apply = library_apply
|
||||||
|
@library = library_apply.library
|
||||||
|
@user = user
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
raise Error, '该状态下不能进行此操作' unless library_apply.may_agree?
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
library_apply.agree!
|
||||||
|
library_apply.library.publish!
|
||||||
|
|
||||||
|
# 将消息改为已处理
|
||||||
|
Tiding.where(container_id: library.id, container_type: 'Library', tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||||
|
notify_library_author!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def notify_library_author!
|
||||||
|
Tiding.create!(user_id: library.user_id, trigger_user_id: 1,
|
||||||
|
container_id: library.id, container_type: 'Library',
|
||||||
|
tiding_type: 'System', status: 1)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,37 @@
|
|||||||
|
class Libraries::RefuseApplyService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :library_apply, :library, :user, :params
|
||||||
|
|
||||||
|
def initialize(library_apply, user, params)
|
||||||
|
@library_apply = library_apply
|
||||||
|
@library = library_apply.library
|
||||||
|
@user = user
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
reason = params[:reason].to_s.strip
|
||||||
|
raise Error, '原因不能为空' if reason.blank?
|
||||||
|
raise Error, '该状态下不能进行此操作' unless library_apply.may_refuse?
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
library_apply.reason = reason
|
||||||
|
library_apply.refused_at = Time.current
|
||||||
|
library_apply.refuse
|
||||||
|
library_apply.save!
|
||||||
|
|
||||||
|
# 将消息改为已处理
|
||||||
|
Tiding.where(container_id: library.id, container_type: 'Library', tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||||
|
notify_library_author!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def notify_library_author!
|
||||||
|
Tiding.create!(user_id: library.user_id, trigger_user_id: 1,
|
||||||
|
container_id: library.id, container_type: 'Library',
|
||||||
|
tiding_type: 'System', status: 2, extra: library_apply.reason)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,37 @@
|
|||||||
|
class Libraries::SaveService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :library, :user, :params
|
||||||
|
|
||||||
|
def initialize(library, user, params)
|
||||||
|
@library = library
|
||||||
|
@user = user
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
validate_params!
|
||||||
|
|
||||||
|
if library.new_record?
|
||||||
|
library.user_id = user.id
|
||||||
|
library.generate_uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
attachment_ids = params.delete(:attachment_ids)
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
library.assign_attributes(params)
|
||||||
|
library.save!
|
||||||
|
|
||||||
|
Attachment.where(id: attachment_ids).update_all(container_id: library.id, container_type: 'Library')
|
||||||
|
end
|
||||||
|
|
||||||
|
library
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def validate_params!
|
||||||
|
raise Error, '附件不能为空' if params[:attachment_ids].blank?
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,31 @@
|
|||||||
|
class Libraries::SubmitService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :library
|
||||||
|
|
||||||
|
def initialize(library)
|
||||||
|
@library = library
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
raise Error, '该状态下不能提交审核' unless library.may_submit?
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
library.published_at = Time.current
|
||||||
|
library.submit
|
||||||
|
library.save!
|
||||||
|
|
||||||
|
library.library_applies.create!
|
||||||
|
send_library_apply_notify!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def send_library_apply_notify!
|
||||||
|
Tiding.create!(user_id: 1, trigger_user_id: library.user_id,
|
||||||
|
container_id: library.id, container_type: 'Library',
|
||||||
|
tiding_type: 'Apply', status: 0)
|
||||||
|
Trustie::Sms.send(mobile: '18711011226', send_type:'publish_library' , name: '管理员') rescue nil
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,120 @@
|
|||||||
|
<div class="edu-class-container mb15">
|
||||||
|
<div class="edu-con-top clearfix">
|
||||||
|
<p class="ml15 fl color-grey">文库发布</p>
|
||||||
|
</div>
|
||||||
|
<div class="edu-con-bg01 mt15">
|
||||||
|
<div class="edu-tab clearfix mb20">
|
||||||
|
<ul id="edu-tab-nav" class="border-bottom-orange">
|
||||||
|
<li id="edu-tab-nav-1" class="new-tab-nav background-orange" onclick="HoverLi(1);">
|
||||||
|
<%= link_to "待审批", library_applies_path(status: :pending), class: 'tab_type', remote: true %>
|
||||||
|
</li>
|
||||||
|
<li id="edu-tab-nav-2" class="new-tab-nav" onclick="HoverLi(2);">
|
||||||
|
<%= link_to "已审批", library_applies_path(status: [:refused, :agreed]), class: 'tab_type', remote: true %>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<div class="cl"></div>
|
||||||
|
<div id="edu-tab-con-1">
|
||||||
|
<div class="mt10">
|
||||||
|
<div class="edu-position fr task-form-30 mb10 mr15">
|
||||||
|
<input class="task-form-100 panel-box-sizing" placeholder="输入文库标题、编号进行检索" type="text" id="search_name">
|
||||||
|
<a href="javascript:void(0);" class="edu-btn-search font-16 color-grey mt10" id="search"><i class="fa fa-search"></i></a>
|
||||||
|
</div>
|
||||||
|
<div class="cl"></div>
|
||||||
|
<div id="authentication_list" class="auth_table">
|
||||||
|
<%= render :partial => "managements/library_applies/library_apply_list"%>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="edu-tab-con-2" class="undis">
|
||||||
|
<div class="mt10">
|
||||||
|
<p class="fl task-form-60 mt8 ml15 clearfix">
|
||||||
|
<%= link_to "全部", library_applies_path(status: [:refused, :agreed]), :class => "edu-filter-cir-grey mr5 fl font-12 active", :id => "library_all_authentication", :remote => true %>
|
||||||
|
<%= link_to "同意", library_applies_path(status: :agreed), :class => "edu-filter-cir-grey mr5 fl font-12", :id => "library_agree_authentication", :remote => true %>
|
||||||
|
<%= link_to "拒绝", library_applies_path(status: :refused), :class => "edu-filter-cir-grey mr5 fl font-12", :id => "library_reject_authentication", :remote => true %>
|
||||||
|
</p>
|
||||||
|
<div class="edu-position fr task-form-30 mb10 fr mr15">
|
||||||
|
<input class="task-form-100 panel-box-sizing" placeholder="输入文库标题、编号进行检索" type="text" id="library_search_name">
|
||||||
|
<a href="javascript:void(0);" class="edu-btn-search font-16 color-grey mt10" id="library_search"><i class="fa fa-search"></i></a>
|
||||||
|
</div>
|
||||||
|
<div class="cl"></div>
|
||||||
|
<div id="library_authentication_list" class="auth_table">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cl"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
/* -------------------------- 拒绝 ------------------------------------ */
|
||||||
|
function reject_library_authentication_reason(nThis){
|
||||||
|
var reason = $(nThis).parent().parent().find('div');
|
||||||
|
reason.find("input").val("");
|
||||||
|
reason.toggle();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------- 取消 ------------------------------------ */
|
||||||
|
function library_hide_reject_reason(nThis){
|
||||||
|
var reason = $(nThis).parent().parent();
|
||||||
|
reason.find("input").val("");
|
||||||
|
reason.hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------- 提交拒绝原因 --------------------------------- */
|
||||||
|
function library_submit_reject_reason(id, nThis){
|
||||||
|
var nReason = $(nThis).parent().parent();
|
||||||
|
var reason = nReason.find("input").val();
|
||||||
|
if (reason == '') {
|
||||||
|
alert('请输入原因');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$.ajax({
|
||||||
|
url: '/managements/library_applies/' + id + '/refuse',
|
||||||
|
type: 'post',
|
||||||
|
data: {reason: reason},
|
||||||
|
success: function(data){
|
||||||
|
if (data && data.status != -1) {
|
||||||
|
$('#authentication_list .admin-con-box.apply-' + id).remove();
|
||||||
|
|
||||||
|
if($('#authentication_list .admin-con-box').length < 5){
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
alert(data.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------- 按名字进行搜索(未审批) ----------------------------- */
|
||||||
|
$("#search").live("click", function(){
|
||||||
|
var iName = $("#search_name").val();
|
||||||
|
$.ajax({
|
||||||
|
url: "/managements/library_applies",
|
||||||
|
dataType: 'script',
|
||||||
|
data: { search: iName, status: 'pending' }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* ------------------- 按名字进行搜索(已审批)-------------------- */
|
||||||
|
$("#library_search").live("click", function(){
|
||||||
|
var iName = $("#library_search_name").val();
|
||||||
|
var id = $("#library_all_authentication").parent().find(".active").attr("id");
|
||||||
|
var status = '';
|
||||||
|
|
||||||
|
if(id == "library_all_authentication"){
|
||||||
|
status = ['refused', 'agreed'];
|
||||||
|
}else if(id=="library_agree_authentication"){
|
||||||
|
status = 'agreed';
|
||||||
|
}else{
|
||||||
|
status = 'refused';
|
||||||
|
}
|
||||||
|
$.ajax({
|
||||||
|
url: "/managements/library_applies",
|
||||||
|
dataType: 'script',
|
||||||
|
data: { search: iName, status: status}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
@ -0,0 +1,30 @@
|
|||||||
|
var nTabIcon_1 = $("#edu-tab-con-1");
|
||||||
|
var nTabIcon_2 = $("#edu-tab-con-2");
|
||||||
|
var nTabNav_1 = $("#edu-tab-nav-1");
|
||||||
|
var nTabNav_2 = $("#edu-tab-nav-2");
|
||||||
|
var nAudit = $("#library_all_authentication").parent();
|
||||||
|
|
||||||
|
<% if params[:status].to_s == 'pending' %>
|
||||||
|
$("#authentication_list").html("<%= j( render :partial => "managements/library_applies/library_apply_list" ) %>");
|
||||||
|
nTabNav_1.addClass("background-orange");
|
||||||
|
nTabNav_2.removeClass("background-orange");
|
||||||
|
nTabIcon_1.show();
|
||||||
|
nTabIcon_2.hide();
|
||||||
|
<% else %>
|
||||||
|
$("#library_authentication_list").html("<%= j( render :partial => "managements/library_applies/library_apply_list" ) %>");
|
||||||
|
nTabNav_1.removeClass("background-orange");
|
||||||
|
nTabNav_2.addClass("background-orange");
|
||||||
|
nTabIcon_1.hide();
|
||||||
|
nTabIcon_2.show();
|
||||||
|
/* -------------------------- 未审批(全部、同意、拒绝点击时动态样式) ------------------------------ */
|
||||||
|
if(<%= params[:status].to_s == 'agreed' %>){
|
||||||
|
nAudit.find(".active").removeClass("active");
|
||||||
|
$("#library_agree_authentication").addClass("active");
|
||||||
|
}else if(<%= params[:status].to_s == 'refused' %>){
|
||||||
|
nAudit.find(".active").removeClass("active");
|
||||||
|
$("#library_reject_authentication").addClass("active");
|
||||||
|
}else{
|
||||||
|
nAudit.find(".active").removeClass("active");
|
||||||
|
$("#library_all_authentication").addClass("active");
|
||||||
|
}
|
||||||
|
<% end %>
|
@ -0,0 +1,8 @@
|
|||||||
|
'zh':
|
||||||
|
activerecord:
|
||||||
|
models:
|
||||||
|
library: '文库'
|
||||||
|
attributes:
|
||||||
|
library:
|
||||||
|
title: '标题'
|
||||||
|
content: '描述'
|
@ -0,0 +1,18 @@
|
|||||||
|
class CreateLibraries < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :libraries do |t|
|
||||||
|
t.references :user
|
||||||
|
|
||||||
|
t.string :title
|
||||||
|
t.text :content
|
||||||
|
t.string :uuid, unique: true
|
||||||
|
t.string :status
|
||||||
|
t.integer :visited_count
|
||||||
|
t.datetime :published_at
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :libraries, :published_at
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,14 @@
|
|||||||
|
class CreateLibraryApplies < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :library_applies do |t|
|
||||||
|
t.references :library
|
||||||
|
t.string :status
|
||||||
|
t.string :reason
|
||||||
|
|
||||||
|
t.datetime :refused_at
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :library_applies, :refused_at
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue