add: reversed keyword management

This commit is contained in:
yystopf 2021-08-31 14:22:49 +08:00
parent fe7dfcea52
commit 4cb71179fd
11 changed files with 265 additions and 1 deletions

View File

@ -0,0 +1,76 @@
/*
* @Description: Do not edit
* @Date: 2021-08-31 11:16:45
* @LastEditors: viletyy
* @Author: viletyy
* @LastEditTime: 2021-08-31 14:19:46
* @FilePath: /forgeplus/app/assets/javascripts/admins/reversed_keywords/index.js
*/
$(document).on('turbolinks:load', function(){
var showSuccessNotify = function() {
$.notify({
message: '操作成功'
},{
type: 'success'
});
}
// close user
$('.reversed-keyword-list-container').on('click', '.close-action', function(){
var $closeAction = $(this);
var $uncloseAction = $closeAction.siblings('.unclose-action');
var keywordID = $closeAction.data('id');
customConfirm({
content: '确认关闭限制吗?',
ok: function(){
$.ajax({
url: '/admins/reversed_keywords/' + keywordID,
method: 'PUT',
dataType: 'json',
data: {
reversed_keyword: {
closed: true
}
},
success: function() {
showSuccessNotify();
$closeAction.hide();
$uncloseAction.show();
$(".reversed-keyword-item-"+keywordID).children('td').eq(3).text("")
}
});
}
});
});
// unclose user
$('.reversed-keyword-list-container').on('click', '.unclose-action', function(){
var $uncloseAction = $(this);
var $closeAction = $uncloseAction.siblings('.close-action');
var keywordID = $uncloseAction.data('id');
customConfirm({
content: '确认开启限制吗?',
ok: function () {
$.ajax({
url: '/admins/reversed_keywords/' + keywordID,
method: 'PUT',
dataType: 'json',
data: {
reversed_keyword: {
closed: false
}
},
success: function() {
showSuccessNotify();
$closeAction.show();
$uncloseAction.hide();
$(".reversed-keyword-item-"+keywordID).children('td').eq(3).text("√")
}
});
}
})
});
})

View File

@ -0,0 +1,84 @@
class Admins::ReversedKeywordsController < Admins::BaseController
before_action :get_keyword, only: [:edit,:update, :destroy]
# before_action :validate_identifer, only: [:create, :update]
def index
sort_by = ReversedKeyword.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_at'
sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
q = ReversedKeyword.ransack(identifier_cont: params[:search])
keywords = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
@keywords = paginate(keywords)
end
def new
@keyword = ReversedKeyword.new
end
def edit
end
def create
@keyword = ReversedKeyword.new(keyword_params)
if @keyword.save
redirect_to admins_reversed_keywords_path
flash[:success] = '系统保留关键词创建成功'
else
redirect_to admins_reversed_keywords_path
flash[:danger] = @keyword.errors.full_messages.join(",")
end
end
def update
respond_to do |format|
if @keyword.update_attributes(keyword_params)
format.html do
redirect_to admins_reversed_keywords_path
flash[:success] = '系统保留关键词更新成功'
end
format.js {render_ok}
else
format.html do
redirect_to admins_reversed_keywords_path
flash[:danger] = @keyword.errors.full_messages.join(",")
end
format.js {render_js_error}
end
end
end
def destroy
if @keyword.destroy
redirect_to admins_reversed_keywords_path
flash[:success] = "系统保留关键词删除成功"
else
redirect_to admins_reversed_keywords_path
flash[:danger] = "系统保留关键词删除失败"
end
end
private
def keyword_params
params.require(:reversed_keyword).permit!
end
def get_keyword
@keyword = ReversedKeyword.find_by(id: params[:id])
unless @keyword.present?
redirect_to admins_reversed_keywords_path
flash[:danger] = "系统保留关键词不存在"
end
end
def validate_identifer
identifer = keyword_params[:identifier].to_s.downcase
if identifer.blank?
redirect_to admins_reversed_keywords_path
flash[:danger] = '系统保留关键词标识不能为空'
elsif ProjectLanguage.exists?(name: identifer)
redirect_to admins_reversed_keywords_path
flash[:danger] = '系统保留关键词已存在'
end
end
end

View File

@ -12,5 +12,15 @@
class ReversedKeyword < ApplicationRecord
scope :is_reversed, -> (identifier){where(identifier: identifier, closed: false) if identifier.present?}
scope :is_reversed, -> (identifier){where(identifier: identifier.downcase, closed: false) if identifier.present?}
validates :identifier, presence: true, uniqueness: true
before_validation :set_identifier
private
def set_identifier
self.identifier = self.identifier.downcase
end
end

View File

