feat: 投稿とタグのリレーション・テーブルについて論理削除と履歴を追加(#84) (#148)

#84 マイグレ修正

Merge remote-tracking branch 'origin/main' into feature/084

#84 構文エラー修正

#84

#84

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #148
This commit was merged in pull request #148.
This commit is contained in:
2025-12-13 16:34:45 +09:00
parent 3b32d4d8ac
commit 9a656a9e6e
8 changed files with 173 additions and 56 deletions
+7 -5
View File
@@ -1,11 +1,13 @@
require 'mini_magick'
class Post < ApplicationRecord
require 'mini_magick'
belongs_to :parent, class_name: 'Post', optional: true, foreign_key: 'parent_id'
belongs_to :uploaded_user, class_name: 'User', optional: true
has_many :post_tags, dependent: :destroy
has_many :tags, through: :post_tags
has_many :post_tags, dependent: :destroy, inverse_of: :post
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
has_many :tags, through: :active_post_tags
has_many :user_post_views, dependent: :destroy
has_many :post_similarities_as_post,
class_name: 'PostSimilarity',
+18
View File
@@ -1,7 +1,25 @@
class PostTag < ApplicationRecord
include Discard::Model
belongs_to :post
belongs_to :tag, counter_cache: :post_count
belongs_to :created_user, class_name: 'User', optional: true
belongs_to :deleted_user, class_name: 'User', optional: true
validates :post_id, presence: true
validates :tag_id, presence: true
validates :post_id, uniqueness: {
scope: :tag_id,
conditions: -> { where(discarded_at: nil) } }
def discard_by! deleted_user
return self if discarded?
transaction do
update!(discarded_at: Time.current, deleted_user:)
Tag.where(id: tag_id).update_all('post_count = GREATEST(post_count - 1, 0)')
end
self
end
end
+6 -4
View File
@@ -1,6 +1,8 @@
class Tag < ApplicationRecord
has_many :post_tags, dependent: :destroy
has_many :posts, through: :post_tags
has_many :post_tags, dependent: :delete_all, inverse_of: :tag
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
has_many :posts, through: :active_post_tags
has_many :tag_aliases, dependent: :destroy
has_many :nico_tag_relations, foreign_key: :nico_tag_id, dependent: :destroy
@@ -43,13 +45,13 @@ class Tag < ApplicationRecord
'meta:' => 'meta' }.freeze
def self.tagme
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
@tagme ||= Tag.find_or_create_by!(name: 'タグ希望') do |tag|
tag.category = 'meta'
end
end
def self.bot
@bot ||= Tag.find_or_initialize_by(name: 'bot操作') do |tag|
@bot ||= Tag.find_or_create_by!(name: 'bot操作') do |tag|
tag.category = 'meta'
end
end