From d176508010884d034cff7c9f055894c46a9bc03d Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 12 Oct 2021 16:14:45 +0800 Subject: [PATCH] add: system notification management --- .../admins/system_notifications/index.js | 76 +++++++++++++++++++ .../admins/system_notifications_controller.rb | 76 +++++++++++++++++++ app/models/system_notification.rb | 17 +++++ app/models/system_notification_history.rb | 21 +++++ app/models/user.rb | 4 +- app/views/admins/shared/_sidebar.html.erb | 4 +- .../system_notifications/_form.html.erb | 57 ++++++++++++++ .../system_notifications/_form_modal.html.erb | 40 ++++++++++ .../system_notifications/_list.html.erb | 35 +++++++++ .../admins/system_notifications/edit.js.erb | 2 + .../system_notifications/index.html.erb | 21 +++++ .../admins/system_notifications/index.js.erb | 1 + .../admins/system_notifications/new.js.erb | 2 + config/routes.rb | 5 ++ ...11012060724_create_system_notifications.rb | 13 ++++ ...37_create_system_notification_histories.rb | 10 +++ 16 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/admins/system_notifications/index.js create mode 100644 app/controllers/admins/system_notifications_controller.rb create mode 100644 app/models/system_notification.rb create mode 100644 app/models/system_notification_history.rb create mode 100644 app/views/admins/system_notifications/_form.html.erb create mode 100644 app/views/admins/system_notifications/_form_modal.html.erb create mode 100644 app/views/admins/system_notifications/_list.html.erb create mode 100644 app/views/admins/system_notifications/edit.js.erb create mode 100644 app/views/admins/system_notifications/index.html.erb create mode 100644 app/views/admins/system_notifications/index.js.erb create mode 100644 app/views/admins/system_notifications/new.js.erb create mode 100644 db/migrate/20211012060724_create_system_notifications.rb create mode 100644 db/migrate/20211012060837_create_system_notification_histories.rb diff --git a/app/assets/javascripts/admins/system_notifications/index.js b/app/assets/javascripts/admins/system_notifications/index.js new file mode 100644 index 000000000..90809b344 --- /dev/null +++ b/app/assets/javascripts/admins/system_notifications/index.js @@ -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/system_notifications/index.js + */ +$(document).on('turbolinks:load', function(){ + + var showSuccessNotify = function() { + $.notify({ + message: '操作成功' + },{ + type: 'success' + }); + } + + // close user + $('.system-notification-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/system_notifications/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + system_notification: { + is_top: false + } + }, + success: function() { + showSuccessNotify(); + $closeAction.hide(); + $uncloseAction.show(); + $(".system-notification-item-"+keywordID).children('td').eq(3).text("") + } + }); + } + }); + }); + + // unclose user + $('.system-notification-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/system_notifications/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + system_notification: { + is_top: true + } + }, + success: function() { + showSuccessNotify(); + $closeAction.show(); + $uncloseAction.hide(); + $(".system-notification-item-"+keywordID).children('td').eq(3).text("√") + } + }); + } + }) + }); +}) \ No newline at end of file diff --git a/app/controllers/admins/system_notifications_controller.rb b/app/controllers/admins/system_notifications_controller.rb new file mode 100644 index 000000000..1a71173c6 --- /dev/null +++ b/app/controllers/admins/system_notifications_controller.rb @@ -0,0 +1,76 @@ +class Admins::SystemNotificationsController < Admins::BaseController + before_action :get_notification, only: [:history, :edit,:update, :destroy] + # before_action :validate_identifer, only: [:create, :update] + + def index + sort_by = SystemNotification.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 = SystemNotification.ransack(subject_cont: params[:search]) + notifications = q.result(distinct: true).order("#{sort_by} #{sort_direction},created_at desc") + @notifications = paginate(notifications) + end + + def history + @users = @notification.users + end + + def new + @notification = SystemNotification.new + end + + def edit + end + + def create + @notification = SystemNotification.new(notification_params) + if @notification.save + redirect_to admins_system_notifications_path + flash[:success] = '系统保留关键词创建成功' + else + redirect_to admins_system_notifications_path + flash[:danger] = @notification.errors.full_messages.join(",") + end + end + + def update + + respond_to do |format| + if @notification.update_attributes(notification_params) + format.html do + redirect_to admins_system_notifications_path + flash[:success] = '系统保留关键词更新成功' + end + format.js {render_ok} + else + format.html do + redirect_to admins_system_notifications_path + flash[:danger] = @notification.errors.full_messages.join(",") + end + format.js {render_js_error} + end + end + end + + def destroy + if @notification.destroy + redirect_to admins_system_notifications_path + flash[:success] = "系统保留关键词删除成功" + else + redirect_to admins_system_notifications_path + flash[:danger] = "系统保留关键词删除失败" + end + end + + private + def notification_params + params.require(:system_notification).permit! + end + + def get_notification + @notification = SystemNotification.find_by(id: params[:id]) + unless @notification.present? + redirect_to admins_system_notifications_path + flash[:danger] = "系统保留关键词不存在" + end + end +end \ No newline at end of file diff --git a/app/models/system_notification.rb b/app/models/system_notification.rb new file mode 100644 index 000000000..76c514cd3 --- /dev/null +++ b/app/models/system_notification.rb @@ -0,0 +1,17 @@ +# == Schema Information +# +# Table name: system_notifications +# +# id :integer not null, primary key +# subject :string(255) +# sub_subject :string(255) +# content :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# + +class SystemNotification < ApplicationRecord + + has_many :system_notification_histories + has_many :users, through: :system_notification_histories +end diff --git a/app/models/system_notification_history.rb b/app/models/system_notification_history.rb new file mode 100644 index 000000000..8723ca917 --- /dev/null +++ b/app/models/system_notification_history.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: system_notification_histories +# +# id :integer not null, primary key +# system_message_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_system_notification_histories_on_system_message_id (system_message_id) +# index_system_notification_histories_on_user_id (user_id) +# + +class SystemNotificationHistory < ApplicationRecord + + belongs_to :system_notification + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 3c4a900a0..22c5e3cd7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -170,7 +170,9 @@ class User < Owner has_many :issues, dependent: :destroy, foreign_key: :author_id has_many :pull_requests, dependent: :destroy has_many :public_keys, class_name: "Gitea::PublicKey",primary_key: :gitea_uid, foreign_key: :owner_id, dependent: :destroy - + has_many :system_notification_histories + has_many :system_notifications, through: :system_notification_histories + # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } scope :like, lambda { |keywords| diff --git a/app/views/admins/shared/_sidebar.html.erb b/app/views/admins/shared/_sidebar.html.erb index 402449b3b..75abbc124 100644 --- a/app/views/admins/shared/_sidebar.html.erb +++ b/app/views/admins/shared/_sidebar.html.erb @@ -30,8 +30,6 @@
  • <%= sidebar_item(admins_reversed_keywords_path, '系统保留关键词', icon: 'key', controller: 'admins-reversed_keywords') %>
  • -
  • <%= sidebar_item(admins_message_templates_path, '消息模版', icon: 'folder', controller: 'admins-message_templates') %>
  • -
  • <%= sidebar_item(admins_laboratories_path, '云上实验室', icon: 'cloud', controller: 'admins-laboratories') %>
  • @@ -48,6 +46,8 @@ <%= sidebar_item_group('#setting-system', '系统配置', icon: 'wrench') do %>
  • <%= sidebar_item(admins_sites_path, 'setting接口配置', icon: 'deaf', controller: 'admins-sites') %>
  • <%= sidebar_item(admins_edu_settings_path, '全局变量配置', icon: 'pencil-square', controller: 'admins-edu_settings') %>
  • +
  • <%= sidebar_item(admins_system_notifications_path, '系统通知配置', icon: 'bell', controller: 'admins-system_notifications') %>
  • +
  • <%= sidebar_item(admins_message_templates_path, '消息模版配置', icon: 'folder', controller: 'admins-message_templates') %>
  • <% end %>
  • diff --git a/app/views/admins/system_notifications/_form.html.erb b/app/views/admins/system_notifications/_form.html.erb new file mode 100644 index 000000000..8e7619101 --- /dev/null +++ b/app/views/admins/system_notifications/_form.html.erb @@ -0,0 +1,57 @@ +
    +
    <%= type == "create" ? "新建" : "编辑" %>忽略文件
    + <%= link_to "返回", admins_project_ignores_path, class: "btn btn-default pull-right" %> +
    + +
    + <%= form_for @notification, url: {controller: "system_notifications", action: "#{type}"} do |p| %> +
    + +
    + <%= p.text_field :subject, class: "form-control input-lg", placeholder: "请输入系统通知标题" %> +
    + +
    +
    + +
    + <%= p.text_field :sub_subject, class: "form-control input-lg", placeholder: "请输入系统通知副标题" %> +
    + +
    +
    + +
    + <%= p.text_area :content, class:"form-control", style: 'display: none;', rows: "10", cols: "20", placeholer: "请输入系统通知正文" %> +
    + +
    + +
    +
    + <%= p.check_box :is_top, class: "form-check-input", value:"true"%> + +
    +
    +
    + <%= p.submit "确认", class: "btn btn-primary submit-btn" %> +
    + <% end %> +
    \ No newline at end of file diff --git a/app/views/admins/system_notifications/_form_modal.html.erb b/app/views/admins/system_notifications/_form_modal.html.erb new file mode 100644 index 000000000..658bbc9c0 --- /dev/null +++ b/app/views/admins/system_notifications/_form_modal.html.erb @@ -0,0 +1,40 @@ + \ No newline at end of file diff --git a/app/views/admins/system_notifications/_list.html.erb b/app/views/admins/system_notifications/_list.html.erb new file mode 100644 index 000000000..0d86ab035 --- /dev/null +++ b/app/views/admins/system_notifications/_list.html.erb @@ -0,0 +1,35 @@ + + + + + + + + + + + + + <% if notifications.present? %> + <% notifications.each_with_index do |notification, index| %> + + + + + + + + + <% end %> + <% else %> + <%= render 'admins/shared/no_data_for_table' %> + <% end %> + +
    序号标题副标题<%= sort_tag('是否置顶', name: 'is_top', path: admins_system_notifications_path) %><%= sort_tag('创建时间', name: 'created_at', path: admins_system_notifications_path) %>操作
    <%= list_index_no((params[:page] || 1).to_i, index) %><%= notification.subject %><%= notification.sub_subject %><%= notification.is_top ? '√' : '' %><%= notification.created_at&.strftime('%Y-%m-%d %H:%M') %> + <%= javascript_void_link '置顶', class: 'action unclose-action', data: { id: notification.id }, style: notification.is_top ? 'display: none;' : '' %> + <%= javascript_void_link '取消置顶', class: 'action close-action', data: { id: notification.id }, style: notification.is_top ? '' : 'display: none;' %> + <%= link_to "编辑", edit_admins_system_notification_path(notification), remote: true, class: "action" %> + <%= link_to "删除", admins_system_notification_path(notification), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %> +
    + +<%= render partial: 'admins/shared/paginate', locals: { objects: notifications } %> \ No newline at end of file diff --git a/app/views/admins/system_notifications/edit.js.erb b/app/views/admins/system_notifications/edit.js.erb new file mode 100644 index 000000000..63fad5838 --- /dev/null +++ b/app/views/admins/system_notifications/edit.js.erb @@ -0,0 +1,2 @@ +$("#admins-system-notification-content").html("<%= j render(partial: 'admins/system_notifications/form', locals: {type: 'create'}) %>") +createMDEditor('system-notification-content-editor', { height: 500, placeholder: '请输入邮件模版' }); \ No newline at end of file diff --git a/app/views/admins/system_notifications/index.html.erb b/app/views/admins/system_notifications/index.html.erb new file mode 100644 index 000000000..01a35f216 --- /dev/null +++ b/app/views/admins/system_notifications/index.html.erb @@ -0,0 +1,21 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb('系统通知模版') %> +<% end %> + +
    +
    + <%= form_tag(admins_system_notifications_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': '搜索中...') %> + + <% end %> + <%= link_to "新增", new_admins_system_notification_path, remote: true, class: "btn btn-primary pull-right", "data-disabled-with":"...新增" %> +
    + +
    +
    + +
    + <%= render partial: 'admins/system_notifications/list', locals: { notifications: @notifications } %> +
    +
    \ No newline at end of file diff --git a/app/views/admins/system_notifications/index.js.erb b/app/views/admins/system_notifications/index.js.erb new file mode 100644 index 000000000..a5cfec841 --- /dev/null +++ b/app/views/admins/system_notifications/index.js.erb @@ -0,0 +1 @@ +$('.system-notification-list-container').html("<%= j( render partial: 'admins/system_notifications/list', locals: { notifications: @notifications } ) %>"); \ No newline at end of file diff --git a/app/views/admins/system_notifications/new.js.erb b/app/views/admins/system_notifications/new.js.erb new file mode 100644 index 000000000..885357b5a --- /dev/null +++ b/app/views/admins/system_notifications/new.js.erb @@ -0,0 +1,2 @@ +$("#admins-system-notification-content").html("<%= j render(partial: 'admins/system_notifications/form', locals: {type: 'create'}) %>") +createMDEditor('system-notification-content-editor', { height: 500, placeholder: '请输入邮件模版' }); diff --git a/config/routes.rb b/config/routes.rb index 005bd926a..b940ed700 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -670,6 +670,11 @@ Rails.application.routes.draw do resources :project_licenses resources :project_ignores resources :reversed_keywords + resources :system_notifications do + member do + get :history + end + end resources :message_templates, only: [:index, :edit, :update] do collection do get :init_data diff --git a/db/migrate/20211012060724_create_system_notifications.rb b/db/migrate/20211012060724_create_system_notifications.rb new file mode 100644 index 000000000..2dcade417 --- /dev/null +++ b/db/migrate/20211012060724_create_system_notifications.rb @@ -0,0 +1,13 @@ +class CreateSystemNotifications < ActiveRecord::Migration[5.2] + def change + # 系统消息 + create_table :system_notifications do |t| + t.string :subject, comment: "标题" + t.string :sub_subject, comment: "副标题" + t.string :content, comment: "正文" + t.boolean :is_top, comment: "是否置顶" + + t.timestamps + end + end +end diff --git a/db/migrate/20211012060837_create_system_notification_histories.rb b/db/migrate/20211012060837_create_system_notification_histories.rb new file mode 100644 index 000000000..9110f3ce9 --- /dev/null +++ b/db/migrate/20211012060837_create_system_notification_histories.rb @@ -0,0 +1,10 @@ +class CreateSystemNotificationHistories < ActiveRecord::Migration[5.2] + def change + create_table :system_notification_histories do |t| + t.references :system_notification + t.references :user + + t.timestamps + end + end +end