@ -0,0 +1,32 @@
<div class="modal fade reversed-keyword-change-modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><%= type == "create" ? "新增" : "编辑" %></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<%= form_for @keyword, url: {controller: "reversed_keywords", action: "#{type}"} do |p| %>
<div class="modal-body">
<div class="form-group">
<label>
系统保留关键词标识 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_field :identifier,class: "form-control input-lg",required: true%>
</div>
<div class="form-group">
<label>
系统保留关键词描述
</label>
<%= p.text_area :description,class: "form-control",placeholder: ""%>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<%= p.submit "确认", class: "btn btn-primary submit-btn" %>
</div>
<% end %>
</div>
</div>
</div>

View File

@ -0,0 +1,37 @@
<table class="table table-hover text-center subject-list-table">
<thead class="thead-light">
<tr>
<th width="5%">序号</th>
<th width="30%"><%= sort_tag('标识', name: 'identifier', path: admins_reversed_keywords_path) %></th>
<th width="20%">描述</th>
<th width="20%"><%= sort_tag('限制是否开启', name: 'closed', path: admins_reversed_keywords_path) %></th>
<th width="20%"><%= sort_tag('创建时间', name: 'created_at', path: admins_reversed_keywords_path) %></th>
<th width="25%">操作</th>
</tr>
</thead>
<tbody>
<% if keywords.present? %>
<% keywords.each_with_index do |keyword, index| %>
<tr class="reversed-keyword-item-<%= keyword.id %>">
<td><%= list_index_no((params[:page] || 1).to_i, index) %></td>
<td>
<%= link_to(keyword.identifier, "javascript:void(0)") %>
</td>
<td><%= keyword.description %></td>
<td class="keyword_closed"><%= !keyword.closed ? '√' : '' %></td>
<td><%= keyword.created_at&.strftime('%Y-%m-%d %H:%M') %></td>
<td class="action-container">
<%= javascript_void_link '开启限制', class: 'action unclose-action', data: { id: keyword.id }, style: keyword.closed? ? '' : 'display: none;' %>
<%= javascript_void_link '关闭限制', class: 'action close-action', data: { id: keyword.id }, style: keyword.closed? ? 'display: none;' : '' %>
<%= link_to "编辑", edit_admins_reversed_keyword_path(keyword), remote: true, class: "action" %>
<%= link_to "删除", admins_reversed_keyword_path(keyword), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %>
</td>
</tr>
<% end %>
<% else %>
<%= render 'admins/shared/no_data_for_table' %>
<% end %>
</tbody>
</table>
<%= render partial: 'admins/shared/paginate', locals: { objects: keywords } %>

View File

@ -0,0 +1,2 @@
$("#reversed-keyword-modals").html("<%= j render(partial: 'admins/reversed_keywords/form_modal', locals: {type: 'update'}) %>")
$(".reversed-keyword-change-modal").modal('show');

View File

@ -0,0 +1,18 @@
<% define_admin_breadcrumbs do %>
<% add_admin_breadcrumb('系统保留关键词') %>
<% end %>
<div class="box search-form-container project-list-form">
<%= form_tag(admins_reversed_keywords_path, method: :get, class: 'form-inline search-form flex-1', remote: true) do %>
<%= text_field_tag(:search, params[:search], class: 'form-control col-12 col-md-2 mr-3', placeholder: '标识检索') %>
<%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %>
<input type="reset" class="btn btn-secondary clear-btn" value="清空"/>
<% end %>
<%= link_to "新增", new_admins_reversed_keyword_path, remote: true, class: "btn btn-primary pull-right", "data-disabled-with":"...新增" %>
</div>
<div class="box admin-list-container reversed-keyword-list-container">
<%= render partial: 'admins/reversed_keywords/list', locals: { keywords: @keywords } %>
</div>
<div id="reversed-keyword-modals">
</div>

View File

@ -0,0 +1 @@
$('.reversed-keyword-list-container').html("<%= j( render partial: 'admins/reversed_keywords/list', locals: { keywords: @keywords } ) %>");

View File

@ -0,0 +1,2 @@
$("#reversed-keyword-modals").html("<%= j render(partial: 'admins/reversed_keywords/form_modal', locals: {type: 'create'}) %>")
$(".reversed-keyword-change-modal").modal('show');

View File

@ -26,6 +26,7 @@
<li><%= sidebar_item(admins_project_categories_path, '分类列表', icon: 'sitemap', controller: 'admins-project_categories') %></li>
<li><%= sidebar_item(admins_project_licenses_path, '开源许可证', icon: 'file-text-o', controller: 'admins-project_licenses') %></li>
<li><%= sidebar_item(admins_project_ignores_path, '忽略文件', icon: 'git', controller: 'admins-project_ignores') %></li>
<li><%= sidebar_item(admins_reversed_keywords_path, '系统保留关键词', icon: 'key', controller: 'admins-reversed_keywords') %></li>
<% end %>
</li>

View File

@ -655,6 +655,7 @@ Rails.application.routes.draw do
resources :project_categories
resources :project_licenses
resources :project_ignores
resources :reversed_keywords
resources :major_informations, only: [:index]
resources :ec_templates, only: [:index, :destroy] do
collection do