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 @@
序号 | +标题 | +副标题 | +<%= 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" %> + | +