From f78a08bea117ea633cb07b576aacecd860387f16 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Mon, 5 Oct 2026 00:59:38 +0900 Subject: [PATCH] #422 --- .../tag_descriptions_controller.rb | 38 ++++--------- .../app/controllers/wiki_pages_controller.rb | 4 ++ .../20261005000000_repair_wiki_updated_at.rb | 53 +++++++++++++++++++ backend/db/schema.rb | 2 +- 4 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 backend/db/migrate/20261005000000_repair_wiki_updated_at.rb diff --git a/backend/app/controllers/tag_descriptions_controller.rb b/backend/app/controllers/tag_descriptions_controller.rb index 726123f..06d78c2 100644 --- a/backend/app/controllers/tag_descriptions_controller.rb +++ b/backend/app/controllers/tag_descriptions_controller.rb @@ -14,7 +14,7 @@ class TagDescriptionsController < ApplicationController render json: representation(description) end - def create + def update return head :unauthorized unless current_user return head :forbidden unless current_user.gte_member? @@ -24,39 +24,23 @@ class TagDescriptionsController < ApplicationController tag = Tag.find_by(id: params[:id]) return head :not_found unless tag - if TagDescription.exists?(tag:, language_code: 'ja') - return render_unprocessable_entity('説明はすでに存在します.') + description = TagDescription.find_by(tag:, language_code: 'ja') + + if description + TagDescriptionOperation.update!(tag:, body:, updated_by_user: current_user) + description.reload + render json: representation(description) + else + TagDescriptionOperation.create!(tag:, body:, created_by_user: current_user) + description = TagDescription.find_by!(tag:, language_code: 'ja') + render json: representation(description), status: :created end - - TagDescriptionOperation.create!(tag:, body:, created_by_user: current_user) - description = TagDescription.find_by!(tag:, language_code: 'ja') - - render json: representation(description), status: :created rescue ActiveRecord::RecordInvalid => e render_validation_error e.record rescue ActiveRecord::RecordNotUnique render_record_not_unique end - def update - return head :unauthorized unless current_user - return head :forbidden unless current_user.gte_member? - - body = params[:body].to_s - return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank? - - description = TagDescription.find_by(tag_id: params[:id], language_code: 'ja') - return create unless description - - TagDescriptionOperation.update!( - tag: description.tag, body:, updated_by_user: current_user) - description.reload - - render json: representation(description) - rescue ActiveRecord::RecordInvalid => e - render_validation_error e.record - end - private def representation description diff --git a/backend/app/controllers/wiki_pages_controller.rb b/backend/app/controllers/wiki_pages_controller.rb index 60b7407..8fb84e9 100644 --- a/backend/app/controllers/wiki_pages_controller.rb +++ b/backend/app/controllers/wiki_pages_controller.rb @@ -2,6 +2,10 @@ class WikiPagesController < ApplicationController rescue_from Wiki::Commit::Conflict, with: :render_wiki_conflict def index + # 互換性のための化石リソース + # TODO: ヘルプおよびタグ・グループ追加後に, + # TODO: タグ説明とヘルプ、タグ・グループから取得する作りに変更する. + # title = params[:title].to_s.strip descriptions = TagDescription.where(language_code: 'ja') .includes(tag: :tag_names) diff --git a/backend/db/migrate/20261005000000_repair_wiki_updated_at.rb b/backend/db/migrate/20261005000000_repair_wiki_updated_at.rb new file mode 100644 index 0000000..5509b65 --- /dev/null +++ b/backend/db/migrate/20261005000000_repair_wiki_updated_at.rb @@ -0,0 +1,53 @@ +class RepairWikiUpdatedAt < ActiveRecord::Migration[8.0] + class WikiPage < ActiveRecord::Base + self.table_name = 'wiki_pages' + end + + class WikiVersion < ActiveRecord::Base + self.table_name = 'wiki_versions' + end + + class TagDescription < ActiveRecord::Base + self.table_name = 'tag_descriptions' + end + + class TagDescriptionVersion < ActiveRecord::Base + self.table_name = 'tag_description_versions' + end + + def up + say_with_time 'Repairing wiki_pages.updated_at from wiki_versions' do + WikiPage.find_each do |page| + version = + WikiVersion + .where(wiki_page_id: page.id) + .order(version_no: :desc) + .first + + next unless version + + page.update_columns(updated_at: version.created_at) + end + end + + say_with_time 'Repairing tag_descriptions.updated_at from tag_description_versions' do + TagDescription.find_each do |description| + version = + TagDescriptionVersion + .where( + tag_id: description.tag_id, + language_code: description.language_code) + .order(version_no: :desc) + .first + + next unless version + + description.update_columns(updated_at: version.created_at) + end + end + end + + def down + ; + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 914edcb..611cced 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2026_10_03_010000) do +ActiveRecord::Schema[8.0].define(version: 2026_10_05_000000) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false