From 36bae719472d4c3a4e065a71351b635310417b51 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 25 Dec 2020 16:17:46 +0800 Subject: [PATCH] =?UTF-8?q?[ADD]=E5=A2=9E=E5=8A=A0=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E8=AE=B8=E5=8F=AF=E7=9A=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apply_signatures_controller.rb | 16 +++++ .../project_categories_controller.rb | 2 +- app/controllers/projects_controller.rb | 2 +- app/models/apply_signature.rb | 6 ++ app/models/license.rb | 2 + app/models/project.rb | 4 ++ app/models/user.rb | 1 + app/queries/projects/list_query.rb | 2 +- app/services/projects/create_service.rb | 7 +- .../apply_signatures/create.json.jbuilder | 4 ++ app/views/licenses/index.json.jbuilder | 2 +- .../projects/_project_detail.json.jbuilder | 72 ++++++++++--------- app/views/projects/show.json.jbuilder | 6 ++ app/views/repositories/edit.json.jbuilder | 1 + app/views/repositories/show.json.jbuilder | 6 ++ config/routes.rb | 1 + .../20201225064324_create_apply_signatures.rb | 11 +++ ...add_some_columns_about_apply_signatures.rb | 6 ++ spec/models/apply_signature_spec.rb | 5 ++ 19 files changed, 118 insertions(+), 38 deletions(-) create mode 100644 app/controllers/apply_signatures_controller.rb create mode 100644 app/models/apply_signature.rb create mode 100644 app/views/apply_signatures/create.json.jbuilder create mode 100644 db/migrate/20201225064324_create_apply_signatures.rb create mode 100644 db/migrate/20201225065222_add_some_columns_about_apply_signatures.rb create mode 100644 spec/models/apply_signature_spec.rb diff --git a/app/controllers/apply_signatures_controller.rb b/app/controllers/apply_signatures_controller.rb new file mode 100644 index 00000000..652e5989 --- /dev/null +++ b/app/controllers/apply_signatures_controller.rb @@ -0,0 +1,16 @@ +class ApplySignaturesController < ApplicationController + def create + ActiveRecord::Base.transaction do + begin + @signature = current_user.apply_signatures.create!(project_id: params[:project_id]) + @attachment = Attachment.find_by_id(params[:attachment_id]) + @attachment.container = @signature + @attachment.save! + rescue Exception => e + tip_exception("#{e}") + raise ActiveRecord::Rollback + end + render_json + end + end +end \ No newline at end of file diff --git a/app/controllers/project_categories_controller.rb b/app/controllers/project_categories_controller.rb index 4b4a9634..edc550ec 100644 --- a/app/controllers/project_categories_controller.rb +++ b/app/controllers/project_categories_controller.rb @@ -12,7 +12,7 @@ class ProjectCategoriesController < ApplicationController # projects = Project.visible # end @project_children_categories = ProjectCategory.descendants - projects = Project.no_anomory_projects.visible + projects = Project.no_anomory_projects.secret_and_visible categories = projects.joins(:project_category).where(project_categories: {ancestry: nil}).group("project_categories.name", "project_categories.id").size.keys.to_h extra_category_id = categories.delete("其他") categories = categories.merge({"其他": extra_category_id}) if extra_category_id.present? #其他的放在最后面 diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index bfecf81a..76400860 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -43,7 +43,7 @@ class ProjectsController < ApplicationController # else # projects = Project.visible # end - projects = Project.no_anomory_projects.visible + projects = Project.no_anomory_projects.secret_and_visible language_lists = projects.joins(:project_language).group("project_languages.name", "project_languages.id").size.keys.sort.to_h @project_group_list = language_lists.delete_if { |k, v| k.blank? } end diff --git a/app/models/apply_signature.rb b/app/models/apply_signature.rb new file mode 100644 index 00000000..f76bda94 --- /dev/null +++ b/app/models/apply_signature.rb @@ -0,0 +1,6 @@ +class ApplySignature < ApplicationRecord + belongs_to :user + belongs_to :project + + scope :with_user_id, -> (user_id) {where(user_id: user_id)} +end diff --git a/app/models/license.rb b/app/models/license.rb index 952c4a13..1c665075 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -1,3 +1,5 @@ class License < ApplicationRecord include Projectable + has_many :attachments, as: :container, dependent: :destroy + end diff --git a/app/models/project.rb b/app/models/project.rb index bd2008af..9c73dfde 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -31,10 +31,14 @@ class Project < ApplicationRecord has_many :versions, -> { order("versions.created_on DESC, versions.name DESC") }, dependent: :destroy has_many :praise_treads, as: :praise_tread_object, dependent: :destroy has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" + has_many :apply_signatures, dependent: :destroy after_save :check_project_members 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)} scope :no_anomory_projects, -> {where("projects.user_id is not null and projects.user_id != ?", 2)} + scope :secret_and_visible, -> {joins(:license).where("licenses.is_secret = TRUE OR projects.is_public = TRUE")} + + delegate :is_secret, to: :license def self.search_project(search) diff --git a/app/models/user.rb b/app/models/user.rb index 41be85f9..b112a660 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -92,6 +92,7 @@ class User < ApplicationRecord # 教学案例 # has_many :libraries, dependent: :destroy has_many :project_trends, dependent: :destroy + has_many :apply_signatures, dependent: :destroy # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } diff --git a/app/queries/projects/list_query.rb b/app/queries/projects/list_query.rb index 91a2a762..9e35b717 100644 --- a/app/queries/projects/list_query.rb +++ b/app/queries/projects/list_query.rb @@ -10,7 +10,7 @@ class Projects::ListQuery < ApplicationQuery end def call - q = Project.visible.search_project(params[:search]) + q = Project.secret_and_visible.search_project(params[:search]) scope = q.result(distinct: true) .includes(:project_category, :project_language, :repository, owner: :user_extension) diff --git a/app/services/projects/create_service.rb b/app/services/projects/create_service.rb index f4297acf..692d1efe 100644 --- a/app/services/projects/create_service.rb +++ b/app/services/projects/create_service.rb @@ -52,6 +52,11 @@ class Projects::CreateService < ApplicationService # end def repo_is_public - params[:private].blank? ? true : !params[:private] + license = License.find_by_id(params[:license_id]) + if license.is_secret + false + else + params[:private].blank? ? true : !params[:private] + end end end diff --git a/app/views/apply_signatures/create.json.jbuilder b/app/views/apply_signatures/create.json.jbuilder new file mode 100644 index 00000000..acffa1cb --- /dev/null +++ b/app/views/apply_signatures/create.json.jbuilder @@ -0,0 +1,4 @@ +json.id @signature.id +json.attachment do + json.name @attachment.filename +end diff --git a/app/views/licenses/index.json.jbuilder b/app/views/licenses/index.json.jbuilder index 706262a6..110cd84c 100644 --- a/app/views/licenses/index.json.jbuilder +++ b/app/views/licenses/index.json.jbuilder @@ -1 +1 @@ -json.licenses @licenses, :id, :name +json.licenses @licenses, :id, :name, :is_secret diff --git a/app/views/projects/_project_detail.json.jbuilder b/app/views/projects/_project_detail.json.jbuilder index d7cd196e..41be421b 100644 --- a/app/views/projects/_project_detail.json.jbuilder +++ b/app/views/projects/_project_detail.json.jbuilder @@ -3,38 +3,44 @@ if user.blank? nil else json.id project.id -json.repo_id project&.repository&.id -json.identifier project.identifier -json.name project.name -json.description Nokogiri::HTML(project.description).text -json.visits project.visits -json.praises_count project.praises_count.to_i -json.forked_count project.forked_count.to_i -json.is_public project.is_public -json.mirror_url project.repository&.mirror_url -json.type project&.numerical_for_project_type -json.last_update_time render_unix_time(project.updated_on) -json.time_ago time_from_now(project.updated_on) -json.forked_from_project_id project.forked_from_project_id -json.author do - json.name user.try(:show_real_name) - json.login user.login - json.image_url url_to_avatar(project.owner) -end -json.category do - if project.project_category.blank? - json.nil! - else - json.id project.project_category.id - json.name project.project_category.name + json.repo_id project&.repository&.id + json.identifier project.identifier + json.name project.name + json.description Nokogiri::HTML(project.description).text + json.visits project.visits + json.praises_count project.praises_count.to_i + json.forked_count project.forked_count.to_i + json.is_public project.is_public + json.is_secret project.is_secret + json.mirror_url project.repository&.mirror_url + json.type project&.numerical_for_project_type + json.last_update_time render_unix_time(project.updated_on) + json.time_ago time_from_now(project.updated_on) + json.forked_from_project_id project.forked_from_project_id + json.author do + json.name user.try(:show_real_name) + json.login user.login + json.image_url url_to_avatar(project.owner) + end + json.category do + if project.project_category.blank? + json.nil! + else + json.id project.project_category.id + json.name project.project_category.name + end + end + json.language do + if project.project_language.blank? + json.nil! + else + json.id project.project_language.id + json.name project.project_language.name + end + end + user_apply_signatures = project.apply_signatures.with_user_id(current_user.id) + json.user_apply_signatures user_apply_signatures do |signature| + json.id signature.id + json.status signature.status end end -json.language do - if project.project_language.blank? - json.nil! - else - json.id project.project_language.id - json.name project.project_language.name - end -end -end diff --git a/app/views/projects/show.json.jbuilder b/app/views/projects/show.json.jbuilder index e17da467..c9a8b15d 100644 --- a/app/views/projects/show.json.jbuilder +++ b/app/views/projects/show.json.jbuilder @@ -1,6 +1,12 @@ json.name @project.name json.identifier @project.identifier json.is_public @project.is_public +json.is_secret @project.is_secret json.description @project.description json.repo_id @project.repository.id json.repo_identifier @project.repository.identifier +user_apply_signatures = @project.apply_signatures.with_user_id(current_user.id) +json.user_apply_signatures user_apply_signatures do |signature| + json.id signature.id + json.status signature.status +end \ No newline at end of file diff --git a/app/views/repositories/edit.json.jbuilder b/app/views/repositories/edit.json.jbuilder index 355823b1..45f88858 100644 --- a/app/views/repositories/edit.json.jbuilder +++ b/app/views/repositories/edit.json.jbuilder @@ -5,3 +5,4 @@ json.project_description @project.description json.project_category_id @project.project_category_id json.project_language_id @project.project_language_id json.private !@project.is_public +json.is_secret @project.is_secret diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index 539f713a..c2c4bf28 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -16,6 +16,12 @@ json.permission User.current&.admin? ? "Manager" : @project.get_premission(@use json.mirror_url @project&.repository.mirror_url json.mirror @project&.repository.mirror_url.present? json.type @project.numerical_for_project_type +json.is_secret @project.is_secret +user_apply_signatures = @project.apply_signatures.with_user_id(current_user.id) +json.user_apply_signatures user_apply_signatures do |signature| + json.id signature.id + json.status signature.status +end unless @project.common? json.mirror_status @repo.mirror_status diff --git a/config/routes.rb b/config/routes.rb index df6eae80..da70a528 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,6 +35,7 @@ Rails.application.routes.draw do delete :destroy_files end end + resources :apply_signatures, only: [:create] get 'home/index' get 'home/search' get 'main/first_stamp' diff --git a/db/migrate/20201225064324_create_apply_signatures.rb b/db/migrate/20201225064324_create_apply_signatures.rb new file mode 100644 index 00000000..ce0cba8b --- /dev/null +++ b/db/migrate/20201225064324_create_apply_signatures.rb @@ -0,0 +1,11 @@ +class CreateApplySignatures < ActiveRecord::Migration[5.2] + def change + create_table :apply_signatures do |t| + t.references :user + t.references :project + t.integer :status, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20201225065222_add_some_columns_about_apply_signatures.rb b/db/migrate/20201225065222_add_some_columns_about_apply_signatures.rb new file mode 100644 index 00000000..83e1913e --- /dev/null +++ b/db/migrate/20201225065222_add_some_columns_about_apply_signatures.rb @@ -0,0 +1,6 @@ +class AddSomeColumnsAboutApplySignatures < ActiveRecord::Migration[5.2] + def change + add_column :licenses, :is_secret, :boolean, default: false + add_column :members, :is_apply_signature, :boolean, default: false + end +end diff --git a/spec/models/apply_signature_spec.rb b/spec/models/apply_signature_spec.rb new file mode 100644 index 00000000..6c7f41b9 --- /dev/null +++ b/spec/models/apply_signature_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ApplySignature, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end