投稿に対する履歴(#264) (#307)

Merge branch 'main' into feature/264

#264

#264

#264

#264

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #307
This commit was merged in pull request #307.
This commit is contained in:
2026-04-11 17:05:57 +09:00
parent e021423904
commit c36b2c8a1b
10 changed files with 533 additions and 17 deletions
+3
View File
@@ -11,6 +11,7 @@ class Post < ApplicationRecord
has_many :user_post_views, dependent: :delete_all
has_many :post_similarities, dependent: :delete_all
has_many :post_versions
has_one_attached :thumbnail
@@ -30,6 +31,8 @@ class Post < ApplicationRecord
super(options).merge(thumbnail: nil)
end
def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
def related limit: nil
ids = post_similarities.order(cos: :desc)
ids = ids.limit(limit) if limit
+38
View File
@@ -0,0 +1,38 @@
class PostVersion < ApplicationRecord
before_update do
raise ActiveRecord::ReadOnlyRecord, '版は更新できません.'
end
before_destroy do
raise ActiveRecord::ReadOnlyRecord, '版は削除できません.'
end
belongs_to :post
belongs_to :parent, class_name: 'Post', optional: true
belongs_to :created_by_user, class_name: 'User', optional: true
enum :event_type, { create: 'create',
update: 'update',
discard: 'discard',
restore: 'restore' }, prefix: true, validate: true
validates :version_no, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :event_type, presence: true, inclusion: { in: event_types.keys }
validates :url, presence: true
validate :validate_original_created_range
scope :chronological, -> { order(:version_no, :id) }
private
def validate_original_created_range
f = original_created_from
b = original_created_before
return if f.blank? || b.blank?
if f >= b
errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.'
end
end
end
+10
View File
@@ -1,3 +1,6 @@
require 'set'
class Tag < ApplicationRecord
include MyDiscard
@@ -150,6 +153,8 @@ class Tag < ApplicationRecord
def self.merge_tags! target_tag, source_tags
target_tag => Tag
affected_post_ids = Set.new
Tag.transaction do
Array(source_tags).compact.uniq.each do |source_tag|
source_tag => Tag
@@ -158,6 +163,7 @@ class Tag < ApplicationRecord
source_tag.post_tags.kept.find_each do |source_pt|
post_id = source_pt.post_id
affected_post_ids << post_id
source_pt.discard_by!(nil)
unless PostTag.kept.exists?(post_id:, tag: target_tag)
PostTag.create!(post_id:, tag: target_tag)
@@ -180,6 +186,10 @@ class Tag < ApplicationRecord
end
end
Post.where(id: affected_post_ids.to_a).find_each do |post|
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: nil)
end
# 投稿件数を再集計
target_tag.update_columns(post_count: PostTag.kept.where(tag: target_tag).count)
end