This commit is contained in:
2026-04-09 22:44:09 +09:00
parent 7b15cb2c5a
commit b2d72ffcb0
7 changed files with 218 additions and 16 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
+35
View File
@@ -0,0 +1,35 @@
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'
validates :version_no, presence: true, numerically: { 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