diff --git a/app/assets/javascripts/admins/projects/index.js b/app/assets/javascripts/admins/projects/index.js new file mode 100644 index 000000000..55da8e455 --- /dev/null +++ b/app/assets/javascripts/admins/projects/index.js @@ -0,0 +1,137 @@ +/* + * @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 + $('.project-list-container').on('click', '.recommend-action', function(){ + var $closeAction = $(this); + var $uncloseAction = $closeAction.siblings('.unrecommend-action'); + + var keywordID = $closeAction.data('id'); + customConfirm({ + content: '确认将该项目设置为推荐项目吗?', + ok: function(){ + $.ajax({ + url: '/admins/projects/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + project: { + recommend: true, + recommend_index: 1 + } + }, + success: function() { + showSuccessNotify(); + $closeAction.hide(); + $uncloseAction.show(); + $(".project-item-"+keywordID).children('td').eq(5).text("√") + } + }); + } + }); + }); + + // unclose user + $('.project-list-container').on('click', '.unrecommend-action', function(){ + var $uncloseAction = $(this); + var $closeAction = $uncloseAction.siblings('.recommend-action'); + + var keywordID = $uncloseAction.data('id'); + customConfirm({ + content: '确认取消该推荐项目吗?', + ok: function () { + $.ajax({ + url: '/admins/projects/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + project: { + recommend: false, + recommend: 1 + } + }, + success: function() { + showSuccessNotify(); + $closeAction.show(); + $uncloseAction.hide(); + $(".project-item-"+keywordID).children('td').eq(5).text("") + } + }); + } + }) + }); + + + // close user + $('.project-list-container').on('click', '.pinned-action', function(){ + var $closeAction = $(this); + var $uncloseAction = $closeAction.siblings('.unpinned-action'); + + var keywordID = $closeAction.data('id'); + customConfirm({ + content: '确认将该项目设置为精选项目吗?', + ok: function(){ + $.ajax({ + url: '/admins/projects/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + project: { + is_pinned: true, + } + }, + success: function() { + showSuccessNotify(); + $closeAction.hide(); + $uncloseAction.show(); + $(".project-item-"+keywordID).children('td').eq(4).text("√") + } + }); + } + }); + }); + + // unclose user + $('.project-list-container').on('click', '.unpinned-action', function(){ + var $uncloseAction = $(this); + var $closeAction = $uncloseAction.siblings('.pinned-action'); + + var keywordID = $uncloseAction.data('id'); + customConfirm({ + content: '确认取消该精选项目吗?', + ok: function () { + $.ajax({ + url: '/admins/projects/' + keywordID, + method: 'PUT', + dataType: 'json', + data: { + project: { + is_pinned: false, + } + }, + success: function() { + showSuccessNotify(); + $closeAction.show(); + $uncloseAction.hide(); + $(".project-item-"+keywordID).children('td').eq(4).text("") + } + }); + } + }) + }); +}) \ No newline at end of file diff --git a/app/controllers/admins/project_categories_controller.rb b/app/controllers/admins/project_categories_controller.rb index ba83e841d..282faf771 100644 --- a/app/controllers/admins/project_categories_controller.rb +++ b/app/controllers/admins/project_categories_controller.rb @@ -22,7 +22,7 @@ class Admins::ProjectCategoriesController < Admins::BaseController max_position_items = ProjectCategory.select(:id, :position).pluck(:position).reject!(&:blank?) max_position = max_position_items.present? ? max_position_items.max.to_i : 0 - @project_category = ProjectCategory.new(name: @name,position: max_position) + @project_category = ProjectCategory.new(name: @name,position: max_position, pinned_index: params[:project_category][:pinned_index].to_i) if @project_category.save redirect_to admins_project_categories_path flash[:success] = '创建成功' @@ -33,7 +33,7 @@ class Admins::ProjectCategoriesController < Admins::BaseController end def update - if @project_category.update_attribute(:name, @name) + if @project_category.update_attributes({name: @name, pinned_index: params[:project_category][:pinned_index].to_i}) redirect_to admins_project_categories_path flash[:success] = '更新成功' else @@ -43,7 +43,7 @@ class Admins::ProjectCategoriesController < Admins::BaseController end def destroy - if @project_language.destroy + if @project_category.destroy redirect_to admins_project_categories_path flash[:success] = "删除成功" else diff --git a/app/controllers/admins/projects_controller.rb b/app/controllers/admins/projects_controller.rb index 9e06eb1c9..4175f7250 100644 --- a/app/controllers/admins/projects_controller.rb +++ b/app/controllers/admins/projects_controller.rb @@ -1,4 +1,5 @@ class Admins::ProjectsController < Admins::BaseController + before_action :find_project, only: [:edit, :update] def index sort_by = Project.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_on' @@ -8,6 +9,26 @@ class Admins::ProjectsController < Admins::BaseController @projects = paginate projects.includes(:owner, :members, :issues, :versions, :attachments, :project_score) end + def edit ;end + + def update + respond_to do |format| + if @project.update_attributes(project_update_params) + format.html do + redirect_to admins_projects_path + flash[:sucess] = "更新成功" + end + format.js {render_ok} + else + format.html do + redirect_to admins_projects_path + flash[:danger] = "更新失败" + end + format.js {render_js_error} + end + end + end + def destroy project = Project.find_by!(id: params[:id]) ActiveRecord::Base.transaction do @@ -21,4 +42,13 @@ class Admins::ProjectsController < Admins::BaseController redirect_to admins_projects_path flash[:danger] = "删除失败" end + + private + def find_project + @project = Project.find_by_id(params[:id]) + end + + def project_update_params + params.require(:project).permit(:is_pinned, :recommend, :recommend_index) + end end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index d36c13efc..aa2e152cf 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -130,7 +130,7 @@ class Project < ApplicationRecord has_many :webhooks, class_name: "Gitea::Webhook", primary_key: :gpid, foreign_key: :repo_id after_create :init_project_common, :incre_user_statistic, :incre_platform_statistic after_save :check_project_members, :reset_cache_data - before_save :set_invite_code, :reset_unmember_followed + before_save :set_invite_code, :reset_unmember_followed, :set_recommend_and_is_pinned before_destroy :decre_project_common after_destroy :decre_user_statistic, :decre_platform_statistic scope :project_statics_select, -> {select(:id,:name, :is_public, :identifier, :status, :project_type, :user_id, :forked_count, :visits, :project_category_id, :project_language_id, :license_id, :ignore_id, :watchers_count, :created_on)} @@ -214,6 +214,16 @@ class Project < ApplicationRecord end end + def set_recommend_and_is_pinned + self.recommend = self.recommend_index.zero? ? false : true + # 私有项目不允许设置精选和推荐 + unless self.is_public + self.recommend = false + self.recommend_index = 0 + self.is_pinned = false + end + end + def self.search_project(search) ransack(name_or_identifier_cont: search) end diff --git a/app/views/admins/project_categories/_form_modal.html.erb b/app/views/admins/project_categories/_form_modal.html.erb index fd20936b6..84e43416a 100644 --- a/app/views/admins/project_categories/_form_modal.html.erb +++ b/app/views/admins/project_categories/_form_modal.html.erb @@ -9,7 +9,18 @@ <%= form_for @project_category, url: {controller: "project_categories", action: "#{type}"} do |p| %> + +
+
diff --git a/app/views/admins/projects/shared/_form_modal.html.erb b/app/views/admins/projects/shared/_form_modal.html.erb new file mode 100644 index 000000000..359d3bdd3 --- /dev/null +++ b/app/views/admins/projects/shared/_form_modal.html.erb @@ -0,0 +1,26 @@ + \ No newline at end of file diff --git a/app/views/admins/projects/shared/_list.html.erb b/app/views/admins/projects/shared/_list.html.erb index fe8d96e9d..90ce5c4ad 100644 --- a/app/views/admins/projects/shared/_list.html.erb +++ b/app/views/admins/projects/shared/_list.html.erb @@ -5,6 +5,8 @@ ID 项目名称 公开 + 精选 + 推荐 issue 资源 版本库 @@ -12,8 +14,8 @@ 里程碑 成员 管理员 - <%= sort_tag('创建时间', name: 'created_on', path: admins_projects_path) %> - 操作 + <%= sort_tag('创建时间', name: 'created_on', path: admins_projects_path) %> + 操作 @@ -26,6 +28,8 @@ <%= link_to(project.name, "/projects/#{project&.owner&.login}/#{project.identifier}", target: '_blank') %> <%= project.is_public ? '√' : '' %> + <%= project.is_pinned ? '√' : '' %> + <%= project.recommend ? '√' : '' %> <%= project.issues.size %> <%= project.attachments.size %> <%= project&.project_score.try(:changeset_num).to_i %> @@ -37,8 +41,15 @@ <%= project.created_on&.strftime('%Y-%m-%d %H:%M') %> + <% if project.is_public %> + <%= javascript_void_link '精选', class: 'action pinned-action', data: { id: project.id }, style: project.is_pinned ? 'display: none;' : '' %> + <%= javascript_void_link '取消精选', class: 'action unpinned-action', data: { id: project.id }, style: project.is_pinned ? '' : 'display: none;' %> + <%= javascript_void_link '推荐', class: 'action recommend-action', data: { id: project.id }, style: project.recommend ? 'display: none;' : '' %> + <%= javascript_void_link '取消推荐', class: 'action unrecommend-action', data: { id: project.id }, style: project.recommend ? '' : 'display: none;' %> + <%= link_to "设置推荐等级", edit_admins_project_path(project.id), remote: true, class: "action", style: project.recommend ? '' : 'display: none;' %> + <% end %> <%= link_to "删除", admins_project_path(project.id), method: :delete, data:{confirm: "确认删除的吗?"}, class: "delete-project-action" %> - + <% end %> <% else %> diff --git a/config/routes.rb b/config/routes.rb index c4afbe4c3..5d40ca42c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -909,7 +909,7 @@ Rails.application.routes.draw do resources :courses, only: [:index, :destroy, :update] - resources :projects, only: [:index, :destroy] + resources :projects, only: [:index, :edit, :update, :destroy] resources :disciplines, only: [:index, :create, :edit, :update, :destroy] do post :adjust_position, on: :member