From 2985dc790223a7615ae8512d592c288a2d9fcd1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=B1=E5=91=B1=E5=91=B1?= Date: Tue, 21 Nov 2023 17:06:43 +0800 Subject: [PATCH] add uuid --- app/controllers/attachments_controller.rb | 4 +- app/models/attachment.rb | 88 ++++++++++--------- app/models/issue.rb | 7 +- .../20231121084405_add_uuid_to_attachments.rb | 5 ++ 4 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 db/migrate/20231121084405_add_uuid_to_attachments.rb diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 2bbccb495..9ed2632db 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -146,7 +146,7 @@ class AttachmentsController < ApplicationController if params[:type] == 'history' AttachmentHistory.find params[:id] else - Attachment.find params[:id] + Attachment.find params[:id] || Attachment.find_by(uuid: params[:id]) end end @@ -217,7 +217,7 @@ class AttachmentsController < ApplicationController def attachment_candown unless current_user.admin? || current_user.business? candown = true - if @file.container + if @file.container && @file.uuid.nil? if @file.container.is_a?(Issue) project = @file.container.project candown = project.is_public || (current_user.logged? && project.member?(current_user)) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index f79aca153..defc73662 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1,44 +1,45 @@ -# == Schema Information -# -# Table name: attachments -# -# id :integer not null, primary key -# container_id :integer -# container_type :string(30) -# filename :string(255) default(""), not null -# disk_filename :string(255) default(""), not null -# filesize :integer default("0"), not null -# content_type :string(255) default("") -# digest :string(60) default(""), not null -# downloads :integer default("0"), not null -# author_id :integer default("0"), not null -# created_on :datetime -# description :text(65535) -# disk_directory :string(255) -# attachtype :integer default("1") -# is_public :integer default("1") -# copy_from :integer -# quotes :integer default("0") -# is_publish :integer default("1") -# publish_time :datetime -# resource_bank_id :integer -# unified_setting :boolean default("1") -# cloud_url :string(255) default("") -# course_second_category_id :integer default("0") -# delay_publish :boolean default("0") -# memo_image :boolean default("0") -# extra_type :integer default("0") -# -# Indexes -# -# index_attachments_on_author_id (author_id) -# index_attachments_on_container_id_and_container_type (container_id,container_type) -# index_attachments_on_course_second_category_id (course_second_category_id) -# index_attachments_on_created_on (created_on) -# index_attachments_on_is_public (is_public) -# index_attachments_on_quotes (quotes) -# - +# == Schema Information +# +# Table name: attachments +# +# id :integer not null, primary key +# container_id :integer +# container_type :string(30) +# filename :string(255) default(""), not null +# disk_filename :string(255) default(""), not null +# filesize :integer default("0"), not null +# content_type :string(255) default("") +# digest :string(60) default(""), not null +# downloads :integer default("0"), not null +# author_id :integer default("0"), not null +# created_on :datetime +# description :text(65535) +# disk_directory :string(255) +# attachtype :integer default("1") +# is_public :integer default("1") +# copy_from :integer +# quotes :integer default("0") +# is_publish :integer default("1") +# publish_time :datetime +# resource_bank_id :integer +# unified_setting :boolean default("1") +# cloud_url :string(255) default("") +# course_second_category_id :integer default("0") +# delay_publish :boolean default("0") +# memo_image :boolean default("0") +# extra_type :integer default("0") +# uuid :string(255) +# +# Indexes +# +# index_attachments_on_author_id (author_id) +# index_attachments_on_container_id_and_container_type (container_id,container_type) +# index_attachments_on_course_second_category_id (course_second_category_id) +# index_attachments_on_created_on (created_on) +# index_attachments_on_is_public (is_public) +# index_attachments_on_quotes (quotes) +# + @@ -97,6 +98,11 @@ class Attachment < ApplicationRecord downloads end + def generate_uuid + self.uuid = uuid || SecureRandom.uuid + save! + end + def quotes_count quotes.nil? ? 0 : quotes end diff --git a/app/models/issue.rb b/app/models/issue.rb index 210f3a61f..504804f13 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -102,7 +102,7 @@ class Issue < ApplicationRecord scope :closed, ->{where(status_id: 5)} scope :opened, ->{where.not(status_id: 5)} after_create :incre_project_common, :incre_user_statistic, :incre_platform_statistic - after_save :change_versions_count, :send_update_message_to_notice_system, :associate_attachment_container, :refresh_root_issue_count + after_save :change_versions_count, :send_update_message_to_notice_system, :associate_attachment_container, :refresh_root_issue_count, :generate_uuid after_destroy :update_closed_issues_count_in_project!, :decre_project_common, :decre_user_statistic, :decre_platform_statistic def incre_project_common @@ -186,6 +186,11 @@ class Issue < ApplicationRecord end end + def generate_uuid + return if pm_project_id.nil? + attachments.map(&:generate_uuid) + end + def is_collaborators? if self.assigned_to_id.present? && self.project.present? self.project.member?(self.assigned_to_id) diff --git a/db/migrate/20231121084405_add_uuid_to_attachments.rb b/db/migrate/20231121084405_add_uuid_to_attachments.rb new file mode 100644 index 000000000..5d0e2ba02 --- /dev/null +++ b/db/migrate/20231121084405_add_uuid_to_attachments.rb @@ -0,0 +1,5 @@ +class AddUuidToAttachments < ActiveRecord::Migration[5.2] + def change + add_column :attachments, :uuid, :string, index: true + end +end